strSQL as String

S

S Jackson

I am trying to requery a combo box on my form based on the selection made in
another combo box. Here is the code I have so far:

'Code to filter lkpPetitioner
Dim strSQL As String
strSQL = "SELECT tblCases.PetLastName & ', ' & tblCases.PetFirstName AS
Petitioner, " _

'Build WHERE Clause
Select Case Me.FindProgram
Case "CNA"
strSQL = strSQL & "WHERE tblCases.Program = CNA " _

Case "EMR"
strSQL = strSQL & "WHERE tblCases.Program = EMR " _

Case "NFA"
strSQL = strSQL & "WHERE tblCases.Program = NFA " _

Case "CMA"
strSQL = strSQL & "WHERE tblCases.Program = CMA " _

End Select

'Order Statement
strSQL = strSQL & "ORDER by PetLastName;"
MsgBox strSQL

Me.lkpPetitioner.RowSource = strSQL
Me.lkpPetitioner.Requery

I keep getting a syntax error (missing operator) in query expression 'WHERE
tblCases.Program = NFA ORDER by PetLastName' for the lkpPetitioner combo box
data source, but everything seems to look fine when I view the MsgBox
strSQL:

SELECT tblCases.PetLastName & ', ' & tblCases.PetFirstName AS Petitioner,
WHERE tblCases.Program = NFA ORDER by PetLastName;

What is wrong here?
TIA
S. Jackson
 
S

S Jackson

Figured it out! I forgot the to include SELECT tblCases.Program and I
forgot to include the FROM clause. FYI, here is the correct code:

Dim strSQL As String
strSQL = "SELECT tblCases.PetLastName & ', ' & tblCases.PetFirstName AS
Petitioner, " _
& "tblCases.Program FROM tblCases " _

'Build WHERE Clause
Select Case Me.FindProgram
Case "CNA"
strSQL = strSQL & "WHERE tblCases.Program = " & "'" & "CNA" & "' " _

Case "EMR"
strSQL = strSQL & "WHERE tblCases.Program = " & "'" & "EMR" & "' " _

Case "NFA"
strSQL = strSQL & "WHERE tblCases.Program = " & "'" & "NFA" & "' " _

Case "CMA"
strSQL = strSQL & "WHERE tblCases.Program = " & "'" & "CMA" & "' " _

End Select

'Order Statement
strSQL = strSQL & "ORDER BY tblCases.PetLastName; "
 
K

Klatuu

A simpler solution:

Dim strSQL As String
strSQL = "SELECT tblCases.PetLastName, tblCases.PetFirstName AS
Petitioner, " _
& "tblCases.Program FROM tblCases WHERE tblCases.Program = '" _
& Me.FindProgram & "' ORDER BY tblCases.PetLastName; "
 

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