Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Lounger
    Join Date
    Jun 2015
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Unhappy Operand without operator error message

    I'm trying to have a MsgBox open up with a text input in to enter a date. I took the code directly from Microsoft and I get the operand without operator error message. Below is my code. Can someone please help?

    Code:
    Dim StartDate As Date    ' Declare variables.
    Dim Msg
    StartDate = InputBox("Enter a date")
    Msg = "Yield " & DateDiff("m", StartDate, Now()) 
    MsgBox Msg
    Thank you to whoever can help!

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts
    For one thing, you haven't defined the type of Msg so it defaults to the type variant but you really want it to be a text value so you should say Msg As String. In addition, it may well be that Msg is a reserved word in VBA. I would give it a different name, such as UserPrompt.

    But the issue with using the MsgBox function is that you cannot enter data into a MsgBox. What would do the trick is the InputBox() function - see InputBox() as one of many references for the details on how it is used. If you want a more polished solution than the InputBox() function can give you, then you can use a regular form in Access, but in the PopUp and Modal format to do error checking and other useful things to the data entered.
    Wendell

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Enginerd,

    It works on my Access 2010 with the minor changes shown:
    AccessTestMsg.JPG

    Note: I entered 5/1/2015 in the inputbox.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    New Lounger
    Join Date
    Jun 2015
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts
    HTH,


    I tried that and it kicks back with the "Dim" at StartDate as the operand without operator. (If that makes sense) I have a feeling it will also do that with the Msg as String. I am using Access 2013, so I'm not sure if that has an effect?

  5. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Enginerd,

    FYI: HTH = Hope This Helps!

    Could you please post the entire procedure so we can see if there is something else in play here?
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  6. #6
    3 Star Lounger
    Join Date
    Apr 2002
    Location
    Redditch, Worcestershire, England
    Posts
    233
    Thanks
    0
    Thanked 22 Times in 21 Posts
    Quote Originally Posted by enginerd View Post
    I'm trying to have a MsgBox open up with a text input in to enter a date. I took the code directly from Microsoft and I get the operand without operator error message. Below is my code. Can someone please help?

    Code:
    Dim StartDate As Date    ' Declare variables.
    Dim Msg
    StartDate = InputBox("Enter a date")
    Msg = "Yield " & DateDiff("m", StartDate, Now()) 
    MsgBox Msg
    Thank you to whoever can help!
    that code runs for me without any amendments in Excel 2010.

    Have you definitely lifted the text exactly as it appears in your program? 'Operand without operator' suggests you may have a stray character like ' or , somewhere in your code!

  7. #7
    New Lounger
    Join Date
    Jun 2015
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I am positive. that is identical to what I am trying to run.

  8. #8
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Enginerd,

    I just tried running your exact code on Access 2016 and it works perfectly.
    There MUST be something that you have differently or Access 2013 has a bug!

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  9. #9
    New Lounger
    Join Date
    Jun 2015
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts
    does it make a huge difference if i try to have this code run on a query vs a form?

  10. #10
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Enginerd,

    As I said before we need to see the whole context otherwise we're shooting in the dark with a code snipit that seems to work just fine.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  11. #11
    New Lounger
    Join Date
    Jun 2015
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Capture.PNG

    These are my fields along with what I want to show up in my query. I am trying to write this code from completion date.

  12. #12
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Enginerd,

    So if I read this correctly you are trying to get the number of months from Start Date to Completion Date or maybe the current date? If this is so you should add a calculated field to your query to show this information. HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  13. #13
    New Lounger
    Join Date
    Jun 2015
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Kind of. I'm trying to allow for an input so for example i can input 5/1/2015 and current date and it will return all fields within that range. Trouble is i need the 5/1/2015 field to be changeable.

  14. #14
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Enginerd,

    Ok so you want the range of dates between an inputted data and today. So you want all records that have a completion date in that range?
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  15. #15
    New Lounger
    Join Date
    Jun 2015
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Yes. Exactly. If that is possible

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •