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!

Try to add a module then put a global variable
Example:

Module1
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.

so does this thread solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.