I'm trying to do the following
I understand the concept of a cursor, though I think much better with array indexes. The movenext movelast movefirst stuff makes sense, but I have no idea how to use it.
So far I have the following code, and it works, up until I want to receive the resulting data set as a recordset, and then put it into an array. I have exhausted my ability to google this issue.
- Send a query to a SQL Server DB
- Receive a dataset (in the form of a recordset object)
- Store all of the rows of a specific column of the recordset data into a dynamic array (which would require something like redim array(1 to recordset.recordcount).
- I can use the resulting array to make a report, since I know how to use arrays.
I understand the concept of a cursor, though I think much better with array indexes. The movenext movelast movefirst stuff makes sense, but I have no idea how to use it.
So far I have the following code, and it works, up until I want to receive the resulting data set as a recordset, and then put it into an array. I have exhausted my ability to google this issue.
Code:
Sub place_recordset_into_array()
[INDENT]Dim query_string As String
Dim array_size As String
Dim myarray() As String
[/INDENT]
query_string = "SELECT DISTINCT [field] FROM [table]"
[INDENT]Call run_query(query_string)
[/INDENT]
[INDENT]array_size = record_set.recordcount 'Gives me an error saying "Object Required"
[/INDENT]
ReDim myarray(1 To array_size) 'I'm only getting 1 record when I look in the local variables window during debugger, even though there should be 5 or 6 records
[INDENT]
For i = 1 to array_size 'Haven't gotten this far yet, but I suspect it won't work.
[/INDENT]
[INDENT=2]myarray(i) = record_set.fields([field_name]),Item(i)
[/INDENT]
[INDENT]Next i
[/INDENT]
End Sub
Function run_query(query_string As String) As ADODB.Recordset
Dim server_connection As ADODB.Connection
Dim server_command As ADODB.Command
Dim record_set As ADODB.Recordset
Set server_connection = New ADODB.Connection
server_connection.Open ("Provider='SQLOLEDB'; Data Source='[server name]'; Initial Catalog='[DB Name]'; Integrated Security='SSPI';")
Set server_command = New ADODB.Command
server_command.ActiveConnection = server_connection
server_command.CommandText = query_string
Set record_set = server_command.Execute(query_string)
End Function
Last edited by a moderator: