我有VBA访问循环遍历我的表2记录集并使用我的主表连接每一行并创建新的工作表。 我很新,不能解决问题;和?这次运行时错误3296 JOIN表达式不支持你可以让专家查看我的代码并帮助我吗?
这是我的[sampleDB] https://drive.google.com/file/d/0B980etBxqQuzTGxiS1g3eUlLcHc/edit?usp=sharing
谢谢。
Sub ExportReport()
Dim dbsReport As DAO.Database
Dim qdf As DAO.QueryDef
Dim rstSKSF As DAO.Recordset
Dim strSQL As String
Dim xlsxPath As String
On Error GoTo ErrorHandler
Set dbsReport = CurrentDb
xlsxPath = "I:\Proj\Tr_Rep " & Format(Now(), "mm-dd-yyyy hhmmss AMPM") & ".xlsx"
'Open a recordset on all records from the SkillSoft Request table that have
'a Null value in the ReportsTo field.
strSQL = "SELECT * FROM SKSF_Req WHERE Flag IS NULL"
Set rstSKSF = dbsReport.OpenRecordset(strSQL, dbOpenDynaset)
'If the recordset is empty, exit.
If rstSKSF.EOF Then Exit Sub
With rstSKSF
Do Until .EOF
'join report table with SKSF_request table's Rows
'Create newworksheet for each report joint with SKSF rows
Set qdf = dbsReport.CreateQueryDef("Training_Report", _
"SELECT Report.Name, Report.[Employee Role], Report.[Employee Location], Report.[Retails Region], Report.[Asset Title], Report.[Completion Date], Report.[Completion Stat]FROM Report LEFT JOIN SKSF_Req ON Report.[Asset Title] = rstSKSF(SKSF_Req.[Course Name]) WHERE (((Report.[Asset Title]) = rstSKSF([SKSF_RequestForm].[Course Name])) And (rstSKSF((SKSF_Req.Role) Like " * " & [Report].[Employee Role] & " * ")) GROUP BY Report.Name, Report.[Employee Role], Report.[Employee Location], Report.[Retails Region], Report.[Asset Title], Report.[Completion Date], Report.[Completion Stat], Report.[EMP ID]")
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Training_Report", xlsxPath, True
DoCmd.DeleteObject acQuery, "Training_Report"
.Edit
rstSKSF![Flag] = "Y" 'Set Flag
.Update
.MoveNext
Loop
End With
rstSKSF.Close
dbsReport.Close
Set rstSKSF = Nothing
Set dbsReport = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Sub
答案 0 :(得分:0)
我不是100%确定这是你的错误的来源,但你的SQL语句中有双引号((SKSF_Req.Role) Like " * " & [Report].[Employee Role] & " * "
肯定会导致代码失败。另外,你不能引用像这样的SQL语句中的记录集(是否可以直接在SQL中包含记录集而不是迭代它?它会快得多)
"SELECT Report.Name, Report.[Employee Role], Report.[Employee Location], Report.[Retails Region], Report.[Asset Title], Report.[Completion Date], Report.[Completion Stat]FROM Report LEFT JOIN SKSF_Req ON Report.[Asset Title] = rstSKSF(SKSF_Req.[Course Name]) WHERE (((Report.[Asset Title]) = rstSKSF([SKSF_RequestForm].[Course Name])) And (rstSKSF((SKSF_Req.Role) Like " * " & [Report].[Employee Role] & " * ")) GROUP BY Report.Name, Report.[Employee Role], Report.[Employee Location], Report.[Retails Region], Report.[Asset Title], Report.[Completion Date], Report.[Completion Stat], Report.[EMP ID]"
应该更像:
"SELECT Report.Name, Report.[Employee Role], Report.[Employee Location]," & _
" Report.[Retails Region], Report.[Asset Title], Report.[Completion Date], " & _
" Report.[Completion Stat] " & _
"FROM Report LEFT JOIN SKSF_Req ON Report.[Asset Title] = '" & rstSKSF![Course Name] & "'" & _
" WHERE (((Report.[Asset Title]) = '" & rstSKSF![Course Name] & "'" & _
" And '" & rstSKSF!Role & "' Like ' * ' & [Report].[Employee Role] & ' * ')) " & _
" GROUP BY Report.Name, Report.[Employee Role], Report.[Employee Location], " & _
" Report.[Retails Region], Report.[Asset Title], Report.[Completion Date], " & _
" Report.[Completion Stat], Report.[EMP ID]"
答案 1 :(得分:0)
我明白了。
DAO不接受"' Like ' * ' & [Report].[Employee Role] & ' * '
我正在从查询中读取并且必须将其更改为s trSQL = "SELECT * FROM SKSF_Req WHERE Flag IS NULL"
语句中的真实表。