Results 1 to 3 of 3
  1. #1
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839

    Update a table through VBA

    I am trying to update the [Units].COID1 using VBA.



    Code:
    Private Sub COLevel_Click()
    Dim rv As DAO.Recordset
    Dim A As Integer, B As String, C As String, COID1 As Integer
    
    Set rv = CurrentDb.OpenRecordset("Units")
    
    If rv.BOF And rv.EOF Then
                    rv.Close
                Else
                    rv.MoveLast
                    rv.MoveFirst
        Do While Not rv.EOF
            
            For A = 1 To 10
                B = Chr(64 + A)
                C = "'" & B & " *'"
            
            If rv!Netbase Like C And rv!Echelon = "CO" Then
                        COID1 = A
            Else
                        COID1 = 0
            End If
    
            rv.Edit
            rv!COID1 = COID1
            rv.Update
            rv.Close
            
            Next A
                  rv.MoveNext
        Loop
    
    End If
    
    Set rv = Nothing
    
    End Sub
    I error on: "Runtime Error '3420'; Object invalid or no longer set."
    If rv!Netbase Like C And rv!Echelon = "CO" Then

    What I am trying to do is query the table where the NetID begins with the letter A through J using chr(64 + A) and Echelon ="CO" using the Like Command and adding in B. This should output to the corresponding record in Column COID1.

    Any suggestions?

    Thanks
    Last edited by Thompyt; 10-08-2016 at 09:06 PM. Reason: Updated code to get Like Command correct.

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You shouldn't close the recordset until you aren't using it any more.
    Code:
        Do While Not rv.EOF
            
            For A = 1 To 10
                B = Chr(64 + A)
                C = "'" & B & " *'"
            
            If rv!Netbase Like C And rv!Echelon = "CO" Then
                        COID1 = A
            Else
                        COID1 = 0
            End If
    
            rv.Edit
            rv!COID1 = COID1
            rv.Update
            rv.Close
            
            Next A
                  rv.MoveNext
        Loop
    Try
    Code:
    Private Sub COLevel_Click()
        Dim rv As DAO.Recordset
        Dim A As Integer, B As String, C As String, COID1 As Integer
    
        Set rv = CurrentDb.OpenRecordset("Units")
    
        If rv.BOF And rv.EOF Then
            rv.Close
        Else
            rv.MoveLast
            rv.MoveFirst
            Do While Not rv.EOF
    
                For A = 1 To 10
                    B = Chr(64 + A)
                    C = "'" & B & " *'"
    
                    If rv!Netbase Like C And rv!Echelon = "CO" Then
                        COID1 = A
                    Else
                        COID1 = 0
                    End If
    
                    rv.Edit
                    rv!COID1 = COID1
                    rv.Update
    
                Next A
                
                rv.MoveNext
            Loop
    
        End If
    
        rv.Close
        Set rv = Nothing
    
    End Sub

  3. #3
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Thanks Steve!

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

Similar Threads

  1. Replies: 4
    Last Post: 04-27-2016, 04:36 PM
  2. Update Table after Record Update with Form
    By speciman_A in forum Forms
    Replies: 25
    Last Post: 10-31-2014, 01:00 PM
  3. Replies: 7
    Last Post: 09-21-2012, 11:09 AM
  4. Replies: 1
    Last Post: 02-08-2012, 04:50 PM
  5. Replies: 1
    Last Post: 01-22-2011, 12:23 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