Private Sub btnUpdateCompanyName_Click() Dim accApp As Access.Application Dim strNewCompanyName As String Dim obj As AccessObject Dim ctl As Control Dim dbPath As String On Error GoTo ErrorHandler ' Get the new company name from the text box and handle Null value strNewCompanyName = Nz(Me.txtCompanyName.Value, "") ' Check if the text box is empty If Trim(strNewCompanyName) = "" Then MsgBox "Please enter the new company name.", vbExclamation Exit Sub End If ' Construct the relative path to the other database dbPath = CurrentProject.Path & "\Sales.accdb" ' Check if the other database is already open If IsDatabaseOpen(dbPath) Then MsgBox "The database is already open. Please close it before running this operation.", vbExclamation Exit Sub End If ' Open the other database Set accApp = New Access.Application accApp.OpenCurrentDatabase (dbPath) ' Loop through all forms For Each obj In accApp.CurrentProject.AllForms accApp.DoCmd.OpenForm obj.Name, acDesign For Each ctl In accApp.Forms(obj.Name).Controls If ctl.ControlType = acLabel And ctl.Name = "Label1" Then ctl.Caption = strNewCompanyName End If Next ctl accApp.DoCmd.Close acForm, obj.Name, acSaveYes Next obj ' Loop through all reports For Each obj In accApp.CurrentProject.AllReports accApp.DoCmd.OpenReport obj.Name, acViewDesign For Each ctl In accApp.Reports(obj.Name).Controls If ctl.ControlType = acLabel And ctl.Name = "Label1" Then ctl.Caption = strNewCompanyName End If Next ctl accApp.DoCmd.Close acReport, obj.Name, acSaveYes Next obj ' Close the other database accApp.CloseCurrentDatabase accApp.Quit Set accApp = Nothing ' Notify user MsgBox "Company name updated successfully!" Exit Sub ErrorHandler: If Not accApp Is Nothing Then accApp.CloseCurrentDatabase accApp.Quit Set accApp = Nothing End If MsgBox "An error occurred: " & Err.Description, vbCritical End Sub
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter