How can I uncheck an entire column of checkboxes? - vba

How can I uncheck an entire column of checkboxes?
I have a field in each record in my access table that has a checkbox, as time goes on I check boxes, but at the end to reset I would like to put a button on the split form that will uncheck all of the boxes.
It is the second field "Acct" on the table "tblData1" and I want to control it from the form "frmMain" Using a button "cmdResetAcct"
Thank you in advance!

Run an UPDATE query.
CurrentDb.Execute "UPDATE tblData1 SET Acct = False WHERE Acct <> False"
The WHERE clause makes it more efficient because only the checked rows are written.

You can use the recordset you already have directly in the OnClick event of your button:
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
If rs.RecordCount > 0 Then
rs.MoveFirst
End If
While Not rs.EOF
If rs!Acct.Value = True Then
rs.Edit
rs!Acct.Value = False
rs.Update
End If
rs.MoveNext
Wend
Set rs = Nothing
This is very fast, and your form will update instantly.

Related

Access VBA - AfterUpdate() Recordset edit leads to "No current Record 3021" even if Record exists

I have an issue where I was not able to find a solution by tinkering and searching similiar problems already solved on the web.
First:
Im'using a form to enter a component ID (CID). After entering the Number in the Field I'm making use of the AfterUpdate() function to use the CID to look up a corresponding Prod ID in another Table. The Matching Prod ID from the other Table will then be entered automaticaly into the field. The field is a multivalued Field.
Here is the code:
Private Sub Form_AfterUpdate()
Dim rst As DAO.Recordset
Dim rstChld As DAO.Recordset2
Dim tmpVar
If Me.Dirty Then
Me.Dirty = False
End If
If StrComp(Me.Part, "Device") = 0 Then
tmpVar = DLookup("[Device Prod]", "subLookTblCIDDevice", "[CID Device] = '" & Me.CIDDevice & "'")
Set rst = Me.Recordset
If Me.Recordset.RecordCount = 0 Then
rst.MoveFirst
End If
If Not (rst.BOF And rst.EOF) Then
If rst.Updatable Then
rst.Edit
Set rstChld = rst!Prod.Value
rstChld.AddNew
rstChld.Fields(0) = tmpVar
rstChld.Update
rst.Update
Me.Bookmark = rst.LastModified
Set rst = Nothing
Set rstChld = Nothing
End If
End If
End If
End Sub
If the record exists and im Changing the CID - Everything is working fine the corresponding field gets its correct coresponding ID.
But if it is a new Record and the first of the recordset I got the error message
No Current Record - 3021
It can be mitigated by adding
If Me.Recordset.RecordCount = 0 Then
rst.MoveFirst
End If
But if its a new Record and not the first record it changes the previous record.
I have tried to use .AddNew instead of .Edit. This will create a new record after the one which has been updated.
I would be really glad if someone could have a look at it. I dont understand whats going on. why it is jumping before or after the record.
Thx and Cheers

How to load a multiline recordset into an Access form with VBA

I'm runing a query (which is returning the correct data) but how can I loop the recordset and add each line in the form with VBA? Because it's currently adding the last line repeatedly.
Private Sub buscarReservasFio()
Dim strSQL As String
strSQL = "SELECT * FROM tblReservasFio WHERE fk_solicitacao = " & "'" & arrParameters(0) & "'"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
If rs.BOF Or rs.EOF Then
'to-do insert
MsgBox ("Insert")
Else
Do Until rs.EOF
DoCmd.GoToRecord , , acNewRec
Me.ds_funcao = rs.Fields("ds_funcao").Value
Me.fk_titulo = rs.Fields("fk_titulo").Value
Me.nr_cor = rs.Fields("nr_cor").Value
Me.nr_peso = rs.Fields("nr_peso").Value
Me.dt_liberacao = rs.Fields("dt_liberacao").Value
Me.dt_baixa = rs.Fields("dt_baixa").Value
rs.MoveNext
Loop
End If
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
Apparently, form is bound to table but your code is populating UNBOUND controls. That's the only way I can replicate your output - which should be expected. The reason multiple rows of same data show is because controls (BOUND or UNBOUND) repeat for each record and since there is only one set of controls, the same data shows in UNBOUND controls for all rows - last record from recordset loop. Step debug and watch data change on form. The GoToRecord action is meaningless because no data is input to BOUND controls. The result is same without that line.
In order to display multiple records from table, bind controls to fields. Then, options to modify nr_peso field with new calculated value when form first opens:
run an UPDATE action SQL
open a recordset of form's RecordsetClone and loop through that recordset and change value of nr_peso field - changes in RecordsetClone will immediately reflect in the form
code physically moves to each record on form and modifies field value

Create a new record from existing one with changes to two fields in Access dB [duplicate]

Background:
I have a subform (datasheet) I update using a datasheet checkbox afterupdate event:
I clone the clicked record & Insert into the referenced table via an Insert Query
I modify the original record to differentiate from the Inserted record via an Update Query
To avoid RecordLock complaints, I have inserted: SendKeys "+{Enter}", True after each of the above to save the updates - is there a better way to do this??
Next I need to requery the subform to display the newly inserted record AND use a bookmark (?) to retain the cursor position of the original record (inserted record will be adjacent). Subform datasheet recordset is based on a query.
Question:
From within the subform's afterupdate event, using Forms![ParentForm].[SubForm].Form.Requery does not produce an error, but does open the code window with the line highlighted in Yellow.
Next attempt, from within the subform's afterupdate event, I have attempted to set focus to a control on the ParentForm BEFORE using Forms![ParentForm].[SubForm].Form.Requery, but I get a similar ~silent error~ as noted above.
What is the proper way to REQUERY a subform from within the same subform?
Thanks!
You are making it way too hard for yourself. Here is how to copy a record from a button click. No locks, no queries, and auto update of the form:
Private Sub btnCopy_Click()
Dim rstSource As DAO.Recordset
Dim rstInsert As DAO.Recordset
Dim fld As DAO.Field
If Me.NewRecord = True Then Exit Sub
Set rstInsert = Me.RecordsetClone
Set rstSource = rstInsert.Clone
With rstSource
If .RecordCount > 0 Then
' Go to the current record.
.Bookmark = Me.Bookmark
With rstInsert
.AddNew
For Each fld In rstSource.Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
ElseIf .Name = "SomeFieldToHandle" Then
rstInsert.Fields(.Name).Value = SomeSpecialValue
ElseIf .Name = "SomeOtherFieldToHandle" Then
rstInsert.Fields(.Name).Value = SomeOtherSpecialValue
ElseIf .Name = "SomeFieldToSkip" Then
' Leave empty or with default value.
ElseIf .Name = "SomeFieldToBlank" Then
rstInsert.Fields(.Name).Value = Null
Else
' Copy field content.
rstInsert.Fields(.Name).Value = .Value
End If
End With
Next
.Update
' Go to the new record and sync form.
.MoveLast
Me.Bookmark = .Bookmark
.Close
End With
End If
.Close
End With
Set rstInsert = Nothing
Set rstSource = Nothing
End Sub
If the button is placed on the parent form, replace Me with:
Me!NameOfYourSubformControl.Form
Between AddNew and Update you can modify and insert code to adjust the value of some fields that should not just be copied.

MS Access: Looping through subform records causes Error '3021'

I have a main form and a subform that displays several records. When a checkbox is checked on the main form, I want all "BoxLblTime" and "Material Arrived" fields on the subform to be updated. This is the vba code that is executed when the checkbox is clicked:
Private Sub MaterialArrived_chkbx_Click()
Dim temp As Variant
Dim tempString As String
Dim ctl As Control
'If checkbox is checked
If Forms("JOBS Form").Controls("MaterialArrived_chkbx").Value < 0 Then
Dim rs As Object
'Get records of subform
Set rs = Forms("JOBS Form").[Order Form].Form.Recordset
'Loop until end
Do While Not rs.EOF
rs.Edit
'Update the two fields
rs![Material arrived] = True
rs!BoxLblTime = Now()
rs.Update
rs.MoveNext
Loop
Set rs = Nothing
End If
End Sub
There is some unusual behavior when using this code:
1) When the checkbox on the main form is checked, the two fields are updated in the subform. But if I uncheck the checkbox on the subform and then recheck the main form checkbox, the subform checkbox stays unchecked.
2) When the checkbox on the main form is checked, the two fields are updated. But if I uncheck the checkbox on the subform, move to a new set of subform records (next or back) and then check the main form checkbox, I get the error: '3021' No current record.
Why is this unusual behavior happening?
EDIT:
Here is my code using the update query approach:
Private Sub MaterialArrived_chkbx_Click()
If Forms("JOBS Form").Controls("MaterialArrived_chkbx").Value < 0 Then
With CurrentDb().QueryDefs("Update Orders")
.Parameters("[Material Arrived]").Value = True
.Parameters("[BoxLblTime]").Value = Now()
.Execute dbFailOnError
End With
Forms("JOBS Form").Form.Requery
End If
End Sub
But I'm getting "Item not found in collection" error.
I would suggest an alternative approach.
Create an update query and pass a Boolean parameter indicating the material arrived value.
'Call update query
Private Sub MaterialArrived_chkbx_Click()
With CurrentDb().QueryDefs("Update Orders")
.Parameters("[prmMaterialArrived]").Value = Me.MaterialArrived_chkbx.Value
.Parameters("[prmID]").Value = Me!ID
.Execute dbFailOnError
End With
Me.[Order Form].Form.Requery
End Sub
'SQL
PARAMETERS [prmMaterialArrived] Bit, [prmID] Long;
UPDATE T
SET T.[Material arrived] = [prmMaterialArrived], T.BoxLblTime = Now()
WHERE (((T.ID)=[prmID]));

Showing some rows in textboxes on a Form using vba

I have a query that returns for example 5 rows, I want to show thes these rows(fields) in textboxes. but it only shows me one record. I have also set Default view Property of my form into Continuous form. her is my code:
Private Sub List2_DblClick(Cancel As Integer)
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SELECT XValue, YValue,Wert FROM tb_DCM_Daten WHERE (FzgID=" & Forms!frm_fahrzeug!ID & " AND [Name]='" & List2.Value & "')")
If rst.RecordCount <> 0 Then
Do While Not rst.EOF
Text8.SetFocus
Text8.Text = rst.Fields("XValue").Value
Text10.SetFocus
Text10.Text = rst.Fields("YValue").Value
Text11.SetFocus
Text11.Text = rst.Fields("Wert").Value
rst.MoveNext
Loop
End If
End Sub
how can I do that?
As it stands now, your code would loop through the Recordset, put the values from the first record into the textboxes, then overwrite those values with the ones from the second record, and overwrite those with the values from the third record, and so on.
It sounds like you want a subform. Have a look at the Office tutorial here for more information.