Results 1 to 3 of 3
  1. #1
    Thumbs is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    25

    Referencing another field in current record when looping through recordset

    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

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The UPDATE needs WHERE clause. However, not sure you need the UPDATE at all. Why not just edit the WtyExpireDate field of the current record of the recordset? http://msdn.microsoft.com/en-us/libr...ffice.12).aspx

    Not every year has 365 days, some have 366.

    Instead of Counter, try:

    While Not rst.EOF
    ...
    Wend
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Thumbs is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    25
    Quote Originally Posted by June7 View Post
    The UPDATE needs WHERE clause. However, not sure you need the UPDATE at all. Why not just edit the WtyExpireDate field of the current record of the recordset? http://msdn.microsoft.com/en-us/library/bb243797(v=office.12).aspx

    Not every year has 365 days, some have 366.

    Instead of Counter, try:

    While Not rst.EOF
    ...
    Wend
    June, thanks for the reply. I had already changed counter to an EOF method after writing this post. Actually, I rewrote the code from scratch to try to get a fresh approach.

    I used your suggestion about updating the recordset (which actually runs about 20x faster than the UPDATE query). After a few more hours of beating my head against the wall with things not working, and even trying to revert back to an UPDATE query for the purpose of the WHERE clause (as records were getting updated that I didnt want to update), I was eventually able to get it working with the use of a join in the recordset...blew the doors right off the hinges for me.

    As far as the number of days in a year, I don't need to be specific to the day as to when the warranty runs out. An error margin of a couple of days here and there is acceptable for these purposes.

    Thanks again for all your help. Here is what actually wound up working for me. I call the sub OnOpen so that every time the form is opened, the data it displays will be accurate.

    Code:
    Private Sub AddWtyDate()
    
    Dim rst As DAO.Recordset
    Dim AddYears As String
    Dim passPart As String
    Dim sSQL As String
    Dim AddDays As Integer
    Dim ExpDate As String
    
    Set rst = CurrentDb.OpenRecordset("SELECT CincomPartNumber, ReceivedDate, WtyExpireDate FROM AssetData INNER JOIN WarrantyInfo ON WarrantyInfo.ContractorPartNum = AssetData.CincomPartNumber WHERE NOT(CincomPartNumber) Is Null")
    
    'Check to see if the recordset actually contains rows
    If Not (rst.EOF And rst.BOF) Then
        rst.MoveFirst
        Do Until rst.EOF = True
          
        ' Match part number of current record with part number in WarrantyInfo table, return value from table, and strip all but the first character.
        passPart = rst!CincomPartNumber
        AddYears = Nz(DLookup("[WarrantyType]", "WarrantyInfo", "[ContractorPartNum] = '" & passPart & "' "), 0)
        AddYears = (Left(AddYears, 1))
        
        'Check if returned character is numeric and if required fields are not null. If so, run UPDATE to populate WtyExpireDate.
        If IsNumeric(AddYears) And Not IsNull([ReceivedDate]) And Not IsNull([CincomPartNumber]) Then
            AddDays = AddYears * 365
            rst.Edit
            rst!WtyExpireDate = rst!ReceivedDate + AddDays
            rst.Update
        End If
    
            'Move to the next record.
            rst.MoveNext
        Loop
    Else
        MsgBox "There are no records in the recordset."
    End If
    
    rst.Close
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 04-12-2012, 05:09 PM
  2. Referencing a query field
    By rcrobman in forum Queries
    Replies: 5
    Last Post: 04-29-2011, 04:06 PM
  3. Join A Recordset to the current db Table
    By mjellis in forum Programming
    Replies: 0
    Last Post: 08-10-2010, 02:44 PM
  4. Replies: 1
    Last Post: 11-13-2009, 03:03 AM
  5. referencing each record
    By grgerhard in forum Forms
    Replies: 1
    Last Post: 11-11-2006, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums