在MSAccess中,我使用基于查询的记录集作为数据源,以通过CopyFromRecordset填充XLS文件。
当我引用查询名本身时出现错误(参数太少,预期为1),但是在引用从该查询复制的基础SQL字符串时却按预期工作。不确定我缺少什么:
摘要:
Dim appXLS As Object
Dim wbk As Object
Dim wks1 As Object
Dim rs As dao.Recordset
Dim strSql As String
Dim db As Database
'...unrelated stuff removed
'This method fails:
strSql = "q_Expense_export" '< query
'This method works (exact SQL from query above):
strSql = "SELECT InventoryTeams.EventID, InventoryTeams.EventName, InventoryTeams.StaffID, InventoryTeams.LastName, InventoryTeams.FirstName, '' AS ExpenseDate, '' AS ExpenseType, '' AS Amount, '' AS Notes " & _
"FROM InventoryTeams " & _
"WHERE (((InventoryTeams.EventID) = " & [Forms]![Mainform]![InventoryTeamExp_subform].[Form]![EventID] & ")) " & _
"ORDER BY InventoryTeams.LastName;"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
'Dump rs into XLS...
With wks1
.Range("A" & n).CopyFromRecordset rs
End With
...unrelated XLS formatting
是否可以仅使用查询名称来定义记录集还是完整的SQL?我不介意使用SQL文本,但是我还有其他查询要使用“ q_Expense_export”实现JOINED。仅仅超出了我的技能范围即可获取完整的SQL字符串...
想法?