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,094 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 119

Assisted Solution

by:Rey Obrero
Rey Obrero 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
 

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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 119

Expert Comment

by:Rey Obrero
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
My experience with Windows 10 over a one year period and suggestions for smooth operation
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

910 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now