Results 1 to 11 of 11
  1. #1
    uoghk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    149

    ByRef argument type mismatch

    I use "ThisRecord" as a parameter to tell a sub "UpdateRecords" how to update a table.


    The code likes below. But I get error "ByRef argument type mismatch" on "ThisRecord".
    Code:
    Dim ThisRecord As String
    
    
    Private Sub Form_Open(Cancel As Integer)
    ThisRecord="A"
    .....
    End Sub
    
    
    Private Sub Button_Click()
    Call UpdateRecords(ThisRecord,...)
    End Sub
    
    
    Private Sub UpdateRecords(TxType As String, ...)
    ...
    End Sub


    However, I add a temp variable str in Button_Click as below, then it works.
    Code:
    Private Sub Button_Click()
    Dim str as String
    str=ThisRecord
    Call UpdateRecords(str,...)
    End Sub

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    Perhaps ThisRecord should be declared with Public rather than Dim
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    uoghk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    149
    I also have other variables at the top of the program.
    These variables work well in different sub within the program.

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    What's the name of the button control? You have: Private Sub Button_Click()
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    uoghk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    149
    the button name is btnSave

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    Quote Originally Posted by uoghk View Post
    the button name is btnSave
    So perhaps you need: Private Sub btnSave_Click()
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Are you changing the value of "ThisRecord"?
    The default is ByVal and you dont show ByRef in your arguement.

    Private Sub UpdateRecords(ByRef TxType As String, ...)
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    think that is the other way round

    ByRef is the default and references the passed variable and can modify it within the sub or function (can be useful if you want to modify a number of values in one pass)

    Code:
    Sub setStr(str As String)
        str = "abc"
    End Sub
    
    
    Function testsetStr()
    Dim s As String
        setStr s
        Debug.Print s
    End Function
    and in the immediate window
    Code:
    ?testsetStr()
    abc
    ByVal takes the value and although it can be modified within the sub/function, the modified value is not passed back as it is with ByRef

    OP has provided air code so my guess is in the real code, ThisRecord gets modified somewhere between the form opening and the button being clicked

    Edit:
    Interestingly the OP uses Call, which I haven't used for over 20 years (it's a throwback to the early days of VB and only kept in for backwards compatibility), so thought I'd check out the variations

    So
    setStr s produces the result as above
    Call setstr(s) produces the same result

    but

    Call setStr s does not - it would appear that by leaving out the brackets ByRef's are treated as ByVals so s remains as its original value when passed as a parameter.

    Pretty sure somewhere this will be documented, but I don't have time to go looking

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    think that is the other way round
    I stand corrected. I went to check before I posted and I saw ByVal as the default but I think it may have been in .Net
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  10. #10
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    I think it may have been in .Net
    it is

  11. #11
    uoghk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    149
    Thanks Ajax.
    I ONLY set a value to ThisRecord on Form_Open.

    I add ByVal in sub then it works.
    Code:
    Private Sub UpdateRecords(ByVal TxType As String, ...)
    ...
    End Sub
    I am a newbie. I use Access for just 1 month.
    I will remove using the CALL.

    Thanks all of you.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 04-05-2018, 11:50 AM
  2. Referencing an Accde and getting ByRef Mismatch
    By Perceptus in forum Programming
    Replies: 5
    Last Post: 10-01-2015, 01:49 PM
  3. Another ByRef type mismatch
    By Levonas in forum Programming
    Replies: 3
    Last Post: 03-05-2015, 03:06 PM
  4. Compile Error: ByRef argument type mismatch
    By gaker10 in forum Programming
    Replies: 3
    Last Post: 11-17-2014, 10:33 AM
  5. byref argument type mismatch error
    By karuppasamy in forum Access
    Replies: 1
    Last Post: 06-22-2011, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums