Clearing bound checkboxes on a subform in access - ms-access

i have a form with a subform query on it. The subform query has a row of checkboxes (bound to the table) for users to select multiple items. When the users press a command button another query is run to search for all checkboxes that are marked true and then open another form. I am trying to make it so that when another command button is pressed, all of the checkbox values on the subform query change to false. So far the code that i am using only changes the last selected checkbox to false. How do I get it to change all of the values to false?
Dim ctl As Control
For Each ctl In Me.SuppliesQuerySubform.Controls
If ctl.ControlType = acCheckBox Then
If ctl.Value <> False Then
ctl.Value = False
End If
End If
Next ctl

You have two options:
1) Update the records using SQL. Something like UPDATE tblItems SET IsSelected = FALSE WHERE ... (Replace tblItems with your table and IsSelected with the field the checkbox is bound to.) Then you should refresh your subform with .Requery
2) Use RecordsetClone. This is actually a copy of the records shown in your subform which you can loop through and update.
With Me.RecordSetClone
.MoveFirst
Do While .Eof = false
.Edit
.Fields("IsSelected") = FALSE
.Update
.MoveNext
Loop
End With

Related

option button (radio button) on each record of continuous form

This is for a project in MS Access 2016. I'd like to implement a radio button toggle across all records displaying in a continuous form. only one record can have the toggle "on" so when it's clicked it has to reset the previous record's flag to off. I'm only finding help on radio button usage to select from multiple values of a field for one record, usually on single form. Can this be done?
Radio button must be bound to a yes/no field then use UPDATE action SQL to make sure all records except current have field set to 0. Need a unique record identifier field such as autonumber.
Private Sub Option29_Click()
CurrentDb.Execute "UPDATE tablename SET fieldname = 0 WHERE ID <>" & Me.ID
End Sub
Be aware that in a multi-user database users can conflict with each other and another solution will be needed. Depends what you need to do with the selected record.
Use the RecordSetClone of the form:
Private Sub Active_AfterUpdate()
Dim Records As DAO.Recordset
Me.Dirty = False
If Me!Active.Value = True Then
Set Records = Me.RecordsetClone
Records.MoveFirst
While Not Records.EOF
If Records!Id.Value <> Me!Id.Value Then
If Records!Active.Value = True Then
Records.Edit
Records!Active.Value = False
Records.Update
End If
End If
Records.MoveNext
Wend
Records.Close
End If
End Sub
You can do it in easy way:
1- Add to your table a new Yes/No Field ( e.g named CurrentRec)
2- Put the Radio Button and link it to this field ( e.g named rdCurrRec)
3- For the radio button Set 'Enabled' property to False and 'Locked' property to 'True'. This will disable Click - Double Click and Hover response.
4- Put the following code
Dim PreBookmark(4) As Byte 'Save old bookmark
Private Sub Form_Current()
If Not Me.NewRecord Then 'Make sure not on new record
Me.rdCurrRec = True
If (PreBookmark(0) <> 0 Or PreBookmark(1) <> 0 Or PreBookmark(2) <> 0 Or PreBookmark(3) <> 0) Then
Me.RecordsetClone.Bookmark = PreBookmark
Me.RecordsetClone.Edit
Me.RecordsetClone.Fields("CurrentRec") = False
Me.RecordsetClone.Update
End If
PreBookmark(0) = Me.Bookmark(0)
PreBookmark(1) = Me.Bookmark(1)
PreBookmark(2) = Me.Bookmark(2)
PreBookmark(3) = Me.Bookmark(3)
Else 'If new record remove bullet from previous record
If (PreBookmark(0) <> 0 Or PreBookmark(1) <> 0 Or PreBookmark(2) <> 0 Or PreBookmark(3) <> 0) Then
Me.RecordsetClone.Bookmark = PreBookmark
Me.RecordsetClone.Edit
Me.RecordsetClone.Fields("CurrentRec") = False
Me.RecordsetClone.Update
End If
End If
End Sub
Private Sub Form_Close()
'Remove the mark before exiting to avoid it appear next time to open the form in two places
If Not Me.NewRecord Then
If (PreBookmark(0) <> 0 Or PreBookmark(1) <> 0 Or PreBookmark(2) <> 0 Or PreBookmark(3) <> 0) Then
Me.RecordsetClone.Bookmark = PreBookmark
Me.RecordsetClone.Edit
Me.RecordsetClone.Fields("CurrentRec") = False
Me.RecordsetClone.Update
End If
End If
End Sub
This will not iterate all the data so for large number of records it will not delay the response as it works only on two records

How to notify when a checkbox is checked

Change on checkbox is not recognized when the check is performed by custom function.
We have form where checkboxes can be clicked. In this case the record will be added to a list, which will be updated with a save function. And there is mentioned function to check all check boxes, but this does not behave like when i click an individual checkbox.
code "Select all"
Private Sub Befehl83_Click()
With Me.RecordsetClone
.MoveFirst
Do Until .EOF
.Edit
!visited = True
.update
.MoveNext
Loop
End With
End Sub
clicked on checkbox
Private Sub chkVisited_Click()
If Not visitedList.Contains(Me.Form.Recordset.Fields("trainingMeasureID").Value) Then
visitedList.Add Me.Form.Recordset.Fields("trainingMeasureID").Value
Else
visitedList.Remove Me.Form.Recordset.Fields("trainingMeasureID").Value
End If
End Sub
Currently the state of checkboxes is not saved when used the select-all function. Only when a checkbox is selected individually.
It should save correctly in both cases.
Here's a not so ideal solution:
Private Sub Befehl83_Click()
With Me.Recordset
.MoveFirst
Do Until .EOF
.Edit
.Fields("visited") = True
.Update
Call chkVisited_Click()
.MoveNext
Loop
End With
End Sub
This will actively update the record set, you'll actually see the active record moving in the form.
Another solution would be to rebuild your code and do your updates directly to the underlying data source based on your form's recordset. Then update your list accordingly.
According to the docs, RecordsetClone is a read-only copy of the form's recordset. https://learn.microsoft.com/en-us/office/vba/api/access.form.recordsetclone
Use the AfterUpdate event:
Private Sub Befehl83_Click()
With Me.RecordsetClone
.MoveFirst
Do Until .EOF
If !visited.Value = False Then
.Edit
!visited.Value = True
.update
End If
.MoveNext
Loop
End With
End Sub

Checking textboxes if empty to control visibility of subform

I have a form with textboxes for adding new Event to the database. Some of the textboxes are mandatory while the rest not. Within this also is a subform for adding details of participants.
I don't want the subform to be visible if one or more of the mandatory fields have nothing. I have tried with this but the subform stays invisible regardless whether or not there is data in the mandatory fields:
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If Not (IsNull(ctl.Value) Or ctl.Value = "") Then
Me.subfrmParticipants.Visible = False
Else
Me.subfrmParticipants.Visible = True
End If
End If
Next ctl
You're using a double negative instead of just checking for NULL value in your text box.
Change this
If Not (IsNull(ctl.Value) Or ctl.Value = "") Then
To This:
If (IsNull(ctl.Value) Or ctl.Value = "") Then
Then if the text box is assigned a database NULL value or is empty,
then the hide subform command will kick in

Access VBA: Editing a RecordSet in a continuous subform

I am trying to write a function to loop through the records in a continuous subform and clear the value in a specific field (Entity_Under_Consideration, which is a lookup field represented by a combobox on the subform) for every record.
The following does not work. It doesn't throw any errors either. Can anyone see where I am going wrong?
Public Function clearEUCData(subform As Control)
'take a clone of the subform's recordset
Dim entityRecSet As Recordset
Set entityRecSet = subform.Form.Recordset.Clone()
'if there are any records in the subform...
If entityRecSet.RecordCount > 0 Then
'start with the first record
entityRecSet.MoveFirst
'iterate through each row, clearing the data in the EUC field
Do Until entityRecSet.EOF
With entityRecSet
.Edit
Entity_Under_Consideration = 0
.Update
End With
entityRecSet.MoveNext
Loop
End If
'close and purge the cloned recordset
entityRecSet.Close
Set entityRecSet = Nothing
End Function
You will have to be more explicit:
With entityRecSet
.Edit
.Fields("Entity_Under_Consideration").Value = 0
.Update
End With

MS Access FIlter on subform changing values of other fields

I have a datasheet subform with a combobox name Loan_ID_cbo. Whenever I update the filter via the combobox, the subform is updating the Loan ID (primary key) with the selected Laon ID from the subform, thereby changing the data on the table.
I would like to only filter this data and not allow the filter to edit the data on the table. How can I prevent this from happening?
Here is my VBA code for the After_Update event:
Private Sub Loan_ID_cbo_AfterUpdate()
Application.Echo False
Me.Filter = "MyKey = '" & Loan_ID_cbo & "'"
Me.FilterOn = True
If Loan_ID_cbo = "" Then
Me.Filter = ""
Me.FilterOn = False
End If
Application.Echo True
End Sub
If I understand correctly, your combobox is in the subform datasheet. Thus the combobox appears on each row.
It does happen probably because your combobox is bound to the field Mykey. So changing the combobox do change the value of Mykey.
You should not make a filtering combobox out of the myKey field in the subform, you should make an unbound combobox in the parent form :
In the subform, delete this combobox, and add a new textbox and bind it to Mykey.
On your mainform, create a new combobox and use this one to filter your subform. So the code would be something like :
Private Sub Loan_ID_cbo_AfterUpdate()
Application.Echo False
Me.subformname.Form.Filter = "MyKey = '" & Loan_ID_cbo & "'"
Me.subformname.Form.FilterOn = True
If Loan_ID_cbo = "" Then
Me.subformname.Form.Filter = ""
Me.subformname.Form.FilterOn = False
End If
Application.Echo True
End Sub