【发布时间】:2014-09-06 10:41:13
【问题描述】:
我有 VBA 可以访问我的表 2 记录集,并将每一行与我的主表连接起来并创建新的工作表。 我很新,无法弄清楚问题所在;和 ?这次run-time error 3296 JOIN expression not supported 各位高手能帮我看看我的代码吗?
这是我的 [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
【问题讨论】:
-
你在哪一行代码上得到了错误?
-
您知道您可以创建一个包含来自链接表的记录的查询,并将其用作记录集(如果必须)?
-
我需要我的主桌一张一张地加入我的第二张桌子。我在创建工作表时遇到错误: Set qdf = dbsReport.CreateQueryDef("Training_Report",....
-
Now()也应该是Now。 -
@OverMind 你能举个例子吗?
标签: ms-access vba ms-access-2010 dao