我有一个Access数据库。我想自动查询表Data_01
并将结果导出到每天使用VBScript中的ADO的Excel电子表格。目前我缺乏ADO的技能。
Between Date() And Date()-1
PartNumber
列,我想选择一个特定的部件号。在GUI查询中,条件为Series 400
我目前正在将整个表格导出到Excel,然后使用VBScript选择我想要的列,然后删除所有不需要的数据,然后自动调整最终输出文件的列。这似乎有点处理器和时间。
答案 0 :(得分:1)
您是否尝试使用Excel中的内置函数导入数据?我没有Excel的英文版本,因此我不会引导您使用它们,但我认为菜单名为“数据”。
答案 1 :(得分:1)
以下是一些示例VBScript
Dim cn
Dim rs
strFile = "C:\Docs\LTD.mdb"
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile & ";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "SELECT * FROM tblTable " _
& "WHERE CrDate Between Now() And Date()-1 " _
& "AND OtherField='abc' " _
& "AND PartNumber=1 " _
& "ORDER BY CrDate, PartNumber"
rs.Open strSQL, cn
Set xl = CreateObject("Excel.Application")
Set xlBk = xl.Workbooks.Add
With xlbk.Worksheets(1)
For i = 0 To rs.Fields.Count - 1
.Cells(1, i + 1) = rs.Fields(i).Name
Next
.Cells(2, 1).CopyFromRecordset rs
.Columns("B:B").NumberFormat = "m/d/yy h:mm"
End With
xl.Visible=True
答案 2 :(得分:0)
我的第一反应是做以下事情:
答案 3 :(得分:0)
如果您没有Excel,则可以像这样使用ADO访问xls
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Const strDB = "" 'Location of Database file
Const strXLS = "" 'Location of spreadsheet
Set objAccessConnection = CreateObject("ADODB.Connection")
objAccessConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & strDB
Set objExcelConnection = CreateObject("ADODB.Connection")
objExcelConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strXLS & ";Extended Properties=""Excel 8.0;HDR=Yes;"";"
Set objAccessRecordset = CreateObject("ADODB.Recordset")
Set objExcelRecordSet = CreateObject("ADODB.Recordset")
strAccessQuery = "SELECT * FROM Data_01 WHERE PartNumberColumn = 'Series 400' AND DateColumn BETWEEN #" & Date -1 & "# AND #" & Date & "#"
objAccessRecordset.Open strAccessQuery, objAccessConnection, adOpenStatic, adLockOptimistic
strTable = "Sheet1$"
objExcelRecordSet.Open "Select * FROM [" & strTable & "]", objExcelConnection, adOpenStatic, adLockOptimistic, adCmdText
Do Until objAccessRecordset.EOF
objExcelRecordSet.AddNew
For i = 0 To objAccessRecordSet.Fields.Count - 1
objExcelRecordset.Fields(i).Value = objAccessRecordset.Fields(i).Value
Next
objExcelRecordSet.Update
objAccessRecordset.MoveNext
Loop
objExcelRecordset.Close
Set objExcelRecordset = Nothing
objAccessRecordset.Close
Set objAccessRecordset = Nothing
objAccessConnection.Close
Set objAccessConnection = Nothing
唯一需要注意的是确保电子表格中的列在第一行中有标题,否则此脚本可能会失败。
编辑:
您也可以将记录集写入.csv文件。
Const adClipString = 2
Const ForWriting = 2
Const ForAppending = 8
Const strDB = "C:\Test.mdb"
Const strCSV = "C:\Test.csv"
Set objAccessConnection = CreateObject("ADODB.Connection")
objAccessConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & strDB
Set objAccessRecordset = CreateObject("ADODB.Recordset")
strAccessQuery = "SELECT * FROM Data_01 WHERE PartNumber = 'Series 400' AND TheDate BETWEEN #" & Date -1 & "# AND #" & Date & "#"
objAccessRecordset.Open strAccessQuery, objAccessConnection, adOpenStatic, adLockOptimistic
Set objCSV = CreateObject("Scripting.FileSystemObject").OpenTextFile(strCSV, ForAppending, True)
objCSV.Write objAccessRecordset.GetString(adClipString,,",",CRLF)
objCSV.Close
Set objCSV = Nothing
objAccessRecordset.Close
Set objAccessRecordset = Nothing
objAccessConnection.Close
Set objAccessConnection = Nothing
Excel将打开.csv文件没有问题。这种方法的缺点是Excel不能很好地保存.csv文件,但在excel中,csv文件可以保存为xls。