VBA_oldie
VBA_oldie

Reputation: 11

MS Access rejects arguments, always asking for "="

I have used VBA to program MS Access for many years but never before with Office 2010. Whenever I try to use any arguments I get the message "Expected: =" after I close the brackets.

For example, a simple messagebox accepts the caption but will only produce the default argument (vbOKCancel). In some circumstances I would prefer to use vbYesNo but if I enter MsgBox("Do you wish to cancel",vbYesNo) then although the various alternative responses drop down for selection, I get "Expected: =" on trying to complete the statement. This happens whenever I try to use arguments - for example if I use DoCmd in the OnCurrent event of the Form object to display a particular record.

Example code:

'If closed without a preference being selected, continue:
    If IsNull(cboPreference) Then
        MsgBox ("You have not selected a preference.  Do you want to cancel?",vbYesNo)
        Select Case Response
            Case vbYes
               Exit Sub
            Case vbNo
                cboPreference.SetFocus
        End Select
    End If

I have tried uninstalling and reinstalling the application; but the error occurs both in Windows 7 on my PC and Windows 8 on my laptop (both 64-bit).

I have also installed the latest updates for MS Access 2010. What else can I do?

Upvotes: 1

Views: 63

Answers (1)

HansUp
HansUp

Reputation: 97101

Test these two statements in the Immediate window to see the effect of the parentheses.

MsgBox("Do you wish to cancel",vbYesNo) ' complains about expected =
MsgBox "Do you wish to cancel",vbYesNo  ' this works

If you want to capture the value returned from MsgBox, assign it to a variable ... and you need an equal sign and the parentheses in that situation.

Response = MsgBox("You have not selected a preference.  Do you want to cancel?",vbYesNo)

More about when to use parentheses with a function call, and when not to use them ...

MsgBox "Hi"
Call MsgBox("Hi")
intResponse = MsgBox("Hi")

Upvotes: 1

Related Questions