我用于访问 Access 数据库中的数据的 Inner Join 语句存在问题。我期望发生的是,我遍历了每个产品的记录集。当产品订购不止一次时,我不会将其添加到 Excel 表中,而是增加订购的数量和总成本。
我遇到的问题是,它不是按照我上面描述的方式工作,而是在每次订购时将产品添加到 Excel 表中。我发现它正在按照我的代码中未包含的订购顺序(按他们的 OrderID)打印产品。
有什么帮助吗?
这是代码:
Public Sub WorksheetLoop()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim stDB As String, stSQL As String, stSQLTwo As String, stProvider As String
Dim sheetName As String, stProdName As String
Dim suppNum As Integer, prodNum As Integer
Dim WS_Count As Integer
Dim I As Integer
suppNum = 1
prodNum = 1
stDB = "Data Source= " & ThisWorkbook.Path & "\obsDatabase.accdb"
stProvider = "Microsoft.ACE.OLEDB.12.0"
'Opening connection to database
With cn
.ConnectionString = stDB
.Provider = stProvider
.Open
End With
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count
' Begin the loop.
For I = 2 To WS_Count
ActiveWorkbook.Worksheets(I).Range("A1") = "Company Name - " + ActiveWorkbook.Worksheets(I).Name + ""
ActiveWorkbook.Worksheets(I).Range("A2") = "Item Number"
ActiveWorkbook.Worksheets(I).Range("B2") = "Description"
ActiveWorkbook.Worksheets(I).Range("C2") = "Unit"
ActiveWorkbook.Worksheets(I).Range("D2") = "Cost Per Unit"
ActiveWorkbook.Worksheets(I).Range("E2") = "Quantity"
ActiveWorkbook.Worksheets(I).Range("F2") = "Total Cost"
ActiveWorkbook.Worksheets(I).Range("G2") = "Amount Remaining"
'Function to retrieve info!
stSQL = "SELECT Products.ProductID, Products.ProductName, Products.ProductDescription, Products.ProductUnit, LineItems.UnitPrice, LineItems.Quantity, LineItems.TotalPrice " & _
"FROM Products INNER JOIN LineItems ON LineItems.ProductID = Products.ProductID WHERE Products.SupplierID = " & suppNum & " "
rs.Open stSQL, cn
With rs
Do Until .EOF
If ActiveWorkbook.Worksheets(I).Range("A65536").End(xlUp) = rs.Fields("ProductName") Then
If ActiveWorkbook.Worksheets(I).Range("D65536").End(xlUp) = rs.Fields("UnitPrice") Then
ActiveWorkbook.Worksheets(I).Range("E65536").End(xlUp) = ActiveWorkbook.Worksheets(I).Range("E65536").End(xlUp) + rs.Fields("Quantity")
ActiveWorkbook.Worksheets(I).Range("F65536").End(xlUp) = ActiveWorkbook.Worksheets(I).Range("F65536").End(xlUp) + rs.Fields("TotalPrice")
End If
Else
ActiveWorkbook.Worksheets(I).Range("A65536").End(xlUp).Offset(1, 0) = rs.Fields("ProductName")
ActiveWorkbook.Worksheets(I).Range("B65536").End(xlUp).Offset(1, 0) = rs.Fields("ProductDescription")
ActiveWorkbook.Worksheets(I).Range("C65536").End(xlUp).Offset(1, 0) = rs.Fields("ProductUnit")
ActiveWorkbook.Worksheets(I).Range("D65536").End(xlUp).Offset(1, 0) = rs.Fields("UnitPrice")
ActiveWorkbook.Worksheets(I).Range("E65536").End(xlUp).Offset(1, 0) = rs.Fields("Quantity")
ActiveWorkbook.Worksheets(I).Range("F65536").End(xlUp).Offset(1, 0) = rs.Fields("TotalPrice")
End If
rs.MoveNext
Loop
End With
rs.Close
suppNum = suppNum + 1
ActiveWorkbook.Worksheets(I).Columns("A:A").EntireColumn.AutoFit
ActiveWorkbook.Worksheets(I).Columns("B:B").EntireColumn.AutoFit
ActiveWorkbook.Worksheets(I).Columns("C:C").EntireColumn.AutoFit
ActiveWorkbook.Worksheets(I).Columns("D:D").EntireColumn.AutoFit
ActiveWorkbook.Worksheets(I).Columns("E:E").EntireColumn.AutoFit
ActiveWorkbook.Worksheets(I).Columns("F:F").EntireColumn.AutoFit
ActiveWorkbook.Worksheets(I).Columns("G:G").EntireColumn.AutoFit
Next I
cn.Close
End Sub