Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the name of the button that opened a form? 3

Status
Not open for further replies.

mavalon

Programmer
Apr 18, 2003
125
0
0
US
When I open a form, I need to determine the name of the button from the previous form that opened the current form to know whether or not to display a certain textbox. I have two buttons on the Switchboard which can open the form. I just need to know which of the Switchboard buttons opened it. (If Option1 opened it, I show the field, If Option2 opened it, I hide the field.)

Did that make sense? %-)
 
Create a global variable in a database module:
Global vBtnPointer as Integer

In the button click event procedure put the following VBA code:
vBtnPointer = 1 'for the first button
OR
vBtnPointer = 2 'for the second button

Now in the OnLoad of the new form you can reference this global variable to make the appropriate text boxes visible or invisible:
If vBtnPointer = 1 then
Me![Text1].visible = true
else
Me![Text2].visible = true
end if

Post back if you need more assistance.

Bob Scriver

Nobody believes the official spokesman... but everybody trusts an unidentified source.
Author, Bagdad Bob???

 
Pass OpenArgs from the switchboard to the opened form as follows:

Private Sub cmdOption1_Click()
DoCmd.OpenForm "Form1",,,,,,"Option1"
End Sub

Private Sub cmdOption1_Click()
DoCmd.OpenForm "Form1",,,,,,"Option2"
End Sub

Note: As you type in the DoCmd commands above, you'll see that "Option1" and "Option2" are located in the OpenArgs clause of the command.

In the OnOpen event of the form that is opening (Form1 in the example above), use the OpenArgs to control visibility of the field in question. For example:

If Me.OpenArgs = "Option1" then
Me.Field1.Visible = True
ElseIf Me.OpenArgs = "Option2" Then
Me.Field1.Visible = True
End If

Alternatively (and more efficiently) just put the OpenArgs clause in one of the switchboard buttons and test for it in the OnOpen of "Form1". For example:

If Me.OpenArgs = "Option1" then
Me.Field1.Visible = True
Else
Me.Field1.Visible = False
End If

Note that if you're creative, you can use this method to pass any number of "arguments" (values) to a form or report. For example:

DoCmd.OpenForm "Form1",,,,,,"1:Arg1,2:Arg2,3:Arg3"

The value of Arg2 is culled out of the OpenArgs string as follows:

strOpenArg2 = Mid(Me.OpenArgs,InStr(Me.OpenArgs,"2:")+2,InStr(Me.OpenArgs,"3:")-1)

To see how this works, check out help for the Mid() and InStr() functions.

Once you've set strOpenArg2 to the value "Arg2", then you can use it wherever you need to in "Form1".
 
I greatly appreciate your comments. I found that declaring a global variable called "myOpener" and setting it to either 1 or 2 depending on the button clicked, I could then perform the following method:

Private Sub Form_Open(Cancel As Integer)
If MyOpener = 1 Then
Me!searchbox.Enabled = False
End If
End Sub

(I don't know why I couldn't think of this myself.)

Thanks for all your help. [2thumbsup]
 
Its just that using Globals when not necessary is "bad form" as I've been told many times. The advice I've always been given is to scope variables as narrowly as possible and pass values by using arguments.

Also, I've been told its bad form to dimension global variables in a form module - that they should be dim'd in a standard module, if at all.

If you don't have too many globals it shouldn't hurt performanced. But globally scoped variables are so "easy" to use that they can be habit forming.

Another problem with global variables is that its easy to lose track of all the places the variable's value is modified - making it more tedious to debug - ie: across multiple form modules.
 
Hey ReluctantDataGuy,

Just used your advice about the OpenArgs in order to keep track of which field I want to insert a date in using a popup form with a Calendar control. Works like a charm. Thanks a lot.

H
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top