user12261791
user12261791

Reputation:

How to save data from checkbox value to database using button

I'm trying to save data from checkbox to database

 Private Sub Check2_Click()
 If Check2.Value = True Then
  Check2.Caption = "OK"
  ElseIf Check2.Value = False Then
   Check2.Caption = "not ok"
  End If
 End Sub

    Private Sub Form_Load()
   con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source 
   =C:\Users\MIS02\Documents\checkstrial.accdb;Persist Security Info = false"
   rs.Open "Select * from tableCheck", con, adOpenDynamic, adLockPessimistic
   DTPicker1.Value = Date
   End Sub

 Private Sub addBtn_Click()

  rs.AddNew
  rs.Fields("CheckItem").Value = Label2.Caption
  rs.Fields("Itemno").Value = Label17.Caption
  rs.Fields("Criteria").Value = Label38.Caption
  rs.Fields("AMafter").Value = Check2.Caption

  rs.Update
  MsgBox "Data add successfully"

The only fields that being saved in my database are labels I want the output to be saved in database if the checkbox is checked "OK" and if not then "NOT OK"

Upvotes: 2

Views: 206

Answers (2)

Étienne Laneville
Étienne Laneville

Reputation: 5031

In your Check2_Click, use vbChecked instead of True and vbUnchecked instead of False.

You can also add an IIF statement when updating your record:

rs.Fields("AMafter").Value = IIF(Check2.Value = vbChecked, "OK", "NOT OK")

Upvotes: 1

Étienne Laneville
Étienne Laneville

Reputation: 5031

If your AMafter field is defined as Boolean (Yes/No in Access), you need to set a Boolean value. Try the following:

rs.Fields("AMafter").Value = (Check2.Value = vbChecked)

Upvotes: 0

Related Questions