Results 1 to 4 of 4
  1. #1
    TinaCa is offline Advanced Beginner
    Windows Vista Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    87

    Update number field

    I am migrating data from an old dbase IV database. I have updated all my dates etc. My issue is this that the existing invoice data does not have an invoice Id. The new table in Access does have an invoice ID. I am looking for some suggestions for updating the InvoiceId field (think 1001, 1002, etc). Can I do this with an update query or??? there are about 57,000 rows in the table so i don't want to do it manually

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    What is the datatype of the invoiceID field in your Access table?

    What field in your old dataset uniquely identifies the invoice?

  3. #3
    TinaCa is offline Advanced Beginner
    Windows Vista Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    87
    The InvoiceId is a number field and the old database was not normalized so the only unique combination might be the invoice date and customer id.

    Tina

  4. #4
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    The best way that I can think of is to create a recordset based on the records that have no InvoiceID number and loop through them in code and update them with sequential invoice numbers. A straight update query will not work because there is no way to increment the InvoiceID # as the update query runs.

    I have attached an example database. If you look in tblInvoice, 2 records have an invoiceID and 2 records do not. (close the table when done viewing the records). Open the form frmRunUpdate and click the button. After the code runs, reopen the table and you will see that the two records that did not have an invoice now have an invoice ID.

    The code behind the button on the form is as follows:

    Code:
    Private Sub cmdRunUpdate_Click()
    On Error GoTo Err_cmdRunUpdate_Click
    
    Dim cnn1 As ADODB.Connection
    Set cnn1 = CurrentProject.Connection
    
    'set up the recordset
    Dim myRS As New ADODB.Recordset
    myRS.ActiveConnection = cnn1
    
    'set up the query to get the records that need to be updated
    Dim mySQL As String
    mySQL = "SELECT * FROM tblInvoice WHERE InvoiceID is Null"
    
    'set up a variable for incrementing the invoice ID
    
    Dim myInvoice As Long
    'set the variable to the highest invoice ID # in the invoice table and then add 1 to it
    myInvoice = DMax("invoiceID", "tblInvoice") + 1
    
    Debug.Print myInvoice
    
    'open the recordset
    myRS.Open mySQL, , adOpenDynamic, adLockOptimistic
    
    
    'loop through the records and update each with a new invoice ID number
    Do Until myRS.EOF
        
        With myRS
            !InvoiceID = myInvoice
            .Update
        End With
        
        'increment myInvoice for the next invoice record
        myInvoice = myInvoice + 1
        
        'move to the next record
        myRS.MoveNext
    Loop
    
    MsgBox "update complete"
    
    myRS.Close
    Set myRS = Nothing
    
    
    
    Exit_cmdRunUpdate_Click:
        Exit Sub
    
    Err_cmdRunUpdate_Click:
        MsgBox Err.Description
        Resume Exit_cmdRunUpdate_Click
        
    End Sub
    Attached Files Attached Files

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

Similar Threads

  1. Replies: 2
    Last Post: 03-30-2012, 07:39 AM
  2. Replies: 12
    Last Post: 03-17-2012, 04:46 AM
  3. Replies: 1
    Last Post: 08-31-2011, 04:03 PM
  4. Replies: 4
    Last Post: 04-18-2011, 07:18 AM
  5. Update field in table after update on a subform
    By jpkeller55 in forum Queries
    Replies: 3
    Last Post: 09-30-2010, 08:02 PM

Tags for this Thread

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