Solved

How do I get a Access VBA Yes/No message box to either apply/don't apply

Posted on 2014-02-12
8
10,694 Views
Last Modified: 2014-02-12
I have a checkbox which I would like to have a  "Are you sure you want to make this change?" prompt with a Yes and No button. If they clicked yes, obviously the change would apply. If they click no, I would like the the dialog box to exit without any change to the check box.

The VBA code I have tied to the BeforeUpdate field of this check box is:

Private Sub Inactive_BeforeUpdate(Cancel As Integer)

Dim Response As Integer
Response = MsgBox("Do you want to change the status of this client?", vbYesNo + vbQuestion, "Client Prompt")
If Response = vbYes Then
MsgBox "The user status has been changed", vbOKOnly, "Confirmation..."
Else
MsgBox "The user status has NOT been changed"
End If

End Sub


I'm no programmer but have tried various code formats based on web research but none are fixing my problem.
0
Comment
Question by:OneByte
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 39853890
Hi,

pls try

Private Sub Inactive_BeforeUpdate(Cancel As Integer)

Dim Response As Integer
Response = MsgBox("Do you want to change the status of this client?", vbYesNo + vbQuestion, "Client Prompt")
If Response = vbYes Then
MsgBox "The user status has been changed", vbOKOnly, "Confirmation..."
Else
Cancel = True
MsgBox "The user status has NOT been changed"
End If

End Sub

Open in new window

Regards
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 39853903
Private Sub Inactive_BeforeUpdate(Cancel As Integer)

Dim Response As Integer
Response = MsgBox("Do you want to change the status of this client?", vbYesNo + vbQuestion, "Client Prompt")
If Response = vbYes Then
MsgBox "The user status has been changed", vbOKOnly, "Confirmation..."
Else
MsgBox "The user status has NOT been changed"

'ADD this line
Cancel = True

End If

End Sub

if you don't want the message when No is selected, just remove the line

MsgBox "The user status has NOT been changed"
0
 
LVL 3

Expert Comment

by:0Doc
ID: 39853917
You almost have it.  The thing you have to do is reset the checkbox value if they choose not to change it (because they already clicked it to change it), so you have to set it back.  Try this:

Dim Response As Integer, CheckboxValue
Response = MsgBox("Do you want to change the status of this client?", vbYesNo + vbQuestion, "Client Prompt")
If Response = vbYes Then
  MsgBox "The user status has been changed", vbOKOnly, "Confirmation..."
Else
  If Checkbox.Value = True Then
    Checkbox.Value = False
  Else
    Checkbox.Value = True
  End If
  MsgBox "The user status has NOT been changed"
End If
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Closing Comment

by:OneByte
ID: 39853976
Perfect! That did it Rgonzo1971 and Rey Obrero.

0Doc I received a debug error for line: If Checkbox.Value = True Then

Thank you all for your help and time.
0
 
LVL 3

Expert Comment

by:0Doc
ID: 39854362
Just an FYI since you have it solved.  The debug error is probably because of the name of the checkbox.  I called mine Checkbox.  If you have a different name for your checkbox, you'll need to put that name into the code in the place of "Checkbox"
0
 

Author Comment

by:OneByte
ID: 39854481
0Doc I tried just for kicks and changed Checkbox to Inactive with the code you provided but received "Inactive.Value = False"
Inactive is the name of my check box.

Sorry guys the suggested syntax did fix my functionality with the vbYesNo but  now when I try to close the form, window, or click another button on the form to open another form I get the dialog prompt that set on the check box. The code is only set to the BeforeUpdate area of the check box.
Please advise.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39854496
Private Sub Inactive_BeforeUpdate(Cancel As Integer)

Dim Response As Integer
Response = MsgBox("Do you want to change the status of this client?", vbYesNo + vbQuestion, "Client Prompt")
If Response = vbYes Then
MsgBox "The user status has been changed", vbOKOnly, "Confirmation..."
Else
MsgBox "The user status has NOT been changed"

'ADD this line
Cancel = True
me.Inactive.undo
exit sub


End If

End Sub
0
 

Author Comment

by:OneByte
ID: 39854591
Corrected my problem. Thanks Rey!!
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access 2016 7 35
Install both Office 2010 and 2013 4 37
Update a text value in another table 10 40
Microsoft Office Licensing Question 2 29
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question