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

SqlServer datatype(Bit) 3

Status
Not open for further replies.

tzamora

Programmer
Apr 15, 2002
27
US
I have several columns of datatype(Bit).
When I view my recordset items through the watch window I can see that this fields are boolean (they should). When I display this data on a datagrid they show up as (-1) or (0).
when I equate a textbox with this field, it shows up as "True" Or "False". Query analyzer displays (1) or (0).

I want to use this field as a checkbox, need help
 
Hi there,

I'm assuming you want a checkbox that if the value is 1 it should be ticked and if it is 0 it shouldn't be? When you loop through your recordset you just need to add a check

If rs.field = 1 then
chkBox.value = vbchecked
else
chkBox.value = vbunchecked
end if

hope this helps

Transcend
 
The SQL server type bit is cast into a VB boolean variable (which is why it shows as true/false in VB).
The datagrid shows -1 because if you cast a VB boolean into a numeric value it is defined as -1 (which is a bit weird). You can confirm that by : MsgBox CInt(True)
Transcends solution will FAIL because rs.field will never be 1 (it will be true (~-1) or false (~0), but slightly modifyed it will work (if Rs.field then ...)
Somewhat shorter is:
check1.value = asb(cint(Rst.Fields("YourBitField"))) Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
For asb please read ABS
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
ups. thanks johnwm it should of course have read:

Check1.Value = Abs(CInt(Rst.Fields("YourBitField")))
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
<G> Sunaj, I thought it was just me having trouble with knotted fingers! </G>
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Since ADO translates the database Bit type to a VB Boolean type, you can do this:
Code:
chkLeftHanded.Value = IIF(adoRS.Fields(&quot;IsLeftHanded&quot;).Value, vbChecked, vbUnchecked)
If your database field is nullable, you'll have to take further steps:
Code:
If IsNull(adoRS.Fields(&quot;IsLeftHanded&quot;)) Then
   chkLeftHanded.Value = vbUnchecked
Else
   chkLeftHanded.Value = IIF(adoRS.Fields(&quot;IsLeftHanded&quot;).Value, vbChecked, vbUnchecked)
End If
Hope this helps. Note that I'm not relying on the default properties of the Checkbox or ADO Field object ... in .NET they go away and you must specify them all the way down.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top