Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Dimension

Status
Not open for further replies.

matrixindicator

IS-IT--Management
Sep 6, 2007
418
BE
Hello, dummy question,

I use a recordset to fill an array = ok it works.
However he asked to set the dimension of the array "FJa(72)"
I suppose this is the number of elements I expect to put in.
I have 72 records, but this is variable. Can you set the dimension variable ?


Code:
Dim FJa(72) As String
Dim i As Integer
 
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("TABLE", dbOpenDynaset)

i = 1
Do Until rst.EOF
    FJa(i) = rst!FIELD
    i = i + 1
    rst.MoveNext
Loop
i = Empty
i = 1
 

Dim FJa() As String
Dim i As Integer

Set dbs = CurrentDb
'Count your records
Set rst = dbs.OpenRecordset("SELECT COUNT(*) FROM TABLE", dbOpenDynaset)
'Redimension the array
ReDim FJa(1 To rst.fields(0))
'Close rst
rst.close

Set rst = dbs.OpenRecordset("TABLE", dbOpenDynaset)
i = 1
Do Until rst.EOF
FJa(i) = rst!FIELD
i = i + 1
rst.MoveNext
Loop
...
 
Yes you can. Have a look at ReDim Preserve or maybe you could set the ubound = to the record count.
 
matrixindicator . . .

In parallel with JerryKlmns but without the extra recordset:
Code:
[blue]   Dim db As DAO.Database, rst As DAO.Recordset, fld As Field
   Dim FJa(72) As String, i As Integer
   
   Set db = CurrentDb
   Set rst = db.OpenRecordset("TABLE", dbOpenTable)
   Set fld = rst!Field
   
   If Not rst.BOF Then
      ReDim Ary(1 To rst.RecordCount)
      i = 1
      
      Do Until rst.EOF
          FJa(i) = fld
          i = i + 1
          rst.MoveNext
      Loop
   End If

   Set fld = Nothing
   Set rst = Nothing
   Set db = Nothing[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top