[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Subform events

Posted on 2007-10-04
5
Medium Priority
?
999 Views
Last Modified: 2008-01-09
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

0
Comment
Question by:RwSchultzster
  • 3
5 Comments
 
LVL 34

Accepted Solution

by:
jefftwilley earned 200 total points
ID: 20018807
you can find a little assistance for the order of events here. There are many sites that outline this very topic.

http://office.microsoft.com/en-gb/access/HP051867611033.aspx
J
0
 
LVL 34

Expert Comment

by:jefftwilley
ID: 20018814
in the case of your code, you might want to declare your strActions variable in the subform's module.
J
0
 
LVL 44

Assisted Solution

by:Leigh Purvis
Leigh Purvis earned 200 total points
ID: 20020072
A couple of things.

Firstly - you'd be generally better advised to use ampersand (&) to concatenate strings rather than + (unless you deliberately intend to propagate Nulls).  You've used them part of the time...
What you have works because Variants aren't initialized as Null but as Empty.

Secondly - rather than *move* the variable - you seem to want to refer to the parent's module level variable.
You've correctly made it public, but you still need to qualify it properly (as public variables in class modules are only public when qualified by the class instance... i.e. the current form).

So in your subform code you'd refer to it as
Me.Parent.strActions = Me.Parent.strActions & "zzzz" & vbCrLf
(once again - you were almost there yourself - just not using it consistently).

The reason it wasn't working (or that you couldn't see why) is because of the lack of an Option Explicit in your subform's module header.  This is so important always.  Otherwise implicit variable creation occurs - and that's what you had happening - a strActions variant created in your subform module, rather than referring to the parent form's.
0
 

Author Comment

by:RwSchultzster
ID: 20023783
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.
0
 
LVL 34

Expert Comment

by:jefftwilley
ID: 20024059
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question