?
Solved

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

Posted on 2014-02-12
8
Medium Priority
?
13,441 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 1000 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 1000 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
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 …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

752 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