Consulting

Results 1 to 7 of 7

Thread: Solved: Passing an array to a sub

  1. #1
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location

    Solved: Passing an array to a sub

    Hi guys,

    I'm trying to pass a 13x2 array to a subroutine, but I keep getting a "ByRef Argument Type Mismatch". I'm not sure what to do about it...

    Here's the code that is receiving the array:

    [vba]Sub Invoice_InsertDetail(lInvoiceID As Long, aryInvDetail() As Variant)
    'Date Created : February 16,2006 11:31
    'Author : Ken Puls
    'Macro Purpose: Place an array of invoice detail data into the database

    Dim cnt As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim colHead As String
    Dim lDeptID As Long
    Dim indx As Long
    Dim sSQL As String

    'Concatenate a string with the names of the column headings
    colHead = "(ExpTypeID,Amount,DeptID,InvID)"
    lDeptID = Application.WorksheetFunction.Index(_
    ActiveWorkbook.Worksheets("Tables").Range("tblDepts"), _
    ActiveSheet.Range("VBA_Dept"))

    'Open connection to the database
    cnt.Open sConnect

    'Build the SQL statement
    For indx = 0 To 13
    sSQL = "INSERT INTO tblInvDetail " & colHead & " VALUES (" & _
    GetExpTypeID(aryInvDetail(indx, 0)) & "," & _
    aryInvDetail(indx, 1) & "," & lDeptID & "," & lInvoiceID & ");"
    rst.Open sSQL, cnt
    Next indx

    'Close the ADO objects
    cnt.Close
    Set rst = Nothing
    Set cnt = Nothing
    End Sub[/vba]

    Highlighted code is this part: aryInvDetail(indx, 0)

    I've tried:

    [vba]Sub Invoice_InsertDetail(lInvoiceID As Long, ByRef aryInvDetail() As Variant)
    Sub Invoice_InsertDetail(lInvoiceID As Long, ByRef aryInvDetail As Variant)
    Sub Invoice_InsertDetail(lInvoiceID As Long, aryInvDetail As Variant)[/vba]

    Can anyone help out on this? It's driving me crazy!

    Thanks!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  2. #2
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    I pass multidimensional arrays around all the time and do not remember any problems. Most of my procs avoid declaring array type as they are normally numeric arrays and could be integer, long, single or double. It is not a very good solution from the standpoint of "good code", but try eliminating everything except the actual array names.

    Serves you right for using zero based arrays. Who ever heard of the zeroth item in a list or matrix. Bah! Humbug!
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  3. #3
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Ken,

    It seems to me if you're getting the error at aryInvDetail(indx, 0) then it may have something to do with the function you're sending it to
    GetExpTypeID(aryInvDetail(indx, 0))

    Do you by chance have the argument for that function to be a ByVal? Array items have to be ByRef, if the function is expecting ByVal, you'll error out.

    Matt

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hey guys,

    Mark, why not use a 0 based array? They come standard out of the box that way, and I figured that I'd just work with what MS gave me. Save the Option Base 1. Any advantage to going that route?

    Matt, I think you nailed it. I can get it to work by assigning the array element to a temporary string, although it seems kind of dumb to have to do it:

    [vba] 'Build the SQL statement
    For indx = 0 To 13
    sTemp = aryInvDetail(indx, 0)
    sSQL = "INSERT INTO tblInvDetail " & colHead & " VALUES (" & _
    GetExpTypeID(sTemp) & "," & _
    aryInvDetail(indx, 1) & "," & lDeptID & "," & lInvoiceID & ");"
    rst.Open sSQL, cnt
    Next indx[/vba]

    Changing the function to ByRef didn't help at all.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Another thought could be that you're sending a variant array item to a string variable in the function.. maybe you could use CStr(aryInvDetail(indx, 0)) ?

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Pass it as a simple variant?

    [vba]
    Sub Invoice_InsertDetail(lInvoiceID As Long, aryInvDetail)
    [/vba]

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    BINGO!

    That's it, Matt.

    I would have thought that the element would pass as a string on it's own, but I guess not.

    Thanks!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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