Hi guys,

One form1 I have 2 command buttons - Add new record and other to view/edit existing record (the user can enter record id and click view/edit existing record button).

If the user clicks Add new record, the user is taken to the next screen where the record id is auto-generated & new record is created. Whereas if Edit/View existing record is clicked, existing information for that record will be displayed on the next screen.

How do I determine which command button has been pressed on form1 so that on form2 Add or Update code can be called based on the button clicked.

Any input is greatly appreciated, thanks!

9 Years
Discussion Span
Last Post by jireh

Try to add a module then put a global variable

Public AddUpdate As String
End Module

On form1.

Private Sub Button1_Click()
'Assuming this button is an Add button
  AddUpdate ="Add"
End Sub

Private Sub Button2_Click()
'Assuming this button is an Update button
  AddUpdate ="Update"
End Sub

On form2

Private Sub Form_Load()
 Label1.Caption = AddUpdate
End Sub

so with that simple concept Form2 can determine what button was pressed in Form1.

Module Code:
Public ButtonPressed As String

Button1 Code:
ButtonPressed = "Button1"

Button2 Code:
ButtonPressed = "Button2"

to get what button is pressed...
Select Case ButtonPressed
Case "Button1"
       ...your statement here
Case "Button2"
       ...your statement here
Case Else
       ...your statement here
End Select

Why do you need to know what button is pressed?


Thanks guys for your help! I was digressed to another project & just got back to this. The solution worked.

I had to know which button is pressed because if the user clicked "Add", behind the scenes I'm auto generating new record id in a particular format. If the user clicked "update/view record", then I just need to display existing record info.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.