How can I dynamically set all the Application Forms Caption property
I have one Access application which accesses 3 environments (DEV; T开发者_如何学编程EST; PROD). An environment variable is set upon login to programmatically connect to the correct database.
We would like the user to see which environment they are connected to in each form they use. One thought is to set the color of the title bar, but that impacts all windows.
I have been attempting to put the environment in the form caption dynamically by using the environment variable.
Setting an Event Procedure in the Form Properties such as On Current or On Open such as:
Me.Caption = Me.Caption & " : " & Me!txtEnvName.Value
.... in a dynamic process upon signin to the application is what I would like to do.
Manually changing all the forms in the application is not a desired option.
Here is a sample of what a form would look like in the 3 different evironments:
Customers: DEV
//////////
Customers: TEST
/////////
Customers: PRODUCTION
You can set the captions for all the forms easily enough:
Const strEnvironment = " : DEV"
Sub FormCaption()
Dim frm As Object
For Each frm In CurrentProject.AllForms
DoCmd.OpenForm frm.Name, acDesign
Forms(frm.Name).Caption = frm.Name & strEnvironment
DoCmd.Close acForm, frm.Name, acSaveYes
Next
End Sub
However, I suggest you use the Open event of each of the forms to check a global variable and set the caption accordingly.
This adds the COMPUTERNAME environment variable to the Caption of my form.
Private Sub Form_Open(Cancel As Integer)
Me.Caption = Me.Caption & ": " & Environ("COMPUTERNAME")
End Sub
If you want to also include color to distinguish between the database instances, you could change the detail section background color. This would change it based on the value of Your_env_variable.
Dim lngColor As Long
Select Case Your_env_variable
Case "DEV"
lngColor = vbRed
Case "TEST"
lngColor = vbYellow
Case "PRODUCTION"
lngColor = vbGreen
Case Else
lngColor = -2147483633
End Select
Me.Detail.BackColor = lngColor
精彩评论