Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using SQL in VBA with "OR" Criteria 1

Status
Not open for further replies.

jdgreen

Technical User
Mar 21, 2001
144
US
I have some code I am trying to get to run that goes through a list of e-mail addresses and adds the addresses to a string based on specific criteria. What is happening is the SQL that specifically calls out a value works, but the SQL that references a form for the value does not pull the work. In other words, it doesn't add the e-mail addresses when the values match the forms.

strSQL = "SELECT tblEmail.Email FROM tblEmail WHERE ((tblEmail.LocationType = ' & [Forms]![frmAlertLog]![sfrmAlertLog].[Form]![Location] & ' Or tblEmail.LocationType = 'Warehouse') And (tblEmail.Plant = ' & [Forms]![frmAlertLog]![sfrmAlertLog].[Form]![MfgPlant] & ' Or tblEmail.Plant = 'Plymouth' Or tblEmail.Plant = 'All'));"

strEmail = ""
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

rs.MoveFirst
Do Until rs.EOF
If strEmail = "" Then
strEmail = rs!email
Else
strEmail = rs!email & ";" & strEmail
End If
rs.MoveNext
Loop

Any help would be appreciated.

John Green
 
Try

strSQL = "SELECT tblEmail.Email FROM tblEmail WHERE ((tblEmail.LocationType = '" & [Forms]![frmAlertLog]![sfrmAlertLog].[Form]![Location] & "' Or tblEmail.LocationType = 'Warehouse') And (tblEmail.Plant = '" & [Forms]![frmAlertLog]![sfrmAlertLog].[Form]![MfgPlant] & "' Or tblEmail.Plant = 'Plymouth' Or tblEmail.Plant = 'All'));"


I've added some ". I think I've put them all in.

Hope this helps.
 


Hi,

I cn't directly answer your question BECAUSE I don't know what you INTEND for your logic.

When you combind AND and OR statements, you need to be VERY CAREFUL at the ORDER OF EXECUTION.

For instance each of these statements will yield DIFFERENT results...
[tt]
A OR B And C
(A OR B) And C
A OR (B And C)
[/tt]
So you tell me what your intent is.
Code:
strSQL = "SELECT tblEmail.Email FROM tblEmail 

WHERE ((tblEmail.LocationType = ' & [Forms]![frmAlertLog]![sfrmAlertLog].[Form]![Location] & ' 
  
  Or tblEmail.LocationType = 'Warehouse') 

  And (tblEmail.Plant = ' & [Forms]![frmAlertLog]![sfrmAlertLog].[Form]![MfgPlant] & '

  Or tblEmail.Plant = 'Plymouth' 

  Or tblEmail.Plant = 'All'));"
I THINK that what you want is
Code:
Where (LocationType= a OR LocationType= b)

  AND

      (Plant=x OR Plant =y OR Plant =z)

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top