Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / February 2005

Tip: Looking for answers? Try searching our database.

Array Problems

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MikeC - 14 Feb 2005 18:43 GMT
I'm attempting to use a dynamic array and I'm having two problems:

1)   I can't seem to remember how to append a new row to the array.
Consequently, I seem to be populating only one row at a time as I loop
through my ADODB recordset.

2)  A "Subscript out of range" error occurs when I attempt to reference an
array element by row column.  However, I *can* successfully reference the
array by column 0 or 1.  Perhaps, the solution to problem #1 will also solve
problem #2.

The below code is running in Access 2002 SP3.  The O/S is Windows XP SP2.

Can anyone tell me what I'm doing wrong and how to fix it?

Below is the relevant code fragment:
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   Dim varPmtDetail() As Variant

   With rst1

       'Set cursor location to client so that provider will support
RecordCount property.
       .CursorLocation = adUseClient

       'Open tblPaymentDetail table to read detail records.
       .Open strSQL, cnn1, adOpenForwardOnly, adLockReadOnly, adCmdText

       .MoveFirst
       For i = 0 To .RecordCount - 1
           'Loop through recordset and append(?) each pair of values to the
dynamic array.
           varPmtDetail = Array(!PmtDetailID, !PartialPaymentAmt)

           curTotalPmt = curTotalPmt + !PartialPaymentAmt

           'Subscript out of range error occurs on next line.
           Debug.Print varPmtDetail(i, 0)
           Debug.Print varPmtDetail(i, 1)

           'The below debug lines work fine if I comment the above 2 debug
statements.
           Debug.Print varPmtDetail(0)
           Debug.Print varPmtDetail(1)
           .MoveNext
       Next i
   End With
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Brendan Reynolds - 14 Feb 2005 20:53 GMT
Here is how you would add a row at a time to the array (I've used the
Employees table in Northwind, so that I could test the result before
posting) ...

Public Sub TestArray1()

   Dim varTest()
   Dim rst As ADODB.Recordset
   Dim lngRows As Long

   Set rst = New ADODB.Recordset
   With rst
       .ActiveConnection = CurrentProject.Connection
       .Source = "SELECT FirstName, LastName FROM Employees"
       .Open
       Do Until .EOF
           ReDim Preserve varTest(1, lngRows + 1)
           varTest(0, lngRows) = .Fields("FirstName")
           varTest(1, lngRows) = .Fields("LastName")
           lngRows = lngRows + 1
           .MoveNext
       Loop
       .Close
   End With
   For lngRows = 0 To UBound(varTest, 2)
       Debug.Print varTest(0, lngRows); " "; varTest(1, lngRows)
   Next lngRows

End Sub

However, once you have the recordcount, you can dimension the array once,
and avoid the need to use Preserve, which will be much more efficient ...

Public Sub TestArray2()

   Dim varTest()
   Dim rst As ADODB.Recordset
   Dim lngRows As Long

   Set rst = New ADODB.Recordset
   With rst
       .ActiveConnection = CurrentProject.Connection
       .CursorLocation = adUseClient
       .Source = "SELECT FirstName, LastName FROM Employees"
       .Open
       ReDim varTest(1, .RecordCount - 1)
       Do Until .EOF
           varTest(0, lngRows) = .Fields("FirstName")
           varTest(1, lngRows) = .Fields("LastName")
           lngRows = lngRows + 1
           .MoveNext
       Loop
       .Close
   End With
   For lngRows = 0 To UBound(varTest, 2)
       Debug.Print varTest(0, lngRows); " "; varTest(1, lngRows)
   Next lngRows

End Sub

Signature

Brendan Reynolds (MVP)

> I'm attempting to use a dynamic array and I'm having two problems:
>
[quoted text clipped - 44 lines]
>    End With
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
MikeC - 14 Feb 2005 21:27 GMT
Excellent code.  Thanks Brendan.

I can now see part of the problem I was having.  The wording of the online
help ("Declaring Arrays") had lead me to think that the first element in the
below array was the row and the second array element was the column.  After
reading your code, I see that I had it backwards!

I also prefer the way you are using the recordset properties.  I'll adopt
this method as my new standard.

Thanks again.

> Here is how you would add a row at a time to the array (I've used the
> Employees table in Northwind, so that I could test the result before
[quoted text clipped - 104 lines]
>>    End With
>> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.