What's wrong with my code?

This code runs when user double-clicks a list box.  The argument field is hidden on the form and is equal to a number that corresponds with the selection in the list box.  I need it to either open a query, open a form, open a report, or run a macro based on the Argument number.  I am getting an "Argument not Optional" error.

Be gentle with me Smile - I'm a novice!

Thanks!

Dim MenuItem As String
Dim Agmt As Integer

MenuItem = Me!RPTLST
Agmt = Forms!frmMENU_3_USERS!Argument

'Runs when RPTLST is double-clicked
'Runs the command to which the menu item is tied
   
    If Agmt = 1 Then
            DoCmd.OpenQuery , MenuItem
    ElseIf Agmt = 2 Then
            DoCmd.OpenReport , MenuItem
    ElseIf Agmt = 3 Then
            DoCmd.OpenForm , MenuItem
    ElseIf Agmt = 4 Then
            DoCmd.RunMacro , MenuItem
    Else
        MsgBox "Unknown option."
        Exit Sub
End If



Answer this question

What's wrong with my code?

  • Tùng

    What's wrong with my code

    1. It's Not VB.Net
    2. It's not even VB, it's VBA running under MS Access and acts like Macro code.
    3. "Argument not Optional" error is telling me that your sending an empty value as parameter. Be sure that MenuItem is not empty. Technically it shouldn't be when double clicking a list item, but that's what i'm seeing. Try adding debug.print MenuItem in your code to see if it has any value, or simply toggle that line as a breakpoint and check it against your selection.

    Toggling a break point is as easy as clicking the gray area to the left of the line of code. In most cases the line turns maroon in color and when code excutions reaches the line it pauses (Breaks) you can then hover over the variable expression and it's value will be displayed. BTW, I love the way this works under VB 8.

    One more thing before the moderators lock this thing. In naming conventions try not to use words attributed to code that mean something. Like argument as it pertains to an argument list passed to a procedure, etc. If you must append it with m_argument, MyArgument, or iArgument which ever makes the most sense to you and anyone who follows up on your code.

    Randy

  • linch12

    Thank you for your reply.  I did finally figure it out - it was as simple as removing the comma before MenuItem.

    I have to settle for what I'm working with since it is company software. 

    Your debug and naming suggestions will be helpful also.  It never occurred to me to distinguish "my"argument from VBs.  Thank you.

  • What's wrong with my code?