|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
交叉查询输出到EXCEL的例子
Dim strSQL As String
Private Sub Command9_Click()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim I As Integer
Dim x As Integer
Dim oExcel As Object
Dim oBook As Object
Dim strSQL As String
strSQL = "SELECT * FROM (TRANSFORM Count(BatchNumber) AS BatchNumberOfCount SELECT PartNumber, Count(BatchNumber) AS Qty DESC) FROM Data GROUP BY PartNumber PIVOT Defect;"
Set conn = CurrentProject.Connection
rs.Open strSQL, conn, adOpenKeyset, adLockOptimistic
If rs.RecordCount = 0 Then rs.Close: Exit Sub
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add()
Set xlSheet = oBook.Worksheets(1)
oExcel.Visible = True
For I = 0 To rs.Fields.Count - 1
xlSheet.Cells(1, I + 1).Value = rs.Fields(I).Name
Next
xlSheet.Cells(2, 1).CopyFromRecordset rs
rs.Close
End Sub |
|