循环记录集的问题,试图找到数据和数据库记录之间的匹配
Issues with looping through recordset, trying to find match between data and database records
我对 SQL 和整个 ADO 环境还很陌生。我想做的是查看一个字符串是否已经在 ID table 中。当我使用 range.copyfromrecordset 方法时,结果显示得很好。
但是当我循环遍历代码时,我无法以某种方式在字段和行中找到字符串。
当使用 recordset.recordcount 时,它给出 -1。字符串 "SWED-A.ST" 位于我正在成功访问和检索的 ID table 中,但是我如何遍历记录集并在 window 和 debug.print 中立即显示它?我尝试使用循环和 movenext 遍历字段,但只显示 table-heading "ID" ....
代码
Sub Macro1() ' Tester Sub
Dim TickerID As String: TickerID = "SWED-A.ST"
Dim SQLString As String: SQLString = "SELECT TOP 1 ID FROM Instruments WHERE ID = '" & TickerID & "'"
Dim ADODBBuilder As ADODBBuilder: Set ADODBBuilder = New ADODBBuilder
Dim RecordSet As ADODB.RecordSet: Set RecordSet = ADODBBuilder.ReturnRecordset(TickerID, , , SQLString)
Public Function ReturnRecordset(Optional TickerID As String, Optional startdate As Date, Optional enddate As Date, Optional CustomSQLString As String) As ADODB.RecordSet
Const ConnectionPath As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\name\Desktop\DataBase.accdb;Persist Security Info=False;"
Dim DataConnection As ADODB.Connection: Set DataConnection = New ADODB.Connection
Dim RecordSet As ADODB.RecordSet: Set RecordSet = New ADODB.RecordSet
DataConnection.ConnectionString = ConnectionPath
DataConnection.Open
If Len(CustomSQLString) > 0 Then
SQLString = CustomSQLString
Else
If Sheets("Mainwindow").Btn_CurrentTime.Value = True Then
SQLString = "SELECT * FROM " & TickerID _
& " WHERE [Date] BETWEEN #" & startdate & "# AND #" & enddate & "#"
Else
SQLString = "SELECT * FROM " & TickerID
End If
End If
With RecordSet
.ActiveConnection = DataConnection
.Source = SQLString
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
Set ReturnRecordset = RecordSet
'Cleanup
Set RecordSet = Nothing
Set DataConnection = Nothing
End Function
默认的 ADO .Open
使用 adOpenForwardOnly
始终 returns 为 -1 的 .RecordCount
游标。您还在记录集属性中指定了 adOpenForwardOnly。您需要将其更改为 adOpenStatic
或 adOpenKeyset
以获得返回的准确记录数。
DataConnection.Open, adOpenStatic
···
With RecordSet
.ActiveConnection = DataConnection
.Source = SQLString
.LockType = adLockReadOnly
.CursorType = adOpenStatic
.Open
End With
我对 SQL 和整个 ADO 环境还很陌生。我想做的是查看一个字符串是否已经在 ID table 中。当我使用 range.copyfromrecordset 方法时,结果显示得很好。 但是当我循环遍历代码时,我无法以某种方式在字段和行中找到字符串。
当使用 recordset.recordcount 时,它给出 -1。字符串 "SWED-A.ST" 位于我正在成功访问和检索的 ID table 中,但是我如何遍历记录集并在 window 和 debug.print 中立即显示它?我尝试使用循环和 movenext 遍历字段,但只显示 table-heading "ID" ....
代码
Sub Macro1() ' Tester Sub
Dim TickerID As String: TickerID = "SWED-A.ST"
Dim SQLString As String: SQLString = "SELECT TOP 1 ID FROM Instruments WHERE ID = '" & TickerID & "'"
Dim ADODBBuilder As ADODBBuilder: Set ADODBBuilder = New ADODBBuilder
Dim RecordSet As ADODB.RecordSet: Set RecordSet = ADODBBuilder.ReturnRecordset(TickerID, , , SQLString)
Public Function ReturnRecordset(Optional TickerID As String, Optional startdate As Date, Optional enddate As Date, Optional CustomSQLString As String) As ADODB.RecordSet
Const ConnectionPath As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\name\Desktop\DataBase.accdb;Persist Security Info=False;"
Dim DataConnection As ADODB.Connection: Set DataConnection = New ADODB.Connection
Dim RecordSet As ADODB.RecordSet: Set RecordSet = New ADODB.RecordSet
DataConnection.ConnectionString = ConnectionPath
DataConnection.Open
If Len(CustomSQLString) > 0 Then
SQLString = CustomSQLString
Else
If Sheets("Mainwindow").Btn_CurrentTime.Value = True Then
SQLString = "SELECT * FROM " & TickerID _
& " WHERE [Date] BETWEEN #" & startdate & "# AND #" & enddate & "#"
Else
SQLString = "SELECT * FROM " & TickerID
End If
End If
With RecordSet
.ActiveConnection = DataConnection
.Source = SQLString
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
Set ReturnRecordset = RecordSet
'Cleanup
Set RecordSet = Nothing
Set DataConnection = Nothing
End Function
默认的 ADO .Open
使用 adOpenForwardOnly
始终 returns 为 -1 的 .RecordCount
游标。您还在记录集属性中指定了 adOpenForwardOnly。您需要将其更改为 adOpenStatic
或 adOpenKeyset
以获得返回的准确记录数。
DataConnection.Open, adOpenStatic
···
With RecordSet
.ActiveConnection = DataConnection
.Source = SQLString
.LockType = adLockReadOnly
.CursorType = adOpenStatic
.Open
End With