The last post we had resolve a problem with nulls and empty strings!
Now I have annother problem with Logic Operator "AND":
this is the problem:
Example:
This is the fields I have in the Database Access:
Code:
Raca dt_morte1 dt_morte2 dt_morte3
especie Goro Goro
especie car Goro
Now I want extract the fields don't have nulls or empty strings where the "Raca" be "especie" and the dt_morte1,dt_morte2,dt_morte3="Goro"
the code:
Code:
SQL = "select Sum(IIF(dt_morte1 Is Null or dt_morte1='', 0, 1) ) as total1,Sum(IIF(dt_morte2 Is Null or dt_morte2='', 0, 1) ) as total2,Sum(IIF(dt_morte3 Is Null or dt_morte3='', 0, 1) ) as total3, from " & ano
SQL = SQL & " WHERE Raca = '" & especie & "'" AND dt_morte1 = '" & Goro & "' AND dt_morte2 = '" & Goro & "' AND dt_morte3 = '" & Goro & "'
the problem is:
With the "AND dt_morte2 = '" & Goro & "' AND dt_morte3 = '" & Goro & "' " the code return a wrong value.He return's "0".
Only with the first "AND dt_morte1 = '" & Goro &'" the code return the right value "1"
Something wrong with the expression?Why the code just return the right value with one "AND"?
Thanks
Last edited by sacramento; Jun 5th, 2005 at 12:47 PM.
Looks like you want OR, and not AND in that expression.
Code:
SQL = "select Sum(IIF(dt_morte1 Is Null or dt_morte1='', 0, 1) ) as total1,Sum(IIF(dt_morte2 Is Null or dt_morte2='', 0, 1) ) as total2,Sum(IIF(dt_morte3 Is Null or dt_morte3='', 0, 1) ) as total3, from " & ano
SQL = SQL & " WHERE Raca = '" & especie & "'" AND (dt_morte1 = '" & Goro & "' OR dt_morte2 = '" & Goro & "' OR dt_morte3 = '" & Goro & ")'
*** Read the sticky in the DB forum about how to get your question answered quickly!! ***
Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".
Here is the code with something alterations,but the syntax with AND and OR don't return the desired value!He returns all fields...he doesn't make the distinction of the fields with the word "Goro"!
Code:
SQL = _
"select" & _
" Sum(IIF(dt_morte1 Is Null or dt_morte1='', 0, 1) ) as total1, " & _
" Sum(IIF(dt_morte2 Is Null or dt_morte2='', 0, 1) ) as total2, " & _
" Sum(IIF(dt_morte3 Is Null or dt_morte3='', 0, 1) ) as total3, " & _
" Sum(IIF(dt_morte4 Is Null or dt_morte4='', 0, 1) ) as total4, " & _
" Sum(IIF(dt_morte5 Is Null or dt_morte5='', 0, 1) ) as total5, " & _
" Sum(IIF(dt_morte6 Is Null or dt_morte6='', 0, 1) ) as total6, " & _
" Sum(IIF(dt_morte7 Is Null or dt_morte7='', 0, 1) ) as total7, " & _
" Sum(IIF(dt_morte8 Is Null or dt_morte8='', 0, 1) ) as total8 " & _
" from " & ano & _
" WHERE especie = 'especie'" & _
" And dt_morte1 = 'Goro'" & _
" Or dt_morte2 = 'Goro'" & _
" Or dt_morte3 = 'Goro'" & _
" Or dt_morte4 = 'Goro'" & _
" Or dt_morte5 = 'Goro'" & _
" Or dt_morte6 = 'Goro'" & _
" Or dt_morte7 = 'Goro'" & _
" Or dt_morte8 = 'Goro'"
After A lot of tryings a real need some help here because I don't now Why the SQL don't extract that I want!
I had add the project for better understanding!
Please run the project and tell me if sommething is wrong ok?
Exactly what are you trying to count? The number of times the word "Goro" appears in each dt_morte field or the number of records which has at least one dt_morte field equal to "Goro"
Using the sample records in the original post what is the total count that you are expecting?
I suspect you want this SQL statement.
VB Code:
SQL = _
"select" & _
" Sum(IIF(dt_morte1='Galado', 1, 0) ) as total1, " & _
" Sum(IIF(dt_morte2='Galado', 1, 0) ) as total2, " & _
" Sum(IIF(dt_morte3='Galado', 1, 0) ) as total3, " & _
" Sum(IIF(dt_morte4='Galado', 1, 0) ) as total4, " & _
" Sum(IIF(dt_morte5='Galado', 1, 0) ) as total5, " & _
" Sum(IIF(dt_morte6='Galado', 1, 0) ) as total6, " & _
" Sum(IIF(dt_morte7='Galado', 1, 0) ) as total7, " & _
" Sum(IIF(dt_morte8='Galado', 1, 0) ) as total8 " & _
" from " & ano & _
" WHERE especie = 'Canários de Côr'"
[Edit] I update the SQL statement to reflect your project code.
Last edited by brucevde; Jun 5th, 2005 at 11:26 AM.
I try count all the records(dt_morte1...8) (without nulls and empty strings) where the field especie = "Canários de Côr",and the fields(dt_morte1...8) contain the word "Galado"!
Yes, I looked at your project and sample database. Using my posted SQL statement a total of 5 is shown in the listview. Is that the number you are expecting (I am still unclear on what you are trying to calculate)?
[Edit] I changed the SQL statement in my previous post to reflect your project. Try it again.