Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

convert check box value to bit type in SQL DB

Posted on 2007-04-04
3
812 Views
Last Modified: 2013-12-25
How can I convert a check box value, which is True or False to 1 or 0 and save to SQL server data table, the field type is "bit ". Thanks
0
Comment
Question by:Skaugen
3 Comments
 
LVL 29

Expert Comment

by:leonstryker
ID: 18851399
The standard way is to use an If statement.

How is the checkbox connected to your database?

Leon
0
 
LVL 7

Expert Comment

by:prosh0t
ID: 18851920
a bit type is essentially a 'bool'.  If you're using ADO and you have a recordset, just do something like this:

rs!SQLCheckedField = (MyCheckBox.Value = vbChecked)

We'll probably need more details about how you're working with your database, though.





0
 
LVL 16

Accepted Solution

by:
JohnBPrice earned 500 total points
ID: 18863588
I do this frequently, so I have a few routines I like to make it simple with these you do

rs!SQLCheckedField = IntToBool(MyCheckBox.Value)

or visa versa

MyCheckBox.Value = BoolToInt(rs!SQLCheckedField)


Public Function BoolToInt(bitfield As Variant) As Integer
    'return vbchecked or unchecked based on bitfield
    If IsNull(bitfield) Then
        BoolToInt = vbGrayed 'I use null to indicate neither checked nor unchecked, aka unknown
    ElseIf bitfield Then
        BoolToInt = vbchecked
    Else
        BoolToInt = vbUnchecked
    End If
End Function

Public Function IntToBool(checkfield As Integer) As Boolean
    If checkfield <> 0 Then
        IntToBool = True
    Else
        IntToBool = False
    End If
End Function
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question