我从Excel VBA摸索出这一点,查询Access 2007年X.accdb文件:
'
' Variables:
' i: counter
' j: counter
' nFlds: number of fields in the query
' nMax: maximum number of records to be exported, 0=no limit
' strQry: query name
'
' objApp: Access.Application
' qdf: QueryDef
' rst: Recordset
'
Function daoDoQuery()
'
Dim i, j, nFlds, nMax, strQry
'
Dim objApp, qdf
Dim rst As DAO.Recordset
'
Set objApp = CreateObject("Access.Application")
objApp.OpenCurrentDatabase "some_path\AllInformation.accdb"
'
' get Recordset:
'
strQry = "MyQuery"
Set qdf = objApp.CurrentDb.QueryDefs(strQry)
'
' here [Current date] is entered:
'
qdf.Parameters(0).Value = Now()
Set rst = qdf.OpenRecordset(dbOpenDynaset)
'
If (rst.EOF) Then
Set rst = Nothing
daoDoQuery = 0
Exit Function
End If
'
nFlds = rst.Fields.Count
'
' create a new Excel Workbook to write results:
'
i = 1
Application.ScreenUpdating = False
Workbooks.Add
For j = 1 To nFlds
With Cells(i, j)
.Font.Bold = True
.Font.Size = 12
.Value = rst.Fields(j - 1).Name
End With
Next
'
nMax = 50
i = i + 1
'
Do While (Not rst.EOF)
'
For j = 1 To nFlds
Cells(i, j).Value = rst(j - 1)
Next
'
rst.MoveNext
i = i + 1
If (nMax > 0) Then
If (i > nMax) Then
Exit Do
End If
End If
Loop
'
Application.ScreenUpdating = True
'
rst.Close
Set rst = Nothing
Set qdf = Nothing
Set objApp = Nothing
'
daoDoQuery = 1
'
End Function
这将做的工作,金正日将创建一个新的Excel工作簿,由第1工作表作为上市结果:
daoDoQuery
看看这个:http://www.ozgrid.com/forum/showthread.php?t=91639它提供了你所需要的基础知识。 –