Remove the semicolon from the end of this code step (I assume it's commented
out by error?):
'Stage 2 - add the source of the data fields
'strFrom = " FROM tblMarketingContacts right JOIN tblContactCategories
ON tblMarketingContacts.fldContactCategoryID =
tblContactCategories.fldContactCategoryID right JOIN tblTitles ON
tblMarketingContacts.fldTitle = tblTitles.fldTitleID "
strFrom = " FROM tblTitles RIGHT JOIN (tblMarketingContacts INNER JOIN
tblContactCategories ON tblMarketingContacts.fldContactCategoryID =
tblContactCategories.fldContactCategoryID) ON tblTitles.fldTitleID =
tblMarketingContacts.fldTitle;"
Then, add a semicolon to the end of the strSQL value:
strSQL = strSelect & strFrom & strWhere & ";"
Although the semicolon is not necessary when you build a query in VBA.
ACCESS will execute the query correctly even if it's not there at the endof
the query.
--
Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/
Yes, of course Ken. Here it is:
Private Sub cmdExportNamesAddresses_Click()
Dim strSQL As String, strWhere,as string strSelect as string, strFrom
As String
'Stage 1 - Construct query with basic fields
strSelect = "SELECT tblTitles.fldTitle,
tblMarketingContacts.fldInitials, tblMarketingContacts.fldSurname,
tblMarketingContacts.fldAddress1, "
strSelect = strSelect & " tblMarketingContacts.fldAddress2,
tblMarketingContacts.fldAddress3, tblMarketingContacts.fldAddress4,
tblMarketingContacts.fldAddress5, "
strSelect = strSelect & " tblMarketingContacts.fldAddress6,
tblMarketingContacts.fldPostCode"
'Additional field - tblMarketingContacts.fldAffix
If Me!chkNameAffix = True And Me!chkCompanyPosition = False Then
strSelect = "SELECT tblTitles.fldTitle,
tblMarketingContacts.fldInitials, tblMarketingContacts.fldSurname,
tblMarketingContacts.fldAffix, tblMarketingContacts.fldAddress1, "
strSelect = strSelect & " tblMarketingContacts.fldAddress2,
tblMarketingContacts.fldAddress3, tblMarketingContacts.fldAddress4,
tblMarketingContacts.fldAddress5, "
strSelect = strSelect & " tblMarketingContacts.fldAddress6,
tblMarketingContacts.fldPostCode"
End If
'Additional fields -
tblMarketingContacts.fldCompanyName,tblMarketingContacts.fldPosition
If Me!chkNameAffix = False And Me!chkCompanyPosition = True Then
strSelect = "SELECT tblTitles.fldTitle,
tblMarketingContacts.fldInitials, tblMarketingContacts.fldSurname,
tblMarketingContacts.fldPosition, tblMarketingContacts.fldCompanyName,
tblMarketingContacts.fldAddress1, "
strSelect = strSelect & " tblMarketingContacts.fldAddress2,
tblMarketingContacts.fldAddress3, tblMarketingContacts.fldAddress4,
tblMarketingContacts.fldAddress5, "
strSelect = strSelect & " tblMarketingContacts.fldAddress6,
tblMarketingContacts.fldPostCode"
End If
'Additional fields - tblMarketingContacts.fldAffix,
tblMarketingContacts.fldCompanyName,tblMarketingContacts.fldPosition
If Me!chkNameAffix = True And Me!chkCompanyPosition = True Then
strSelect = "SELECT tblTitles.fldTitle,
tblMarketingContacts.fldInitials, tblMarketingContacts.fldSurname,
tblMarketingContacts.fldAffix, tblMarketingContacts.fldPosition,
tblMarketingContacts.fldCompanyName, tblMarketingContacts.fldAddress1,
"
strSelect = strSelect & " tblMarketingContacts.fldAddress2,
tblMarketingContacts.fldAddress3, tblMarketingContacts.fldAddress4,
tblMarketingContacts.fldAddress5, "
strSelect = strSelect & " tblMarketingContacts.fldAddress6,
tblMarketingContacts.fldPostCode"
End If
'Stage 2 - add the source of the data fields
'strFrom = " FROM tblMarketingContacts right JOIN tblContactCategories
ON tblMarketingContacts.fldContactCategoryID =
tblContactCategories.fldContactCategoryID right JOIN tblTitles ON
tblMarketingContacts.fldTitle = tblTitles.fldTitleID "
strFrom = " FROM tblTitles RIGHT JOIN (tblMarketingContacts INNER JOIN
tblContactCategories ON tblMarketingContacts.fldContactCategoryID =
tblContactCategories.fldContactCategoryID) ON tblTitles.fldTitleID =
tblMarketingContacts.fldTitle;"
'Stage 3 - filter in/out specifc records
'Overseas addresses
If Me!chkForeignAdds = True And Me!chkFriends = False And Me!chkVIP =
False Then strWhere = strWhere & " and
tblMarketingContacts.fldOutsideUK = true"
'Friends addresses
If Me!chkForeignAdds = False And Me!chkFriends = True And Me!chkVIP =
False Then strWhere = strWhere & " and tblMarketingContacts.fldFriend
= true"
'VIP addresses
If Me!chkForeignAdds = False And Me!chkFriends = False And Me!chkVIP =
True Then strWhere = strWhere & " and tblMarketingContacts.fldVIP=
true"
'Overseas and chkFriends
If Me!chkForeignAdds = True And Me!chkFriends = True And Me!chkVIP =
False Then strWhere = strWhere & " and
tblMarketingContacts.fldOutsideUK = true and
tblMarketingContacts.fldFriend = true"
'chkFriends and VIPs
If Me!chkForeignAdds = False And Me!chkFriends = True And Me!chkVIP =
True Then strWhere = strWhere & " and tblMarketingContacts.fldFriends
= true and tblMarketingContacts.fldFriend = true"
'Overseas and VIPs
If Me!chkForeignAdds = True And Me!chkFriends = False And Me!chkVIP =
True Then strWhere = strWhere & " and
tblMarketingContacts.fldOutsideUK = true and
tblMarketingContacts.fldVIP = true"
'No Overseas, chkFriends or VIPs
If Me!chkForeignAdds = False And Me!chkFriends = False And Me!chkVIP =
False Then strWhere = strWhere
'Overseas, chkFriends and VIPs
If Me!chkForeignAdds = True And Me!chkFriends = True And Me!chkVIP =
True Then strWhere = strWhere & " and
tblMarketingContacts.fldOutsideUK = true and
tblMarketingContacts.fldVIP = true and tblMarketingContacts.fldFriend
= true"
If strWhere <> "" Then strWhere = " WHERE " & Mid$(strWhere, 6)
strSQL = strSelect & strFrom & strWhere
CurrentDb.QueryDefs("qryExportDataTemplate").SQL = strSQL
DoCmd.OpenQuery "qryExportDataTemplate"
End Sub- Hide quoted text -
- Show quoted text -