0

I have VBA in access to loop through my table 2 recordset and join each row with my main table and create new worksheet. I'm pretty new and can't figure out the problem; and ? This time run-time error 3296 JOIN expression not supported can you experts look through my code and help me?

Here is My [sampleDB] https://drive.google.com/file/d/0B980etBxqQuzTGxiS1g3eUlLcHc/edit?usp=sharing

Thank you.

 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
7
  • 1
    what line of code do you get the error on? Jul 16, 2014 at 3:27
  • You know you can create a query that has the records from linked tables, and use that as a recordset, if you must?
    – Mark C.
    Jul 16, 2014 at 3:29
  • I need my main table join my second table one by one. I get error where I am creating worksheet : Set qdf = dbsReport.CreateQueryDef("Training_Report",....
    – NRad
    Jul 16, 2014 at 3:40
  • Now() should be Now also.
    – Mark C.
    Jul 16, 2014 at 3:45
  • @OverMind can you give me an example?
    – NRad
    Jul 16, 2014 at 13:22

2 Answers 2

0

I'm not 100% sure this is the source of your error, but you have double quotes inside your SQL statement ((SKSF_Req.Role) Like " * " & [Report].[Employee Role] & " * " that will surely cause the code to fail. Also, you can't refer to a recordset within a SQL statement like that (Would it be possible to include the recordset in the SQL directly instead of iterating over it? It would be much faster)

"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]"

should be more like:

"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]"
5
  • Thank you so much, NOw I get Run-time error 3085 undefined function 'rstSKSF' in expression and point out this line: DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Training_Reportsheet", xlsxPath, True
    – NRad
    Jul 16, 2014 at 4:19
  • oh I see, it's not a function, you are trying to refer to your recordset. you also should not include values from the recordset in the string that way. Editing my response Jul 16, 2014 at 4:26
  • Another thing to be aware of, you will need to escape single quotes if it is possible for a Course Name or a Role to have the ' character. (something like Replace(rstSKSF![Course Name],"'","''")) Jul 16, 2014 at 4:32
  • rstSKSF(SKSF_Req.[Course Name]) doesn't work, first you treat it like a function, second you put it inside the query, but it is not accessible when the query is run. it should be '" & rstSKSF![Course Name] & "', of course being wary of single quotes. Jul 16, 2014 at 4:50
  • thank you , i get another error and getting so tired can you help again please? This time run-time error 3296 JOIN expression not supported
    – NRad
    Jul 16, 2014 at 5:01
0

I figured it out. DAO doesn't take "' Like ' * ' & [Report].[Employee Role] & ' * ' and I was reading from query and had to change it to real table in strSQL = "SELECT * FROM SKSF_Req WHERE Flag IS NULL" Statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.