Hi
I have the following code which exports excel files form a query to a folder on C:\. (with a lot of assistance from the forum!)
This code worked well but I have changed the underlying query and it now fails at the line
"CurrentDb.CreateQueryDef "CountryFile", strSQL"
producing the error message "Join Expression Not Supported"
The sql for the underlying query is
I have read a lot about bracketing to solve this problem but have been unable to fix this.
Could someone please tell me what is wrong with this.
Many thanks
I have the following code which exports excel files form a query to a folder on C:\. (with a lot of assistance from the forum!)
Code:
Private Sub cmdExpoirtMissingDataFiles_Click()
Dim rs As Recordset
Dim CreationMoment As String: CreationMoment = " Created on " & Format(Date, "ddmmyy") & " at " & Format(Time, "hhmmss")
Dim strSQL As String
Dim AndWhere As String
Dim MemberFirm As String
Dim xlApp As Object 'Excel.Application
Dim xlWB As Object 'Excel.Workbook
Dim xlSh As Object 'Excel.Worksheet
Dim sFilePath$
On Error GoTo ErrorHandler
Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT CountryCode, [Member Firm] FROM qryMissingData")
Set xlApp = CreateObject("Excel.Application") 'New Excel.Application
Do While Not rs.EOF
' this is the right alias
AndWhere = "tblConsolRawData.CountryCode = " & rs!CountryCode
' access the sql string in the querydef and replace the semicolon with the where clause
strSQL = Replace(CurrentDb.QueryDefs("QryMissingData").Sql, ";", " AND " & AndWhere)
CurrentDb.CreateQueryDef "CountryFile", strSQL
sFilePath = "C:\KPMG\Missing data Files\" & "Missing Data For Country Code " & rs!CountryCode
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "CountryFile", sFilePath, True
DoCmd.DeleteObject acQuery, "CountryFile"
Set xlWB = xlApp.Workbooks.Open(sFilePath)
Set xlSh = xlWB.Sheets(1)
xlApp.Visible = False
xlSh.Range("A1").Select
xlSh.Range(xlApp.Selection, xlApp.Selection.End(-4121)).Select '-4121 = xlDown
xlSh.Range(xlApp.Selection, xlApp.Selection.End(-4161)).Select '-4161 = xlToRight
xlApp.Selection.FormatConditions.Add 2, , "=LEN(TRIM(A1))=0" '2 = xlExpression
xlApp.Selection.FormatConditions(xlApp.Selection.FormatConditions.Count).SetFirstPriority
With xlApp.Selection.FormatConditions(1).Interior
.PatternColorIndex = -4105 '-4105 = xlAutomatic
.ThemeColor = 4 '4 = xlThemeColorLight2
.TintAndShade = 0.599963377788629
End With
xlApp.Selection.FormatConditions(1).StopIfTrue = False
xlWB.Close True
rs.MoveNext
Loop
Call CountFiles
cmdExportMissingDataFiles_Bye:
On Error Resume Next
rs.Close: Set rs = Nothing
Set xlWB = Nothing
Set xlSh = Nothing
xlApp.Quit
Set xlApp = Nothing
Err.Clear
Exit Sub
ErrorHandler:
MsgBox Err.Number & vbCr & Err.Description
' make sure CountryFile is deleted anyway
DoCmd.DeleteObject acQuery, "CountryFile"
Resume cmdExportMissingDataFiles_Bye
End Sub
This code worked well but I have changed the underlying query and it now fails at the line
"CurrentDb.CreateQueryDef "CountryFile", strSQL"
producing the error message "Join Expression Not Supported"
The sql for the underlying query is
Code:
SELECT tblDump.Id, tblConsolRawData.[Member Firm], tblConsolRawData.[Survey Methodology (Legacy Anonymous / Linked Data)], tblConsolRawData.GPID, tblConsolRawData.[First Name], tblConsolRawData.[Last Name], tblConsolRawData.[Email Address], tblConsolRawData.CountryCode, tblConsolRawData.[Global Job Level], tblConsolRawData.[Global Function], tblConsolRawData.[Employment Type], tblConsolRawData.Age, tblConsolRawData.[Full-time/Part-time], tblConsolRawData.Gender, tblConsolRawData.[Tenure (Length of Service)], tblConsolRawData.[Is a High Performer], tblConsolRawData.[Is a Performance Manager], tblConsolRawData.[Is a People Leader], tblConsolRawData.[Is Client Facing], tblConsolRawData.[Has Taken Parental Leave in the Past 3 Years], tblConsolRawData.[Recently Promoted (last 24 months)], tblConsolRawData.[Cost Centre], tblConsolRawData.[Mystery Code], tblConsolRawData.[Function L1], tblConsolRawData.[Function L2], tblConsolRawData.[Function L3], tblConsolRawData.[Function L4], tblConsolRawData.[Function L5], tblConsolRawData.[Function L6], tblConsolRawData.[Function L7], tblConsolRawData.[Function L8], tblConsolRawData.[Function L9], tblConsolRawData.[Function L10], tblConsolRawData.[Function L11], tblConsolRawData.[Function L12], tblConsolRawData.[Mystery Code1], tblConsolRawData.[Location L1], tblConsolRawData.[Location L2], tblConsolRawData.[Location L3], tblConsolRawData.[Location L4], tblConsolRawData.[Location L5], tblConsolRawData.[Location L6], tblConsolRawData.[Location L7], tblConsolRawData.[Location L8], tblConsolRawData.[Mystery Code2], tblConsolRawData.[Market L1], tblConsolRawData.[Market L2], tblConsolRawData.[Market L3], tblConsolRawData.[Market L4], tblConsolRawData.[Market L5], tblConsolRawData.[Market L6], tblConsolRawData.[Market L7], tblConsolRawData.[Market L8], tblConsolRawData.[Country Custom Demographic 1], tblConsolRawData.[Country Custom Demographic 2], tblConsolRawData.[Country Custom Demographic 3], tblConsolRawData.[Country Custom Demographic 4]
FROM tblDump INNER JOIN tblConsolRawData ON (tblDump.CountryCode = tblConsolRawData.CountryCode) AND (tblDump.Id = tblConsolRawData.ID);
I have read a lot about bracketing to solve this problem but have been unable to fix this.
Could someone please tell me what is wrong with this.
Many thanks