Link to home
Start Free TrialLog in
Avatar of mlcktmguy
mlcktmguyFlag for United States of America

asked on

Dynamically building Where Condition

In my VB6.0 app, based on user selections from combo boxes I am trying to dynaically build a 'where' clause conditionally based on what is selected in the combo boxes (there are two of them).

I have verified that the fields 'cboCustomer.ItemData(cboCustomer.ListIndex)' and 'cboItem.ItemData(cboItem.ListIndex)' both contain numeric values if selected by the user.  The problem is that as I build the where clause, a double quote is appended at the end, which is incorrect.  I am not purposely putting it there but when I show the contents the where clause looks like:

"Where ClientID = 16"

based on my statements

    wkVal = cboCustomer.ItemData(cboCustomer.ListIndex)
    whereCond = whereCond & " ClientID = " & wkVal

I want it to look like:  "Where ClientID = " 16

How do I keep the double quote from moving where I put it (prior to the 16) to where the system puts it (after the 16)?

My full logic looks like this:


anyCond = False
whereCond = "WHERE"
myFilter = ""

  If cboCustomer.ListIndex > -1 Then
    If anyCond Then
      whereCond = whereCond & " AND "
      myFilter = myFilter & ", "
    End If
    wkVal = cboCustomer.ItemData(cboCustomer.ListIndex)
    whereCond = whereCond & " ClientID = " & wkVal
    myFilter = myFilter & "Customer: " & cboCustomer.Text
    anyCond = True
  End If

  If cboItem.ListIndex > -1 Then
    If anyCond Then
      whereCond = whereCond & " AND "
      myFilter = myFilter & ", "
    End If
    whereCond = whereCond & " ItemID = " & cboItem.ItemData(cboItem.ListIndex)
    myFilter = myFilter & "Item: " & cboItem.Text
    anyCond = True
  End If

  rs.Open "SELECT * FROM qryPriceSheet" & whereCond, cn
Avatar of AlThePal7
AlThePal7

If you want quotes to appear inside a string, you have to enter the quotes twice e.g.

textbox1.text = "This string includes "" quotes"

Textbox1 will displayed the string

This string includes " quotes

Can you give an example of the complete string you want to end up with, along with the contents of your listbox?
Avatar of mlcktmguy

ASKER

As I mentioned above, this is what I am ending up with:

"Where ClientID = 16"

This is what I want to end up with:

"Where ClientID = " 16

"I have verified that the fields 'cboCustomer.ItemData(cboCustomer.ListIndex)' and 'cboItem.ItemData(cboItem.ListIndex)' both contain numeric values if selected by the user".

I also include the logic I am using to build the string:

    wkVal = cboCustomer.ItemData(cboCustomer.ListIndex)
    whereCond = whereCond & " ClientID = " & wkVal

I have verified that wkVal contains the numeric value 16.

What I odn't understand is based on the statement:

    whereCond = whereCond & " ClientID = " & wkVal

there shold be no double quote at the end but there is.
What you are doing is adding a value to the end of a string.  VB will convert the number to the string and join the 2 strings together.  Whenever it shows a string it will surround it in quotes.  If you really want a string with double quotes before and after the 'Where ClientID = ' part, you will have to use the following:

    whereCond = """Where"
    wkVal = cboCustomer.ItemData(cboCustomer.ListIndex)
    whereCond = whereCond & " ClientID = """ & wkVal
SOLUTION
Avatar of Brownhead
Brownhead

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I appreciate the responses but I am still getting 'Error in the From Clause' on the statement:

rs.Open "SELECT * FROM qryPriceSheet" & whereCond, cn

I tried:

   whereCond = """Where"
    wkVal = cboCustomer.ItemData(cboCustomer.ListIndex)
    whereCond = whereCond & " ClientID = """ & wkVal

and I tried

    whereCond = """" & whereCond & " ClientID = """ & wkVal

no luck yet.

Any other ideas?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm going to split the point because you all gave me an education even though the last comment was the clincher.

It turns out that after trying all of the suggestions, I ended up with my orginal logic with one small change.

Instead this one statement:

rs.Open "SELECT * FROM qryPriceSheet " & whereCond, cn

I loaded my sql statement into a separate 'sqlString' string variable, built the Wherecond string just like I started out and used the following two statement without changing anything else:

  sqlString = sqlString & whereCond
   rs.Open sqlString, cn

It ran the first time I tried it, no moe misplaced quotes etc...

Thanks again for the input and helping me through this frustrating situation.