none
variable not defined question

    Question

  • Hello.  Have several questions regarding the above compile error notice.  Both of these are in the same button click event.

    1)  I am getting the above notice on 'Response' indicating the msgbox when I use it the following way.  So I did a Dim Response as Variant.  Would that be correct?

    Response = MsgBox(leaving msg out to shorten)

    if records_affected <> 0 then
      If Response = vbCancel Then
        'i have other code here
      End If
    End If

    2)  I am getting the above notice on 'Cancel' when I use it the following way.  I did a Dim Cancel As Integer.  Would this be correct?

    Cancel = True

    Thanks...John

     

    Thursday, July 28, 2011 5:50 PM

Answers

  • johnboy0276 wrote:
    > ...
    > 1)  I am getting the above notice on 'Response' indicating the msgbox
    > when I use it the following way.  So I did a Dim Response as Variant.
    > Would that be correct?
    >
    > Response = MsgBox(leaving msg out to shorten)
    >
    > if records_affected <> 0 then
    >  If Response = vbCancel Then
    >  ...
     
    You can use Variant for everything but it's a bit overkill here.
    If in the VBE you set the cursor into the term "MsgBox" and press <F1>
    the first paragraph of the help text should tell you that the return value
    of the MsgBox function is of type Integer.
     
    > 2)  I am getting the above notice on 'Cancel' when I use it the
    > following way.  I did a Dim Cancel As Integer.  Would this be
    > correct?
    >
    > Cancel = True
     
    Well, built in cancel parameters of events are Integers but...
     
    If you get the error and have to declare the variable you are probably
    using it in a place or way that's not appropriate.
     
    If there is a built in cancel parameter then you shouldn't get the error.
    If there is no built in cancel parameter then there is no use to create one
    by your own.
     
    If you use the variable in another sense then you should not call it
    "Cancel" like the predefined parameters and maybe you could
    use the type Boolean.
     
    So you would have to tell more about the circumstances to get
    a definite answer to question #2.
     
    --
    cu
    Karl
    *********
    Access-FAQ (German/Italian): http://www.donkarl.com
     
     
     
    • Proposed as answer by Macy Dong Wednesday, August 03, 2011 8:46 AM
    • Marked as answer by Macy Dong Friday, August 05, 2011 4:55 AM
    Thursday, July 28, 2011 6:10 PM
  • The following has always worked for me:

    Dim Response As Variant
    Response = MsgBox("Message Here", 49,"Title Here")
    If Response = 2 Then (if Response is False)
        DoCmd.CancelEvent
        Exit Sub
    End If
    If Response = 1 And records_affected <>0 Then
        [Your code here] (if Response is True)
    End If

    • Proposed as answer by Lawrence Ellefson Thursday, July 28, 2011 6:22 PM
    • Marked as answer by Macy Dong Friday, August 05, 2011 4:55 AM
    Thursday, July 28, 2011 6:22 PM
  • Cancel is a parameter passed to some event procedures... if this is the case you do not want to dim cancel as a new variable

    you will get variable not defined as a compiler error if you have "option explicit" at the top of your code - this forces variables to be dimmed prior to use, and is a good practice to be in


    For the benefit of others, please mark posts as answered or helpful when they answer or assist you in finding the answer. ...Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. - Mark Twain
    • Proposed as answer by Macy Dong Wednesday, August 03, 2011 8:47 AM
    • Marked as answer by Macy Dong Friday, August 05, 2011 4:55 AM
    Thursday, July 28, 2011 11:45 PM

All replies

  • johnboy0276 wrote:
    > ...
    > 1)  I am getting the above notice on 'Response' indicating the msgbox
    > when I use it the following way.  So I did a Dim Response as Variant.
    > Would that be correct?
    >
    > Response = MsgBox(leaving msg out to shorten)
    >
    > if records_affected <> 0 then
    >  If Response = vbCancel Then
    >  ...
     
    You can use Variant for everything but it's a bit overkill here.
    If in the VBE you set the cursor into the term "MsgBox" and press <F1>
    the first paragraph of the help text should tell you that the return value
    of the MsgBox function is of type Integer.
     
    > 2)  I am getting the above notice on 'Cancel' when I use it the
    > following way.  I did a Dim Cancel As Integer.  Would this be
    > correct?
    >
    > Cancel = True
     
    Well, built in cancel parameters of events are Integers but...
     
    If you get the error and have to declare the variable you are probably
    using it in a place or way that's not appropriate.
     
    If there is a built in cancel parameter then you shouldn't get the error.
    If there is no built in cancel parameter then there is no use to create one
    by your own.
     
    If you use the variable in another sense then you should not call it
    "Cancel" like the predefined parameters and maybe you could
    use the type Boolean.
     
    So you would have to tell more about the circumstances to get
    a definite answer to question #2.
     
    --
    cu
    Karl
    *********
    Access-FAQ (German/Italian): http://www.donkarl.com
     
     
     
    • Proposed as answer by Macy Dong Wednesday, August 03, 2011 8:46 AM
    • Marked as answer by Macy Dong Friday, August 05, 2011 4:55 AM
    Thursday, July 28, 2011 6:10 PM
  • The following has always worked for me:

    Dim Response As Variant
    Response = MsgBox("Message Here", 49,"Title Here")
    If Response = 2 Then (if Response is False)
        DoCmd.CancelEvent
        Exit Sub
    End If
    If Response = 1 And records_affected <>0 Then
        [Your code here] (if Response is True)
    End If

    • Proposed as answer by Lawrence Ellefson Thursday, July 28, 2011 6:22 PM
    • Marked as answer by Macy Dong Friday, August 05, 2011 4:55 AM
    Thursday, July 28, 2011 6:22 PM
  • Cancel is a parameter passed to some event procedures... if this is the case you do not want to dim cancel as a new variable

    you will get variable not defined as a compiler error if you have "option explicit" at the top of your code - this forces variables to be dimmed prior to use, and is a good practice to be in


    For the benefit of others, please mark posts as answered or helpful when they answer or assist you in finding the answer. ...Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. - Mark Twain
    • Proposed as answer by Macy Dong Wednesday, August 03, 2011 8:47 AM
    • Marked as answer by Macy Dong Friday, August 05, 2011 4:55 AM
    Thursday, July 28, 2011 11:45 PM