String for query

radek225

Registered User.
Local time
Today, 09:06
Joined
Apr 4, 2013
Messages
307
I have a query which working fine:
Code:
strSQL = "Select [nazwa], [id_wycena_pre], [Format_(X)], [Format_(Y)], [kolory p], [kolory t], [nazwa_id], [iloscstron], [numerarkusza], [naklad_pracy], [Kolory P], [Kolory T] from tblGoraZleceniaNowaWyceny where [id_wycena_pre]=" & Forms!frmWycenyObszarRoboczy!ID_wycena_pre & " and [nazwa]='" & "środek" & "' or [nazwa]='" & "okładka" & "'"
Set rst = CurrentDb.OpenRecordset(strSQL)...

But now I would like to add next condition to this query
Code:
where [kolory p] is not null or [kolory t] is not null
- these fields are string
I don't know how to wrtie this condition and add to my exiting strSQL string:/
 
I tried
Code:
strSQL = "Select [nazwa], [id_wycena_pre], [Format_(X)], [Format_(Y)], [kolory p], [kolory t], [nazwa_id], [iloscstron], [numerarkusza], [naklad_pracy], [Kolory P], [Kolory T] from tblGoraZleceniaNowaWyceny where [id_wycena_pre]=" & Forms!frmWycenyObszarRoboczy!ID_wycena_pre & " and ([nazwa]='" & "środek" & "' or [nazwa]='" & "okładka" & "') And ([Kolory P] <> "" or [Kolory T]  <> "")"
Set rst = CurrentDb.OpenRecordset(strSQL)

But It works only if [kolory P] is not null
 
Nz(Kolory P]) <> ""

Nz function is Null to Zero.
 
Lots of stuff here:

1. An empty string ("") is not null. Think of "" as an empty box, think of NULL as that box not even existing. There is a difference.

If you want to lump them both together I suggest you test the length of the field:

WHERE LEN([FieldName])>0

Both Null and "" will fail that test and not be returned

2. AND and OR don't play well together. When you start mixing ANDs and ORs in your WHERE clause you need to use parenthesis around the fields that work together. For example A & B below can return different records:

A: WHERE ([Field1]=True AND [Field2]=True) OR ([Field3]=True)
B: WHERE ([Field1]=TRUE) AND ([Field2]=True OR [Field3]=True)

Field1=True, Field2=False & Field3=True will not show up in A, but it will show up in B. When you mix ANDs and ORs you need to employ parenthesis to make the parts that should be evaluated together stay together.

You haven't given us enough information about your query for us to write it for you--I don't know what condtions go with what conditions. I can't tell what should be ANDs and what should be ORs and where parenthesis should go.

Hopefully my explanation is enough for you to do it on your own, if not please demonstrate with sample data what you hope to accomplish.
 

Users who are viewing this thread

Back
Top Bottom