How can I compile an sql query statement from multiple variables

For example:

strMyQuerySatement = select * from Tickets where Type = strType  and StartDate > strStartDate and EndDate < strEndDate and Paid = blnPaid or Ticket# = strTicketNum
LVL 10
jasfoutAsked:
Who is Participating?
 
Brian CroweDatabase AdministratorCommented:
you forgot to close the single quotes around strType

strQuery = "SELECT * FROM Tickets WHERE Type = '" + strType + "'"
0
 
bruintjeCommented:
Hello jasfout,

depends on what you're looking at for variables but something like
--------
Dim strMyQuerySatement As String
strMyQuerySatement = "select * from Tickets where Type = " & strType  & " and StartDate > '" & strStartDate & "' and EndDate < '" & strEndDate & "' and Paid = " & blnPaid & " or Ticket# = " & strTicketNum
--------

hope this helps a bit
bruintje
0
 
Brian CroweDatabase AdministratorCommented:
strMyQueryStatement = "SELECT * FROM Tickets WHERE Type = '" + strType + "' AND StartDate > '" + strStartDate + "' AND EndDate < '" + strEndDate + "' AND Paid = " + cint(blnPaid).tostring + " OR Ticket# = '" + strTicketNum + "'"

this will also depend on how you have formated strStartDate and strEndDate.  Basically you need to include single quotes around string and date values.
0
 
jasfoutAuthor Commented:
well to simplify I am for now just trying one variable
here is where I have the  trouble

dim strType as string = "ALS"
strQuery = "SELECT * FROM Tickets WHERE Type = '" + strType

...throws an exception

strQuery = "SELECT * FROM Tickets WHERE Type = 'ALS'"

...works fine so I beleive it to be a syntax in this line
0
 
jasfoutAuthor Commented:
>you forgot to close the single quotes around strType

Thank you, thats what I needed
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.