Solved

convert check box value to bit type in SQL DB

Posted on 2007-04-04
3
811 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

815 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now