access vba将查询结果附加到XML文件

时间:2013-06-21 14:58:40

标签: xml vba export

我有一个xlsx文件,其中有一些标题,直到第6行。 我需要获取查询的结果并将它们放入第7行的文件中。 我不确定我是否追加或导出查询结果。

Const acExportQuery = 1
Set objAccess = CreateObject("Access.Application")
objAccess.OpenCurrentDatabase "\HealthyImmunity Contact Manager - V1.53.adp"
objAccess.ExportXML acAppendData, "c:\scripts\testOrder.xlsx", "col_USbatchOrders"

有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

下面是一个可以从excel触发的示例代码..即使在excel中也可以将它添加到workbook_open中,它应该从数据库中导入查询执行结果

 Sub GetData()

 Dim cnn As ADODB.Connection

 Dim rs As ADODB.Recordset

 Dim sQRY As String

 Dim strFilePath As String

 strFilePath = "\HealthyImmunity Contact Manager - V1.53.adp"   
 Set cnn = New ADODB.Connection
 Set rs = New ADODB.Recordset
 Sheet1.Range("DataRange").ClearContents
 cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strFilePath & ";"
 sQRY = "SELECT tblData.* FROM tblData"
 rs.CursorLocation = adUseClient
 rs.Open sQRY, cnn, adOpenStatic, adLockReadOnly
 Application.ScreenUpdating = False
 Sheet1.Range("B2").CopyFromRecordset rs
 rs.Close
 Set rs = Nothing
 cnn.Close
 Set cnn = Nothing
 Exit Sub

End Sub