Seithan .The Unborn
Seithan .The Unborn

Reputation: 1

VBA Access Message Box woes

I followed a few simple comments on how to pop a confirmation box before executing a script, but sadly, if I press yes, the script doesn't run.

Private Sub Overwrite_Btn_Click()
    If MsgBox("Yes?", vbOKCancel) = ok Then
        Me.Product_Quantity = Me.Quantity_Input
    Else
        Exit Sub
    End If
End Sub

I'm trying to set Product_Quantity equaling Quantity_Input, and although it works without the MsgBox command, it doesn't with it.

What am I doing wrong?

Upvotes: 0

Views: 1248

Answers (3)

ashleedawg
ashleedawg

Reputation: 21619

Single line:

If MsgBox("Yes?", vbOKCancel) <> vbOk then Exit Sub

'continue code here.

More Information:

Upvotes: 1

LFB
LFB

Reputation: 686

Instead of If MsgBox("Yes?", vbOKCancel) = ok Then try: If MsgBox("Yes?", vbOKCancel) = vbOK Then

Typically the interactions with forms will return one constant from a set of several constants. Those are catalogged in enums. In this case you have several constants in the VbMsgBoxResult class, and vbOK is a constant with value 1, which is returned from clicking the ok button.

Actually, If MsgBox("Yes?", vbOKCancel) = 1 Then would work as well, but it is harder to remember that clicking Ok returns 1 then simply stating a constant named vbOK

In object explorer (F2 on the VBE), searching for VbMsgBoxResult will give all possible results that comes from interacting with a message box.

Upvotes: 2

Doug Coats
Doug Coats

Reputation: 7107

https://www.techonthenet.com/access/constants/msgbox_ret.php

1) Dim a variable as integer.

2) Check for value of integer equal to 6, or check for vbYess

3) ?????

4) Profit

borrowed from link

 Dim LResponse As Integer

 LResponse = MsgBox("Do you wish to continue?", vbYesNo, "Continue")

 If LResponse = vbYes Then
    {...statements...}
 Else
    {...statements...}
 End If

Upvotes: 1

Related Questions