Solved

Passing an Array Dimension to Sub

Posted on 2013-11-06
4
294 Views
Last Modified: 2013-11-07
I want to pass an array dimension via a parameter.  I want a single sub to work with calls in which the array dimension will change.  I get a "constant expression required" error when I try to call this:
rivate Sub SingleRcrd(ByRef iArrayBound As Integer, ByVal ThisSql As String)
    'On Error GoTo ErrorHandler
    
    Dim rs As ADODB.Recordset
    Dim i As Integer
    Dim UploadArr(iArrayBound)
    
    Set rs = New ADODB.Recordset
    rs.Open ThisSql, conn, 1, 2
    For i = 0 To iArrayBound
        UploadArr(i) = rs(i).Value
    Next i
    rs.Close
    Set rs = Nothing
    
    Set rs = New ADODB.Recordset
    rs.Open ThisSql, srvr_conn, 1, 2
    For i = 0 To iArrayBound
        rs(i).Value = UploadArr(i)
    Next i
    rs.Update
    rs.Close
    Set rs = Nothing
    
    Exit Sub
ErrorHandler:
    Beep
    ErrorHandler
End Sub

Open in new window


with this:
    sql = "SELECT EventName, EventDate, Club, Website, Weather, Comments, FeeIncrDate, TimingMethod, EventDirID, ShowOnline, "
    sql = sql & "OnlineReg, WhenShutdown, FeeIncrDate FROM Events WHERE EventID = " & lEventID
    Call SingleRcrd(12, sql)

Open in new window


What am I doing wrong?
0
Comment
Question by:Bob Schneider
4 Comments
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 167 total points
ID: 39629102
I suspect it is because you are passing the parameter byref and then using it to try and define an array which I suspect is where the error is occurring. Can't see why you need to pass byref so do you get the same error if you pass the parameter byval.
0
 
LVL 10

Accepted Solution

by:
Banthor earned 167 total points
ID: 39629125
use REDIM instead of DIM for the array, this tells the compiler that the dimension can be altered.

Another option is to use an xml object
0
 
LVL 5

Assisted Solution

by:advfinance
advfinance earned 166 total points
ID: 39629780
Like Banthor says, use Dim, then ReDim. Change line 6 from

Dim UploadArr(iArrayBound)

Open in new window


to

Dim UploadArr()
ReDim UploadArr(iArrayBound)

Open in new window


--
Chris
0
 

Author Closing Comment

by:Bob Schneider
ID: 39629797
Thanks a bunch!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question