[英]VBA Recordset doesn't return all fields
I just startied working with this database and I have a small problem. 我刚开始使用此数据库,但是有一个小问题。 So the main idea behind this is to use VBA to get needed information from database that I can use later on. 因此,这背后的主要思想是使用VBA从数据库中获取所需的信息,以便以后使用。 I am using ADO recordset and connect sting to connect to server. 我正在使用ADO记录集并连接字符串以连接到服务器。 All is fine apart from one problem: when I am creating RecordSet by using SQL request it only returns one field when i know there should me more. 除一个问题外,一切都很好:当我通过使用SQL请求创建RecordSet时,当我知道应该有更多信息时,它仅返回一个字段。 At the moment I think that RecordSet is just grabbing first result and storing it in but looses anything else that should be there. 目前,我认为RecordSet只是获取第一个结果并将其存储在其中,但会丢失应该存在的其他任何内容。 Can you please help me. 你能帮我么。
Here is my code: 这是我的代码:
'Declare variables'
Dim objMyConn As ADODB.Connection
Dim objMyCmd As ADODB.Command
Dim objMyRecordset As ADODB.Recordset
Dim fldEach As ADODB.Field
Dim OrderNumber As Long
OrderNumber = 172783
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=Local;" & _
"Initial Catalog=SQL_LIVE;"
objMyConn.Open
'Set and Excecute SQL Command'
Set objMyCmd.ActiveConnection = objMyConn
objMyCmd.CommandText = "SELECT fldImage FROM tblCustomisations WHERE fldOrderID=" & OrderNumber
objMyCmd.CommandType = adCmdText
'Open Recordset'
Set objMyRecordset.Source = objMyCmd
objMyRecordset.Open
objMyRecordset.MoveFirst
For Each fldEach In objMyRecordset.Fields
Debug.Print fldEach.Value
Next
At the moment Debug returns only one result when it should return two because there are two rows with the same OrderID. 目前,由于只有两行具有相同的OrderID,因此Debug仅应返回一个结果,而应返回两个结果。
The recordset only opens a single record at a time. 记录集一次只打开一个记录。 You are iterating through all the fields in a single record. 您正在遍历单个记录中的所有字段。 Not each record in the recordset. 不是记录集中的每个记录。
If your query returns two records, you need to tell the Recordset to advance to the next one. 如果查询返回两个记录,则需要告诉Recordset前进到下一个记录。
A query returns one recordset which has some number of records which have some number of fields. 一个查询返回一个记录集 ,该记录集具有一定数量的记录和某些字段。
You are iterating through the fields only for one record in the returned recordset. 您仅在返回的记录集中的一条记录的字段中进行迭代。
You can do this with a few ways, but I generally do something like: 您可以通过以下几种方法来完成此操作,但我通常会执行以下操作:
objMyRecordset.MoveFirst
Do
If Not objMyRecordset.EOF Then
debug.print "Record Opened - only returning 1 field due to SQL query"
For Each fldEach In objMyRecordset.Fields
Debug.Print fldEach.Value
Next
'this moves to the NEXT record in the recordset
objMyRecordset.MoveNext
Else
Exit Do
End If
Loop
Note that if you want to include more fields you will need to modify this line: 请注意,如果要包括更多字段,则需要修改以下行:
objMyCmd.CommandText = "SELECT fldImage FROM tblCustomisations WHERE fldOrderID=" & OrderNumber
To include whatever additional fields you want returned. 包括要返回的任何其他字段 。
You are mixing up terms in your question which makes it unclear 您正在混淆问题中的术语,这使其不清楚
In your first paragraph you describe a problem with "Fields", in the last paragraph you turn it into "Rows". 在您的第一段中,描述了“字段”的问题,在最后一段中,将其变成“行”。 Not exactly the same. 不完全一样。
But whatever you are trying to achieve, the code you wrote will only return one field and one row. 但是,无论您要实现什么目标,编写的代码只会返回一个字段和一行。
If you want all FIELDS, your query should be: 如果需要所有FIELDS,则查询应为:
objMyCmd.CommandText = "SELECT * FROM tblCustomisations WHERE fldOrderID=" & OrderNumber
If you want all ROWS, your loop should be: 如果需要所有ROWS,则循环应为:
objMyRecordset.MoveFirst
If Not objMyRecordset.BOF Then
While Not objMyRecordset.EOF
debug.print objMyRecordset!fldImage
RS.MoveNext
Wend
End If
In addition to the @enderland's answer, you can also have a disconnected RecordSet, that have all the values and fields ready for consumption. 除了@enderland的答案之外,您还可以断开记录集的连接,该记录集具有可供使用的所有值和字段。 It's handy when you need to pass the data around or need to close the connection fast. 当您需要传递数据或需要快速关闭连接时,它非常方便。
Here's a function that returns a disconnected RecordSet: 这是一个返回未连接RecordSet的函数:
Function RunSQLReturnRS(sqlstmt, params())
On Error Resume next
' Create the ADO objects
Dim rs , cmd
Set rs = server.createobject("ADODB.Recordset")
Set cmd = server.createobject("ADODB.Command")
' Init the ADO objects & the stored proc parameters
cmd.ActiveConnection = GetConnectionString()
cmd.CommandText = sqlstmt
cmd.CommandType = adCmdText
cmd.CommandTimeout = 900 ' 15 minutos
collectParams cmd, params
' Execute the query for readonly
rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenForwardOnly, adLockReadOnly
If err.number > 0 then
BuildErrorMessage()
exit function
end if
' Disconnect the recordset
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
Set rs.ActiveConnection = Nothing
' Return the resultant recordset
Set RunSQLReturnRS = rs
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.