Solved

convert check box value to bit type in SQL DB

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

863 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

24 Experts available now in Live!

Get 1:1 Help Now