Cmd button msgBox dependent on previous cmd Button Choice (1 Viewer)

SimoneRene

Registered User.
Local time
Today, 23:35
Joined
Mar 15, 2017
Messages
58
Hi!

I have a form with two command buttons Save and Exit(close) both have event procedures that bring up prompt message box's e.g. Are you sure you want to save? OK or Cancel Buttons

The Exit button message box; Are you sure you want to exit without saving? Yes No buttons, only pops up if the Save button hasn't been clicked I used a Boolean function.

When the user clicks the Exit button (and hasn't clicked the Save Button) then clicks 'Yes' to question 'Are you sure you want to exit without saving?' A before update event runs and the record isn't saved.

The problem arises when the user clicks the Save button-Cancel then the Exit Button, the record IS saved to table and I don't want it to be.

So rather than using Boolean true false which determines if the button is clicked or not is there a way to detect the response from the button?

Thanks! :)
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 15:35
Joined
Aug 30, 2003
Messages
36,127
From a Yes/No message box? Sure:

Code:
response = MsgBox(msg, button, title)
If response = vbYes Then
  'what to do if yes
Else
  'what to do if not
End If
 

SimoneRene

Registered User.
Local time
Today, 23:35
Joined
Mar 15, 2017
Messages
58
From a Yes/No message box? Sure:

Code:
response = MsgBox(msg, button, title)
If response = vbYes Then
  'what to do if yes
Else
  'what to do if not
End If


Hi pbaldy,

I'm not sure what you mean...

This is the current code for the Save button:

Code:
Private Sub SaveBtn_Click()
buttonclicked = True
If FormattedMsgBox("Do you want to save?" & _
"@Saving will add new Drawing Number to Master List" & vbNewLine & _
"Select 'Cancel' to Return to Record. @", vbOKCancel, "Save this record?") = vbOK Then
DoCmd.RunCommand acCmdSaveRecord
Else
Me.Undo
Exit Sub
End If
End Sub

This is the current code for the exit button:

Code:
Private Sub ExitBtn_Click()
Dim Response As Integer
If buttonclicked = True Then
DoCmd.Close
Else
Response = MsgBox(Prompt:="Are you sure you want to Exit without Saving?", Buttons:=vbYesNo)

If Response = vbNo Then

MsgBox ("Please use 'Save Record' button")
Exit Sub
Else
Me.Undo
DoCmd.Close
End If
End If
End Sub


Are you saying instead of 'If buttonclicked = True Then' in the exitbtn code I can add something like:

response = FormattedMsgBox(msg, button, title)
If response = vbYes Then
'what to do if yes
Else
'what to do if not
End If

Or did I not explain my question very well?

Thanks,

Simone
 

jdraw

Super Moderator
Staff member
Local time
Today, 18:35
Joined
Jan 23, 2006
Messages
15,383
Or did I not explain my question very well?

Possibly.

You have 2 buttons, and each button has 2 options

My guess is you have to use the code Paul suggested for each button.
But that's my understanding of your question ---and I may be wrong.

You can check your logic by adding a break-point and stepping through the code
 

Minty

AWF VIP
Local time
Today, 23:35
Joined
Jul 26, 2013
Messages
10,371
If this is a bound form, your data will be saved automatically unless it is undone. You only really need a cancel button.
 

SimoneRene

Registered User.
Local time
Today, 23:35
Joined
Mar 15, 2017
Messages
58
Possibly.

You have 2 buttons, and each button has 2 options

My guess is you have to use the code Paul suggested for each button.
But that's my understanding of your question ---and I may be wrong.

You can check your logic by adding a break-point and stepping through the code

Yes I have two buttons and each button has two options which work fine.

The problem is the second button is dependant on the response chosen from the first button.

My code currently tells the second button if the first button has been clicked or not but not what the response was. Which is why I'm getting a problem.:confused:
 

SimoneRene

Registered User.
Local time
Today, 23:35
Joined
Mar 15, 2017
Messages
58
If this is a bound form, your data will be saved automatically unless it is undone. You only really need a cancel button.

Hi Minty,

Yes this is a bound form, I know save is automatic, I have added an Me.Undo on the before update event which runs if the save button hasn't been clicked however if the save button was clicked and the user clicked the response 'cancel' (also a Me.undo) When the user then goes on to click the exit button because the exit button knows the save button was clicked(regardless of the response) The record IS saved.
 

jdraw

Super Moderator
Staff member
Local time
Today, 18:35
Joined
Jan 23, 2006
Messages
15,383
You can capture the Response from first button click, and use that value in your test of the second button.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 15:35
Joined
Aug 30, 2003
Messages
36,127
Why not move

buttonclicked = True

to after

DoCmd.RunCommand acCmdSaveRecord
 

Minty

AWF VIP
Local time
Today, 23:35
Joined
Jul 26, 2013
Messages
10,371
You can't guarantee that something else the user may have done won't have already saved the record, unless you either use an Unbound form, or a Transactional Form (neither of which are simple to use).

They could press f5 to refresh, page up or down to go to another record, or alt&f4 to close the form.

Any of these mean your can't undo the changes.
 

SimoneRene

Registered User.
Local time
Today, 23:35
Joined
Mar 15, 2017
Messages
58
You can capture the Response from first button click, and use that value in your test of the second button.


Hi jdraw,

This is exactly what I want to do :) How could I achieve this?

Thank you,

Simone.
 

SimoneRene

Registered User.
Local time
Today, 23:35
Joined
Mar 15, 2017
Messages
58
Hi guys,

I think I have fixed my problem...
Sticking with the Boolean I just added changed the statement to true or false depending on the yes or no it appears to be working =]

Code:
Private Sub SaveBtn_Click()
Dim Response As Integer
Response = MsgBox(Prompt:="Are you sure you want to Save this record?", Buttons:=vbYesNo)
If Response = vbNo Then
buttonclicked = False
Exit Sub
Else
DoCmd.Save
buttonclicked = True
End If
End Sub

Thank you all for your help.
 

Users who are viewing this thread

Top Bottom