Hello.
My VB.Net project is connected with MS Access. Whenever input in VB, it will save in Access. I don't have any problem with textbox but I don't know what to do with checkbox and radiobutton.
I want, when checkbox or radiobutton is checked in VB, it will checked also in Access.
This is my code for textbox:

Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim inc As Integer

Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click

        con.ConnectionString = "PROVIDER=Microsoft.Jet.Oledb.4.0; Data source =C:\Documents and Settings\~LaiLi~\My Documents\Visual Studio 2005\Projects\Party\Party\bin\Debug\Party.mdb"
        sql = "SELECT * FROM Planner"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Planner")
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsnewrow As DataRow

If Not DoesAccountExist(EventComboBox.Text) Then
                dsnewrow = ds.Tables("Planner").NewRow()

                dsnewrow.Item("EventName") = EventComboBox.Text
                dsnewrow.Item("ClientName") = ClientTextBox.Text
                dsnewrow.Item("VenueName") = VenueTextBox.Text

               ds.Tables("Planner").Rows.Add(dsnewrow)
                da.Update(ds, "Planner")

                MessageBox.Show("Data saved!")
                With Me
                    EventComboBox.DisplayMember = 0
                    ClientTextBox.Clear()
                    VenueTextBox.Clear()
                End With
End If
End Sub

What should I change to make Access save checkbox and radiobutton?

Dani AI

Generated

Good question, and +1 to for pointing you at Access Yes/No. A couple of gotchas to make this rock-solid in WinForms VB.NET: group your RadioButtons inside the same container (GroupBox/Panel) so only one is ever checked, and if you are using a DataSet with a new/empty table, call FillSchema so your Yes/No columns come through as Boolean instead of String. That way you can assign .Checked without manual conversion.

If your radio choices represent alternatives (e.g., Male/Female), do not store them as two Yes/No fields. Save one value (text or small code) in a single column. Example using parameters (OleDb uses positional ? parameters, so order matters):

Using con As New OleDb.OleDbConnection("your connection string here"), _
      cmd As New OleDb.OleDbCommand("INSERT INTO Planner (IsUrgent, Gender) VALUES (?,?)", con)

    cmd.Parameters.Add("?", OleDb.OleDbType.Boolean).Value = chkUrgent.Checked
    cmd.Parameters.Add("?", OleDb.OleDbType.VarWChar, 1).Value =
        If(rbMale.Checked, "M", If(rbFemale.Checked, "F", DBNull.Value))

    con.Open()
    cmd.ExecuteNonQuery()
End Using

Notes for and :

  • If both Male and Female end up saved, your schema is likely using two Yes/No columns. Prefer a single Gender field (Text 1) as shown above. If you must keep two booleans, set only one from the selected radio and force the other to the opposite value.
  • When working through a DataGridView, bind it to your DataTable, set the gender column as Text (or use a ComboBox column), then call BindingSource.EndEdit() and da.Update(ds) to persist. Setting DataGridView1.Item(5, ...) alone does not hit the database until the bound row is updated.

Recommended Answers

All 4 Replies

On Access you must define the fields as Yes/No

Then

dsnewrow.Item("Urgent") = checkBoxUrgent.Checked
dsnewrow.Item("Option1") = radioButton1.Checked

Hope this helps

It worked. Thank you.

please help me with that kind of problem .. i already made the field with yes/no
but i dont know how to connect the radiobutton on it.

If RadioButton1.Checked = True Then

DataGridView1.Item(5, DataGridView1.CurrentRow.Index).Value = True

Else
RadioButton1.Checked = False
End If

can you write the code for radiobutton, i like to save it in ms access

for example the radiobutton is male and female, when i clicked the male , in access , both are saved .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.