[英]how to export a custom query to excel in access/vba
I need to select different fields based on options selected in a form by a user.我需要根据用户在表单中选择的选项来选择不同的字段。 As a simplified example of what I'm trying to do.
作为我正在尝试做的事情的一个简化示例。
dim selection, sSql as string
If Forms![frmExport]![option1].Value = "Birthday" Then
selection = "bday"
ElseIf Forms![frmExport]![option1].Value = "Ages" Then
selection = "age"
ElseIf Forms![frmExport]![option1].Value = "Name" Then
selection = "name"
Else
selection = "*"
End If
sSql = "Select users." & selection & " from users"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, sSql, sPath & "user_info", True
But I get an error 7871 because while I can pass pre-defined queries to TransferSpreadsheet I apparently can't pass it one in the form of a string.但是我收到错误 7871,因为虽然我可以将预定义的查询传递给 TransferSpreadsheet,但显然我无法以字符串的形式传递它。 Is there a way to make this happen with making the ~50 permutations of possible queries and having a horrible giant nesting of ifs?
有没有办法通过对可能的查询进行大约 50 次排列并拥有可怕的 ifs 巨大嵌套来实现这一目标?
You can make the export query dynamic by setting its .Sql
property:您可以通过设置其
.Sql
属性使导出查询动态化:
Dim qd AS DAO.QueryDef
Set qd = CurrentDb.QueryDefs("myExportQuery")
qd.Sql = sSql
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, qd.Name, sPath & "user_info", True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.