Solved

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

Posted on 2014-02-12
8
9,567 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 48

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

747 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

10 Experts available now in Live!

Get 1:1 Help Now