Building query criteria by form

G

Gordon

I am building the criteria for a query on the fly using a form which
has 3 elements:

1. 2 check boxes to determine which of 1 or 2 additional fields are in
the query
2 3 check boxes (based on Yes/No fields) to select certain records
records to be included or not
3. a multi select list box to select certain categories from one
field.

I realsie that for someone of my limited Access knowledge (Access 97)
this is quite complex so I am approaching this on a phased basis.

I am tackling stages 1 and 2 above having defined and built the SQL
for 3 string variables: strSelect, strFrom and strWhere. I then wish
to combine them then run the query. Here is that part of the code:

If strWhere <> "" Then strWhere = " WHERE " & Mid$(strWhere, 6)
strSQL = strSelect & strFrom & strWhere
CurrentDb.QueryDefs("qryExportDataTemplate").SQL = strSQL
DoCmd.OpenQuery "qryExportDataTemplate"


I am getting an error "characters found after the end of SQL
statement" eg in one pass the code generated is:

SELECT .................. tblMarketingContacts.fldTitle; WHERE
tblMarketingContacts.fldOutsideUK = true and
tblMarketingContacts.fldVIP = true and tblMarketingContacts.fldFriend
= true

The error is clearly the redundant ";" just before the WHERE, but how
do I remove that when building the SQL?

Thanks for any help.

Gordon
 
K

Ken Snell \(MVP\)

You have a semicolon character before the WHERE word in the SQL statement
that you've built. You need to strip that character from the SQL statement
before you append the WHERE clause that you built.
 
G

Gordon

You have a semicolon character before the WHERE word in the SQL statement
that you've built. You need to strip that character from the SQL statement
before you append the WHERE clause that you built.

--

        Ken Snell
<MS ACCESS MVP>http://www.accessmvp.com/KDSnell/















- Show quoted text -

I know Ken - I said that and my question was how do I remove that semi
colon using code?
 
K

Ken Snell \(MVP\)

Not knowing from where it comes, it'll be helpful if you post all the code
where you're building this criteria. I assume the semicolon is coming from
the strForm variable's value, but that is just a guess until we can see the
entire code.
--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


You have a semicolon character before the WHERE word in the SQL statement
that you've built. You need to strip that character from the SQL statement
before you append the WHERE clause that you built.

--

Ken Snell
<MS ACCESS MVP>http://www.accessmvp.com/KDSnell/















- Show quoted text -

I know Ken - I said that and my question was how do I remove that semi
colon using code?
 
G

Gordon

Not knowing from where it comes, it'll be helpful if you post all the code
where you're building this criteria. I assume the semicolon is coming from
the strForm variable's value, but that is just a guess until we can see the
entire code.
--

        Ken Snell
<MS ACCESS MVP>http://www.accessmvp.com/KDSnell/






I know Ken - I said that and my question was how do I remove that semi
colon using code?- Hide quoted text -

- Show quoted text -

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
 
K

Ken Snell \(MVP\)

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 end of
the query.

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


Not knowing from where it comes, it'll be helpful if you post all the code
where you're building this criteria. I assume the semicolon is coming from
the strForm variable's value, but that is just a guess until we can see
the
entire code.
--

Ken Snell
<MS ACCESS MVP>http://www.accessmvp.com/KDSnell/






I know Ken - I said that and my question was how do I remove that semi
colon using code?- Hide quoted text -

- Show quoted text -

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
 
G

Gordon

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 -

Ken,

Thanks for the prompt response. Your solution was so simple - I must
get better with my Access logic !
One final question - is there a simpler way for me to run the query?
At the moment, I have a predefined query template
("qryExportDataTemplate") but which is then called from the now
corrected code as below:

If strWhere <> "" Then strWhere = " WHERE " & Mid$(strWhere, 6)
strSQL = strSelect & strFrom & strWhere & ";"
strSQL = strSelect & strFrom & strWhere
CurrentDb.QueryDefs("qryExportDataTemplate").SQL = strSQL
DoCmd.OpenQuery "qryExportDataTemplate"

Is there a way I can get rid of the query template and just run
everything from the code itself?

Gordon
 
K

Ken Snell \(MVP\)

Using the query template is a very acceptable and appropriate way to do what
you want to do. I see no reason to do anything differently.
--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/



Ken,

Thanks for the prompt response. Your solution was so simple - I must
get better with my Access logic !
One final question - is there a simpler way for me to run the query?
At the moment, I have a predefined query template
("qryExportDataTemplate") but which is then called from the now
corrected code as below:

If strWhere <> "" Then strWhere = " WHERE " & Mid$(strWhere, 6)
strSQL = strSelect & strFrom & strWhere & ";"
strSQL = strSelect & strFrom & strWhere
CurrentDb.QueryDefs("qryExportDataTemplate").SQL = strSQL
DoCmd.OpenQuery "qryExportDataTemplate"

Is there a way I can get rid of the query template and just run
everything from the code itself?

Gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top