我遇到了通过VBA从数据库查询的问题。通过我正在使用的客户端(FlameRobin)的查询返回我想要的完整列表(282条记录),我粘贴到Excel,而通过VBA(使用FireBird驱动程序)粘贴到Excel的查询返回276并省略特定产品。
我发现了什么:
还有什么我可以尝试解决这个问题吗?
Sub GetAccumulations()
Dim ws As Worksheet
Dim StrConn As String
Dim SQLStr As String, p As String
Dim oRs As ADODB.Recordset
Dim oADOConn As ADODB.Connection
Dim ObjFields As ADODB.Fields
Dim intLoop As Integer
Dim iRows As Integer, iCols As Integer
Set ws = ThisWorkbook.Worksheets("Inputs")
StrConn = "Driver={Firebird/InterBase(r) driver};Dbname=localhost:C:\TMP\PROPHET.prw;UID= **user omitted**;PWD=**pword omitted**;"
If oADOConn Is Nothing Then
Set oADOConn = New ADODB.Connection
End If
If oADOConn.State = 0 Then
oADOConn.Open StrConn
End If
Set oRs = New ADODB.Recordset
oRs.CursorLocation = adUseServer
SQLStr = "SELECT c.NAME AS ACCUMULATION, a.Name AS PRODUCT" _
& "FROM PRODUCT a " _
& "LEFT JOIN ACCUMULATIONENTRY b " _
& "ON a.PRODUCTID = b.MEMBERID " _
& "LEFT JOIN ACCUMULATION c " _
& "ON c.ACCUMULATIONID = b.PARENTACCUMULATION " _
& "WHERE c.Name IN **list**"
oRs.Open SQLStr, oADOConn
Set ObjFields = oRs.Fields
For intLoop = 0 To (ObjFields.Count - 1)
ThisWorkbook.Sheets("Inputs").Cells(1, intLoop + 6).Value2 = ObjFields.Item(intLoop).Name
Next
ThisWorkbook.Sheets("Inputs").Cells(2, 15).CopyFromRecordset Data:=oRs
oRs.Close
oADOConn.Close
Set oADOConn = Nothing
End Sub