Can a form see which button opened it?

N

Nick in Tokyo

Hi, I want to use the same form to Add new data and to display results from a
query. The two buttons themselves are on different forms.

The query displays all the inventory for a client code and the add new just
tacks a new record on at the end (acNewRecord).

Is there an OnOpen function that can check to see if the button was the
Add_New button and go to acNewRecord and if not then to execute the query?
 
K

Ken Snell [MVP]

May be that the easier approach is to use the OpenArgs argument of the
OpenForm action to tell the form which button called it:

DoCmd.OpenForm "FormName", OpenArgs:="ButtonName"

Then you can use the Load event to run code that reads the OpenArgs in the
second form and to set the value of an invisible textbox on that form based
on that value:

Private Sub Form_Load()
If Me.OpenArgs = "AddNewButton" Then
Me.txtBox.Value = "new"
Else
Me.txtBox.Value = "old"
End

Then use the value of this textbox to know what the second form should do.
 
N

Nick in Tokyo

I can see what you mean but can't quite use it right.

My Form is based on a select query, which gets it's condition from a combo
box.
ie, you select a client and then open the form which displays all the
hardware information for that client.

Can I execute the query from within the form? ...

If Me.OpenArgs = "AddNew"
DoCmd.GoToRecord , , acNewRec
Else
Execute a select query and disply results in form.
 
K

Ken Snell [MVP]

I'd do something like this:

Private Sub Form_Load()
If Me.OpenArgs = "AddNew" Then
Me.DataEntry = True
Else
Me.DataEntry = False
End If

No need to "run" the query if that query is the Record Source for the form
that you're opening. Just use the above code.

--

Ken Snell
<MS ACCESS MVP>
 
N

Nick in Tokyo

Great. Works fine.

Thanks

I'd do something like this:

Private Sub Form_Load()
If Me.OpenArgs = "AddNew" Then
Me.DataEntry = True
Else
Me.DataEntry = False
End If

No need to "run" the query if that query is the Record Source for the form
that you're opening. Just use the above code.
 
Top