View Full Version : Assigned Event Actions Dynamically


AVWIT
06-04-2007, 09:24 AM
Ok. I've got a form that has a bunch of dynamic label fields. I am reusing them and assigned the captions a value. I want to be able to click on any of these fields and open a new form and pass the caption value to the open args. I can do this for each one individually, but I have close to 200 labels on the page and I'd like to make this dynamically when I iterate through the labels. Here is my coding logic:

iterate through labels
- change OnClick property to OpenForm "x" and pass it OpenArgs "y"

when label is clicked on
- Form "x" should open
- OpenArgs should contain the caption value of the clicked on label box

Any help would be appreciated. Thanks. If there are any more questions on what i'm trying to accomplish and what I know will and won't work, let me know.

David

lagbolt
06-04-2007, 11:54 AM
1) Put a single function on the form that takes as paramaters the name of the form to open and the text of the label...
Private Function DoOpen(fName as string, sText as string)
DoCmd.OpenForm fName, OpenArgs:=sText
End Function
2) To the OnClick property of each of your labels, add text to call the function with parameters. This will look like...
=DoOpen("FormName", [LabelName].[Caption])
-or-
=DoOpen("FormName", "YourCaption")
Your traversal code should know the caption of each label so either of the above are easily doable. The key is the single function on the form which is called directly from the OnClick event.

AVWIT
06-05-2007, 10:44 AM
Thanks. That seems to work for what I am trying to do. Another question.

When the new form I open closes, how can I call a function on my current form again? (Basically after I change the data on the popup form I wanna be able to call a function to refresh my info on the form that called the other one) Thanks.

Dave

lagbolt
06-05-2007, 11:30 AM
To run a sub or function on an open form, say FormA, make sure that routine is declared as public...
Public Sub SomeRoutine(SomeParam As String)
'does nothing
End Sub
Then, elsewhere in your project you can call that function using code like...
Forms("FormA").SomeRoutine "a string parameter"
If you want to use data from a form that is closing put your code in the Form_Unload() event. In the Form_Close() event the closing form's data will have already been discarded.
Private Sub Form_Unload(cancel as integer)
Forms("FormA").SomeRoutine Me.txtExample
End Sub

AVWIT
06-05-2007, 11:38 AM
Thanks.

Dave

lagbolt
06-05-2007, 11:41 AM
Cheers Dave,
Mark