I have problem that while not terribly complex, has me stumped.
I have a combobox that contains either just the person's name, or the person's name and the company. For example, the combobox could contain:
Craig Garnett
Craig Garnett, Plumbers R Us
Billy Bob
Joe Bob
Joe Bob, Electrical Services
I use the value in the combobox (cmbCust.text) as the where criteria in a select statement (SQL Express). Here is the select statement so far:
Select cusCompany, cusID, CusName = case
when cusCompany = '' then CusName
else CusName + ', ' + cusCompany End
from Customer WHERE CusName = '" & cmbCust.Text & "'
AND cusStatus = '" & varActive & "'"
This select statement almost works when just cusName is populated, but cusCompany is blank. However, I need to be able to compare a column with cmbCust when only the cusName is populated or when the cusCompany and cusName is populated. Basically I need to be able to compare either:
cusFName + ', ' + cusCompany with cmbCust.text (if there is a value in cusCompany),
- or -
cusFName with cmbCust.text (if there is no value in cusCompany).
Can I use a case statement in the WHERE clause? I have tried that as well, but can't get it to work. I am trying to do something like the following:
Select cusCompany, cusID, CusFName = case when cusCompany = '' " & _
" then CusFName else CusFName + ', ' + cusCompany End from Customer " & _
" WHERE (Case WHEN cusCompany = '' then CusFName = '" & cmbCust.Text & "' " & _
" else CusFName + ', ' + cusCompany = '" & cmbCust.Text & "' End) " & _
"AND cusStatus = '" & varActive & "'
The error message I get when adding the CASE within the WHERE clause that there is "incorrect syntax near the '='".
Thanks!
I have a combobox that contains either just the person's name, or the person's name and the company. For example, the combobox could contain:
Craig Garnett
Craig Garnett, Plumbers R Us
Billy Bob
Joe Bob
Joe Bob, Electrical Services
I use the value in the combobox (cmbCust.text) as the where criteria in a select statement (SQL Express). Here is the select statement so far:
Select cusCompany, cusID, CusName = case
when cusCompany = '' then CusName
else CusName + ', ' + cusCompany End
from Customer WHERE CusName = '" & cmbCust.Text & "'
AND cusStatus = '" & varActive & "'"
This select statement almost works when just cusName is populated, but cusCompany is blank. However, I need to be able to compare a column with cmbCust when only the cusName is populated or when the cusCompany and cusName is populated. Basically I need to be able to compare either:
cusFName + ', ' + cusCompany with cmbCust.text (if there is a value in cusCompany),
- or -
cusFName with cmbCust.text (if there is no value in cusCompany).
Can I use a case statement in the WHERE clause? I have tried that as well, but can't get it to work. I am trying to do something like the following:
Select cusCompany, cusID, CusFName = case when cusCompany = '' " & _
" then CusFName else CusFName + ', ' + cusCompany End from Customer " & _
" WHERE (Case WHEN cusCompany = '' then CusFName = '" & cmbCust.Text & "' " & _
" else CusFName + ', ' + cusCompany = '" & cmbCust.Text & "' End) " & _
"AND cusStatus = '" & varActive & "'
The error message I get when adding the CASE within the WHERE clause that there is "incorrect syntax near the '='".
Thanks!