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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using FINDFIRST to find a boolean value??

Status
Not open for further replies.

CPro

Programmer
Dec 14, 2005
2
US
In the command,

Data1.Recordset.FindFirst "Left='" + CStr(SearchLeft) + "'

SearchLeft is a boolean variable, as well as the value of the data field, "Left". The code above does not work. How can one use the FINDFIRST command to find a value in the database whose datatype is boolean???

Any help would be greatly appreciated.

Frank D. Jimenez
admin@cabinetpro.com
(541) 776-9133

 
When you say "does not work", it's not quite clear what you mean. What I can tell you is this:
Code:
dim x as boolean
x = True
debug.print cstr(x) ' will return "-1"
x = False
debug.print cstr(x) ' will return "0"
If you're basing your logic on assumptions that differ from the above, then your code will of course not work as you want it to.

I bring this up because it's a common error to believe that True evaluates to 1 rather than -1. If you want to force True to evaluate to 1 instead of -1, you could do this:
Code:
Data1.Recordset.FindFirst "Left='" + CStr(abs(cint(SearchLeft))) + "'

HTH

Bob
 
Or more simply

Data1.Recordset.FindFirst "Left=" & SearchLeft

which resolves to "Left = True" or "Left = False
 
>a common error to believe that True evaluates to 1 rather than -1

Actually (and you probably knew this) True is represented as 1 in most other languages; VB just likes to be different ... (and to be fair VB actually considers anything that is NOT False to be True, i.e anything other than 0 is True)
 
Thank you very much for your responses, but I am still a little unsure of something:

If the variable, SearchLeft, evaluates to -1, because it is boolean and is set to "True"; and the data in the "Left" field of the Recordset is also set to "True", and therefore also evaluates to -1, then why would the line below not find the record?

Data1.Recordset.FindFirst "Left='" + CStr(SearchLeft) + "'

Maybe this line assumes the field is a string, rather than the boolean data type, which is what the field was actually defined as.

I noticed that you, Bob, changed the datatype to CInt, before changing the datatype to CStr. I haven't tried this yet, but I wonder if the following would work:

Data1.Recordset.FindFirst "Left='" + CStr(CInt(SearchLeft)) + "'


Frank D. Jimenez

admin@cabinetpro.com
 
Internally, Boolean data types are numerics (specifically they are BIT fields). The specification

Data1.Recordset.FindFirst "Left='" + CStr(SearchLeft) + "'

will attempt to compare a numeric field (Left) with a string argument ('-1'). Such comparison with either raise a "Unmatched Data Type" error or will return nothing because the field in question is not a text field as indicated by the single quote delimiters.
 
<True is represented as 1 in most other languages; VB just likes to be different
I remember my first VB application, wherein I was attempting to evaluate a check box by comparing its value (under the assumption that it was boolean) to a 1 or 0 integer value from somewhere else. It proved to be an interesting exercise for a Paradox developer. :)

<and the data in the "Left" field of the Recordset is also set to "True", and therefore also evaluates to -1
I wouldn't take that as a given. It is true in Access, however. Two questions: are you using Access? What is the data type of the field called "Left"?

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top