[英]MS Access, VBA - Export Crosstable to Excel
OBJECTIVE 目的
APPROACH APPROACH
ISSUES 问题
DoCmd.TransferSpreadsheet
) to export a crosstable? DoCmd.TransferSpreadsheet
)来导出交叉表? Why this is so difficult amazes me, apologies for the naivety 为什么这么难,令我惊讶,为天真致歉
I simply create the query I want in MS Access. 我只是在MS Access中创建所需的查询。
Then in excel i have a macro I have written that runs the query and outputs to sheet. 然后在Excel中,我编写了一个宏,用于运行查询并将其输出到工作表。 see below for an example.
请参阅下面的示例。
Dim cnString As String
Dim rs As New ADODB.Recordset
'Change connection string to an MS Access one
cnString = "Provider=xx;Server=xx,49168;Database=xx;"
rs.Open queryName, cnString, adOpenStatic, adLockOptimistic
'Output Headings
For i = 1 To rs.Fields.Count
Sheets(PasteSheet).Range(pasteCell).Offset(0, i - 1) = rs.Fields(i - 1).Name
Next i
'Output data
Sheets(PasteSheet).Range(pasteCell).Offset(1, 0).CopyFromRecordset rs
rs.close
If you wanted to do all of this in MS Access then it wouldn't take much to modify. 如果您想在MS Access中完成所有这些操作,则无需花费太多修改。 You would simply just need to create a new workbook and then do a very similar thing to the above code.
您只需要创建一个新的工作簿,然后执行与上述代码非常相似的操作即可。
Following up from my original post. 跟进我原来的帖子。 It appears that you can use
DoCmd.TransferSpreadsheet
to export a crosstable. 看来您可以使用
DoCmd.TransferSpreadsheet
导出交叉表。
I've defined the working code below: 我在下面定义了工作代码:
outputQuery = [your cross table query]
outputPath = [save-to file location]
outputFileName = [save-to file name]
DoCmd.TransferSpreadsheet acExport, SpreadsheetType:=acSpreadsheetTypeExcel12Xml, TableName:=outputQuery, FileName:=outputPath & outputfilename
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.