my tables have a column type varchar(5)
value = 10100
on a form I created five checkbox control
corresponding to the value in column
if the value =1 checkbox= true
and vice versa, the value = 0 checkbox = false
how I can upload the data forms correctly
by any code to help.
thanks a lot

2 Years
Discussion Span
Last Post by Reverend Jim

Make a loop form 1 to length of the text and create a dynamic checkBox and show on your form checked/unchecked.

Dim x  as integer

for i as integer= 1 to x.length

'do your codes here


hope it can give you a way.


You can do

Dim val As String = "10010"

For i As Integer = 0 To 4
    Dim cbx As CheckBox = Me.Controls("CheckBox" & i)
    cbx.Checked = val.Substring(i - 1, 1) = "1"

Just replace the Dim statement with the statements to retrieve the data from the database. I don't know if your problem is with separating the values or with getting the info from the database. If the latter then please give a little more detail about the database (table name, etc). A generic query would be

SELECT myfield FROM tablename
 WHERE someOtherField = `some value`

I presume you have some way to limit the number of records returned to just one.

Edited by Reverend Jim


below is a detailed image of the example
I have not any code to be reasonable
can you help me fix code

Dim ch As String = Nothing
                Dim vl As String = Nothing
                Dim kq As String = Nothing
                For i As Integer = 0 To tb.Rows.Count - 1
                    ch = tb.Rows(i).Item("other")
                    For Each vl In ch
                        kq = vl

                        For j As Integer = 1 To ch.Length
                            If j = 1 And kq = 1 Then
                                ckb1.Checked = True
                                ckb1.Checked = False
                            End If
                            If j = 2 And kq = 1 Then
                                ckb2.Checked = True
                                ckb2.Checked = False
                            End If
                            If j = 3 And kq = 1 Then
                                ckb3.Checked = True
                                ckb3.Checked = False
                            End If
                            If j = 4 And kq = 1 Then
                                ckb4.Checked = True
                                ckb4.Checked = False
                            End If

Posting here a sample example codes which could solve your problem

 Dim x As String = "10111"

        For i As Integer = 1 To x.Length
            For Each control As Control In Me.Controls
                If TypeOf control Is CheckBox Then
                    If control.Name = "CheckBox" & CStr(i) Then
                        Dim chb As CheckBox = CType(control, CheckBox)
                        chb.Checked = Val(Mid(x, i, 1))
                        Exit For
                    End If
                End If

Hope it can help you.


@Sanu - note that Me.Controls("CheckBox" & i) will directly reference the control (but it has to be cast to CheckBox first).

@fugio - you have two examples here that do what you seeem to be asking. If you don't understand something specific please ask instead of just posting code.

Edited by Reverend Jim

Votes + Comments
Thanks for pointing out an unnecessary loop of controls.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.