Advent Day 19 Ms Access Gurus

VBA > Application > Set Application Title

Set the application title to a specified string, whatever you want! Easily show the path and filename of the database in the title bar when it is opened using the AutoExec macro to run a function.

Screen shots

show how to Set Application Title on AutoExec macro

You can set up an AutoExec macro that updates the title bar to the path and filename of the database you are in. This is especially nice for developers -- well anybody really -- but particularly if you are changing designs or writing code.

Examples


set application title to a specified string

Call SetApplicationTitle("My Application Title")

set application title to the path and file name of the current database:

Call SetApplicationTitle( CurrentDb.Name )

... or this does the same thing without having to pass any parameters:

Call SetApplicationTitleMyPathFile()

... the same thing again. When you don't use Call, or handle a return value, parameters are not enclosed in parentheses (even if there are any).

SetApplicationTitleMyPathFile

Logic: SetApplicationTitle

Initally Skip errors.

Dimension a database object variable, and set it to the current database.

Set the AppTitle property of the current database to the specified string. If the AppTitle property does not yet exist (which it should), then create it.

Set up error handler

RefreshTitleBar followed by DoEvents (not sure this is needed) so the change shows up right away.

If the calling program specified pBooShowMessage to be True (default = False), then display a message that the application title was set (you can see if it worked without a message).

Exit code: Release the database object and then exit so execution won't slip into the error handler without a reason.

Parameter

Optional


Logic: SetApplicationTitle_DbPathFile

This function calls SetApplicationTitle, sending the path and file name of the current database (the CurrentDb.Name property).

This is easily called from a macro, like the AutoExec macro. It requires no parameters.

Macros can run functions but not subs (opposite from Excel), which is why a function is defined for SetApplicationTitle_DbPathFile, and no return value is bothered to be set (it will default to False).

AutoExec macro

The AutoExec macro runs automatically when you open a database (unless you bypass it). This calls SetApplicationTitle_DbPathFile, as illustrated in the screen shot, which sends the current database path and filename as the required string parameter.

Code

'*************** Code Start *****************************************************
' Purpose  : Set the application title to a specified string   
' Author   : crystal (strive4peace) 
' Return   : Boolean
' License  : below code
' Code List: www.MsAccessGurus.com/code.htm
'--------------------------------------------------------------------------------

' SetApplicationTitle

'-------------------------------------------------------------------------------- '
Public Function SetApplicationTitle( psAppTitle As String _ , Optional pBooShowMessage As Boolean = False _ ) As Boolean 'strive4peace 'skip errors and continue On Error Resume Next SetApplicationTitle = False 'return value probably doesn't matter 'dimension and set database object Dim db As DAO.Database Set db = CurrentDb 'change the application title db.Properties("AppTitle") = psAppTitle 'go to error handler if there is a future problem If Err.Number > 0 Then 'if there was an error, the property might need to be created On Error GoTo Proc_err db.Properties.Append _ db.CreateProperty("AppTitle", dbText, psAppTitle) Else On Error GoTo Proc_err End If 'refresh the title bar display Application.RefreshTitleBar 'do it now DoEvents 'if pBooShowMessage is not false, then show a message If pBooShowMessage <> False Then MsgBox "Application Title set to: " _ & vbCrLf & vbCrLf & psAppTitle, , "Done" End If SetApplicationTitle = True 'probably would not test this proc_exit: 'skip errors and continue On Error Resume Next 'release database object Set db = Nothing Exit Function proc_err: 'commented because you probably just want to exit 'MsgBox Err.Description, , _ "ERROR " & Err.Number _ & " SetApplicationTitle" Resume Proc_Exit 'this could only be executed if you break the code ' (on MsgBox) and set this to be the Next Statement Resume End Function '--------------------------------------------------------------------------------

' SetApplicationTitle_DbPathFile

'-------------------------------------------------------------------------------- '
Public Function SetApplicationTitle_DbPathFile() As Boolean 's4p 170603, 170820, 181218 'sets application title to path and file name of current database 'runs SetApplicationTitle On Error Resume Next Call SetApplicationTitle(CurrentDb.Name) End Function ' ' LICENSE ' You may freely use and share this code ' provided this license notice and comment lines are not changed; ' code may be modified provided you clearly note your changes. ' You may not sell this code alone, or as part of a collection, ' without my handwritten permission. ' All ownership rights reserved. Use at your own risk. ' ~ crystal (strive4peace) www.MsAccessGurus.com '*************** Code End *******************************************************

Share

Share with others ... here's the link to copy:
https://MsAccessGurus.com/VBA/Code/App_SetApplicationTitle.htm

References

Docs / Office VBA Reference / Language reference / Reference / Statements / On Error

Help: On Error statement

Docs / Office VBA Reference / Language reference / Reference / Statements / Error

Help: Error statement

Docs ...

Help: AppTitle Property