问题
Quick question about ADODB recordset. I want to extract a query result from a recordset and set it to a string variable:
query = "SELECT Value1 FROM Table"
RS = New ADODB.Recordset
RS.Open(query,Conn)
The query is always going to return 3 values. I want take to assign these three values to a variable. RS.GetString returns the query results tab delimited, but I want to be able to iterate through this set and assign each row result to its own variable. Any ideas? Thanks in advance!
回答1:
You can access the tables columns by name through the recordset.
Dim strValue As String
strValue = rs.Fields("ColumnNameHere").Value
As for looping the recordset
Do While rs.EOF = False
strValue = rs.Fields("ColumnNameHere").Value
msgbox (strValue)
rs.MoveNext
Loop
And if you are just learning looping. You'll at some point need to learn about rs.MoveFirst
Once you have been through your recordset and you want to go through it again for some reason you will need to move back to the beginning of the recordset before you can loop it again.
Recordsets also have rs.Find
and rs.Filter
and rs.Sort
.
Lot's of good stuff.
来源:https://stackoverflow.com/questions/33157320/adodb-recordset-to-string-variable-vba