Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Posts
    15

    Angry Unanswered: ByRef error confusion - VBA

    I am self taught in Access and VBA so maybe I missed something that is obvious to someone else. I am completely stumped. Here is the situation:

    I have a custom function with one argument, an integer.....

    Public Function test1(a As Integer) As Integer
    test1 = a + 1
    End Function



    From a different module, I try to call the function......

    Private Sub Command15_Click()
    Dim t1, t2 As Integer
    t1 = 3
    t2 = test1(t1)
    Me!Text1 = t1
    Me!Text2 = t2
    End Sub


    When I compile the code, I get "Compile error: ByRef argument type mismatch" and it highlights the "t1" in the line "t2=test1(t1)". I do not understand why I am getting this error. I am passing an integer and the argument is declared as an integer in my function "test1". I ALSO WANT TO KNOW WHY THE FOLLOWING CHANGES ELIMINATE THE ERROR!?!?!?!?!?:

    If I force evaluation of t1 by putting an extra set of parenthesis around it, there is no error....

    t2=test1((t1)) '---> NO ERROR


    Also, if I declare t1 separately there is no error....

    Dim t1 As Integer
    Dim t2 As Integer '-----> NO ERROR

    IN THE END I WAS ABLE TO COMPILE THE CODE BY DECLARING t1 AND t2 SEPARATELY (as above) BUT IT IS DRIVING ME CRAZY BECAUSE I DO NOT UNDERSTAND WHAT THE DIFFERENCE IS BETWEEN

    "Dim t1, t2 As Integer"
    AND
    "Dim t1 As Integer
    Dim t2 As Integer"


    OR WHY THIS ELIMINATED THE ByRef ERROR I WAS GETTING!! SOMEONE PLEASE EXPLAIN BEFORE I THROW MY PC THROUGH A WINDOW!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  2. #2
    Join Date
    Mar 2004
    Posts
    15

    Talking Nevermind

    Apparently "Dim t1, t2 as Integer" declares only t2 as an integer and leaves t1 Variant. I think declaring variables like this (a list followed by a type for all in the list) worked in Pascal which is probably where I picked it up. In any case, confusion alleviated so nevermind. I needed to code "Dim t1 as Integer, t2 as Integer"

  3. #3
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713

    Re: ByRef error confusion - VBA

    Originally posted by michael515
    I am self taught in Access and VBA so maybe I missed something that is obvious to someone else. I am completely stumped. Here is the situation:

    I have a custom function with one argument, an integer.....

    Public Function test1(a As Integer) As Integer
    test1 = a + 1
    End Function



    From a different module, I try to call the function......

    Private Sub Command15_Click()
    Dim t1, t2 As Integer
    t1 = 3
    t2 = test1(t1)
    Me!Text1 = t1
    Me!Text2 = t2
    End Sub


    When I compile the code, I get "Compile error: ByRef argument type mismatch" and it highlights the "t1" in the line "t2=test1(t1)". I do not understand why I am getting this error. I am passing an integer and the argument is declared as an integer in my function "test1". I ALSO WANT TO KNOW WHY THE FOLLOWING CHANGES ELIMINATE THE ERROR!?!?!?!?!?:

    If I force evaluation of t1 by putting an extra set of parenthesis around it, there is no error....

    t2=test1((t1)) '---> NO ERROR


    Also, if I declare t1 separately there is no error....

    Dim t1 As Integer
    Dim t2 As Integer '-----> NO ERROR

    IN THE END I WAS ABLE TO COMPILE THE CODE BY DECLARING t1 AND t2 SEPARATELY (as above) BUT IT IS DRIVING ME CRAZY BECAUSE I DO NOT UNDERSTAND WHAT THE DIFFERENCE IS BETWEEN

    "Dim t1, t2 As Integer"
    AND
    "Dim t1 As Integer
    Dim t2 As Integer"


    OR WHY THIS ELIMINATED THE ByRef ERROR I WAS GETTING!! SOMEONE PLEASE EXPLAIN BEFORE I THROW MY PC THROUGH A WINDOW!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Look at your dim statement CAREFULLY. Types are not inherited so, t1 is of type VARIANT ... There is your type mismatch.

  4. #4
    Join Date
    Sep 2003
    Location
    T.O.
    Posts
    326
    For those of us who may need further clarification (I had to read the code twice and M Owen's note thrice), michael515's Dim statement:

    Dim t1, t2 as integer is not the same as
    Dim t1 as integer
    Dim t2 as integer

    it is equivalent to:
    Dim t1 as variant (the default undeclared type)
    Dim t2 as integer

    The variant is then passed to a function expecting an integer.

    a different Dim statement that would have worked is:
    Dim t1 as integer, t2 as integer.

    M Owen: Don't mean to supplant your answer, if you can forgive me, please send $1 to Sorry Dude @ 743 Evergreen Terrace ...
    All code ADO/ADOX unless otherwise specified.
    Mike.

  5. #5
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Originally posted by HomerBoo
    For those of us who may need further clarification (I had to read the code twice and M Owen's note thrice), michael515's Dim statement:

    Dim t1, t2 as integer is not the same as
    Dim t1 as integer
    Dim t2 as integer

    it is equivalent to:
    Dim t1 as variant (the default undeclared type)
    Dim t2 as integer

    The variant is then passed to a function expecting an integer.

    a different Dim statement that would have worked is:
    Dim t1 as integer, t2 as integer.
    Homer,

    This bit me in the butt a few times when I was just switching over from C to VB (and subsequently VBA) ... It does get to be a pain sometimes when you have to explicitly type each and every variable (especially those passed as parameters) ...

    - Mike

Posting Permissions

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