I like looking at code, so i reviewed and made some changes to the code:
Code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim var_dsu '<<< this is a variant?? Why isn't it a string??
Dim var_ap_date As Date '<<<< I added "As Date"
Dim var_first_search As String
Dim var_act_control As String
Dim ctl As Control '<<< changed from Object to Control
If IsNull(Me.assoc_ID_cmb) Then
MsgBox "Please enter an associate name.", vbExclamation
Exit Sub
ElseIf IsNull(Me.cred_dsu_cmb) Then
MsgBox "Please enter a DSU.", vbExclamation
Exit Sub
ElseIf IsNull(Me.ap_date_txt) Then
MsgBox "Please enter a date.", vbExclamation
Exit Sub
Else
Set db = CurrentDb
Set rs = db.OpenRecordset("assoc_prod_qry", dbOpenDynaset)
var_dsu = Me.cred_dsu_cmb
var_ap_date = Me.ap_date_txt
For Each ctl In Me.Controls
Select Case ctl.ControlType
' Case acTextBox And ctl.Name <> "ap_date_txt" And ctl.Name <> "auditor_ID_txt" '<<< cannot do this See Help for proper syntax
Case acTextBox
If ctl.Name <> "ap_date_txt" And ctl.Name <> "auditor_ID_txt" Then
If Right(ctl.Name, 4) = "_txt" And Not IsNull(ctl) Then '"_txt" must be @ the end of the name of the text box and can't be null
var_act_control = Left((ctl.Name), Len(ctl.Name) - 4)
var_first_search = "assoc_id = '" & Me.assoc_ID_cmb & "' and code_id = '" & var_act_control & "' and credited_dsu_id = '" & var_dsu & "' and ap_date = #" & var_ap_date & "#"
'findfirst ALWAYS starts from the beginning of the rs
' If Not rs.BOF Then
' rs.MoveFirst
' End If
rs.FindFirst var_first_search
If rs.NoMatch Then 'add new record
rs.AddNew
rs!code_id = var_act_control
rs!ap_date = Me.ap_date_txt
rs!assoc_id = Me.assoc_ID_cmb
rs!credited_dsu_id = Me.cred_dsu_cmb
rs!qty_of_prod = ctl.Value
rs!assoc_entered = Forms!navigation_form.assoc_id_txt
rs!entry_timestamp = Now()
rs.Update
Else
'If found, update the qty of the unit of production on the row where the match was found
rs.Edit
rs!qty_of_prod = ctl.Value
rs!assoc_modified = Forms!navigation_form.assoc_id_txt
rs!modified_timestamp = Now()
rs.Update
End If
' ctl = Null
End If
End If
End Select
Next ctl
rs.Close
Set rs = Nothing
' db.Close '<<< didn't open, so can't close it
Set db = Nothing ' <<< but you can destroy it
MsgBox "Your entry has been submitted.", vbInformation
End If
Exit_submit_cmd_Click:
Exit Sub
err_handler:
MsgBox "Error number " & Err.Number & ": " & Err.Description
rs.Close
db.Close
Set rs = Nothing
Resume Exit_submit_cmd_Click
End Sub
The main change was the "SELECT CASE" syntax was wrong. See Help.
but it only edits the first row when match is found, the rest of the rows it leaves untouched
I'm curious as to why only the first line gets updated when it finds a match and leaves the other rows unchanged.
If .NoMatch indicates record(s) found using .FindFirst, will there be more than one record that meets the criteria? If so, then you will have to add code to move to the next match (.FindNext) and update the fields.
If you expect one or more records would meet the .FindFirst criteria, you might think about using an update query to change all of the records at once, rather than using looping thru the records using .FindNext.
My $0.02.....
No warranty implied or expressed....
Your mileage may vary...
Valid until I blink......