I'm needing to get fields names from a query and put them into a dynamic array. I believe I have found the proper code this, however I get a "Subscript not in Range" error.
Code so far:
Dim qdf As QueryDef
Dim fld As Field
Dim o As Integer
Dim fieldCount as Integer
fieldCount = CurrentDb.QueryDefs("qryctAverage").Fields.Count
Set qdf = db.QueryDefs("qryctAverage")
Dim n As Integer
n = fieldCount
ReDim colHeaders(0 To n - 1)
For o = 0 To n - 1
colHeader(o) = qdf.Fields(o).Name
Next o
Line of error: colHeader(o) = qdf.Fields(o).Name
I'm fairly new to VBA so I appreciate the patience and time put in to helping out! Thanks in advance
Note: All answer I've found have applied to non-dynamic arrays.
EDIT:
I now get "Sub or Function not defined" after removing Dim colHeader() as String
Error Line: colHeader(o) = qdf.Fields(o).Name
qdfoffdb, butfieldCountoffCurrentDb? Are they the same? I would be expecting to seefieldCount = qdf.Fields.Countunder theSet qdfassignment. Alsonis redundant, loop would be clearer withFor o = 0 To fieldCount - 1- Mathieu GuindonfieldCountmoved in so there wouldn't be confusion as to where I gotfieldCountfrom - JaskiercolHeaderwithin the loop, butcolHeaderselsewhere to cause that specific error..... but definitely take into account the other comments. No.... you definecolHeader()but thenReDim colHeaders.... - Darren Bartrup-CookcolHeaderwithcolHeaderseverywhere else in that procedure.... - Mathieu GuindoncolHeaders(o)- Mathieu Guindon