NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click
References on the
Tools menu in the Visual Basic Editor, and make sure that the
Microsoft DAO 3.6 Object Library check box is selected.
Unless you set the
Application Title property in the
Startup dialog box to something different for your database, the default text that appears in the title bar is "Microsoft Access."
You can manually change the
Application Title property of a database by clicking
Startup on the
Tools menu, and then typing a new name in the
Application Title box. Then, whenever you open the database, the title bar displays the name that you typed.
You can also use Visual Basic for Applications to programmatically change
the Application Title every time the database opens. As an example, to set the Application Title to the name of the current database and the name of the current user in the database, follow these steps:
- Create a module and type or paste the following procedures:
Function SetApplicationTitle(ByVal MyTitle As String)
If SetStartupProperty("AppTitle", dbText, MyTitle) Then
Application.RefreshTitleBar
Else
Msgbox "ERROR: Could not set Application Title"
End If
End Function
Function SetStartupProperty(prpName As String, _
prpType As Variant, prpValue As Variant) As Integer
Dim DB As DAO.DATABASE, PRP As DAO.Property, WS As Workspace
Const ERROR_PROPNOTFOUND = 3270
Set DB = CurrentDb()
' Set the startup property value.
On Error GoTo Err_SetStartupProperty
DB.Properties(prpName) = prpValue
SetStartupProperty = True
Bye_SetStartupProperty:
Exit Function
Err_SetStartupProperty:
Select Case Err
' If the property does not exist, create it and try again.
Case ERROR_PROPNOTFOUND
Set PRP = DB.CreateProperty(prpName, prpType, prpValue)
DB.Properties.Append PRP
Resume
Case Else
SetStartupProperty = False
Resume Bye_SetStartupProperty
End Select
End Function
Function CurrentMDB() As String
Dim i As Integer, FullPath As String
FullPath = CurrentDb.Name
' Search backward in string for back slash character.
For i = Len(FullPath) To 1 Step -1
' Return all characters to the right of the back slash.
If Mid(FullPath, i, 1) = "\" Then
CurrentMDB = Mid(FullPath, i + 1)
Exit Function
End If
Next i
End Function
- Create the following macro and save it as AppTitle:
Action
--------
RunCode
AppTitle Actions
-----------------
RunCode
Function Name: SetApplicationTitle(CurrentMDB() & " - " & CurrentUser)
- Run the macro.
Notice that the text in the title bar has changed to reflect the names of the database and the current user.