ADODB RecordSet to string variable VBA

非 Y 不嫁゛ 提交于 2019-12-11 10:00:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!