I have come to my witts end on this one. I'm trying to loop through a recordset, performing an action on each record as I loop through. As part of that action, I want to reference another field on the current record each time it loops.
I have pasted my current code below.
As of right now, it is updating every field in the column with the same value, based on the value of "CincomPartNumber" in the first record. Ultimately, I need to have each record updated independently, as they will have varying years I need to add to the date based on the warranty info in another table (pulled by Dlookup). I suppose the real question I should be asking is how to properly pull the value from another field in the same record as I loop through the recordset?
Also, as part of my testing, I am trying to force the variable value that would normally be passed as an argument to the function. This is just for testing at the moment, though I ultimately intend on running this code on the On Open event. Its not the prettiest at the moment, I just want to get it working.
Thanks in advance for all your help!
Code:
Private Function AddWtyDate(passPart As String)
' Declare variables
Dim dbs As Database
Dim rst As Recordset
Dim Counter As Integer
Dim AddYears As String
' create recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("SELECT CincomPartNumber, ReceivedDate, WtyExpireDate FROM AssetData WHERE NOT(CincomPartNumber) Is Null", dbOpenDynaset)
' find number of records in recordset
Counter = rst.RecordCount
While Counter > 0
With rst
' Match part number of current record with part number in WarrantyInfo table, return value from table, and strip all but the first character.
passPart = !CincomPartNumber
AddYears = DLookup("[WarrantyType]", "WarrantyInfo", "[ContractorPartNum] = '" & passPart & "' ")
AddYears = Left(AddYears, 1) * 365
'Check if returned character is numeric. If so, run UPDATE.
If IsNumeric(AddYears) Then
sSQL = "UPDATE AssetData"
sSQL = sSQL & " SET AssetData.WtyExpireDate = ([ReceivedDate] + '" & AddYears & "');"
DoCmd.RunSQL sSQL
End If
End With
' moves to next record
rst.MoveNext
Counter = Counter - 1
' restart loop
Wend
End Function