Link to home
Start Free TrialLog in
Avatar of RwSchultzster
RwSchultzster

asked on

Subform events

When do subform events fire? I have a subform that has the records for the app. I built some test code to see when the Main form events fire and that works fine. Below are some segments of the code. Just builds a string and then put the string in textbox. Am I using the variable strActions correctly?

Option Compare Database
Option Explicit

Public strActions

Private Sub Form_Activate()
strActions = strActions + "on activate" & vbCrLf
End Sub

Private Sub Form_Load()
strActions = strActions + "on load" & vbCrLf
End Sub


Private Sub Form_Open(Cancel As Integer)
strActions = strActions + "on open" & vbCrLf
strActions = strActions + "Connection done" & vbCrLf
strActions = strActions + "Recordset = rstEvents" & vbCrLf
edtActions.SetFocus
edtActions.Text = strActions
End Sub

I have this for the subform events. No errors but I do not see the string value or any effect. The records show in the subform.

Option Compare Database

Private Sub Form_Activate()
Me.Parent!edtActions.ForeColor = 255
Me.Parent.strActions = strActions + "zzzz" & vbCrLf
End Sub

Private Sub Form_Load()
strActions = strActions + "zzzz" & vbCrLf
End Sub

Private Sub Form_Open(Cancel As Integer)
strActions = strActions + "zzzz" & vbCrLf
End Sub

ASKER CERTIFIED SOLUTION
Avatar of jefftwilley
jefftwilley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
in the case of your code, you might want to declare your strActions variable in the subform's module.
J
SOLUTION
Avatar of Leigh Purvis
Leigh Purvis
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RwSchultzster
RwSchultzster

ASKER

I'm giving LPurvis and JefftWilley both points. Jeff's link explained that the subform is created before the main form is created. Thus it's events were occurring before my variable strActions was being declared in the main form. LPurvis helped with syntax and scope issue.

Is there a CORRECT way to declare truly Global variables before any forms are created?

Thanks for the help.
Although I avoid Global variables like the plague, an accepted approach would be to create a Code Module to hold any and all variables you want to declare globally. They are more easily managed in this way as they are all kept together.

Option Compare Database
Option Explicit
'This module contains all of my Global Variables

Global sStringVar as string
Global bBooleanVar as boolean
Global lLongVar as long

etc...

These will be declared when the database opens and will remain available for the life of the application. So be careful how you use them. You want to make sure you clear them before you use them or when you're finished using them...so they do not carry over values from previous use.
Enjoy!
J