Results 1 to 16 of 16

Updating multiple records in Access table using excel VBA

Threaded View

  1. #3
    Forum Contributor
    Join Date
    01-21-2012
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Updating multiple records in Access table using excel VBA

    Sorry I didn't add the proper code tags to my post earlier. Here is my code for trying to update records in an Access table from excel.

    Application.ScreenUpdating = False ' Prevents screen refreshing.
    
    Dim cn As ADODB.Connection
    Dim DatabaseName As String
    Dim TableName As String
    Dim rs As ADODB.Recordset
    Dim StartRow As Long
    
    Set rs = New ADODB.Recordset
    
    DatabaseName = "DB_test1.mdb" ' Enter your database name here
    TableName = "tblPopulation" ' Enter your Table name here
    StartRow = 2 ' Enter row in sheet to start reading records
    
    
    Dim shtSheetToWork As Worksheet
    Set shtSheetToWork = ActiveWorkbook.Worksheets("Table download")
    '********
    
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Admin\DB_test1.mdb;Persist Security Info=False;"
    
    
    rs.Open TableName, cn, adOpenKeyset, adLockOptimistic
    
    
    Do While Range("A" & StartRow).Value > 0
    
    ' repeat until first empty cell in column A
    With rs
    .Update ' create a new record
    ' add values to each field in the record
    .Fields("Country") = Range("B" & StartRow).Value 'The Country
    .Fields("Yr_1950") = Range("C" & StartRow).Value 'The Year 1950
    .Fields("Yr_2000") = Range("D" & StartRow).Value 'The Year 2000
    .Fields("Yr_2015") = Range("E" & StartRow).Value 'The Year 2015
    .Fields("Yr_2025") = Range("F" & StartRow).Value 'The Year 2025
    .Fields("Yr_2050") = Range("G" & StartRow).Value 'The Year 2025
    .Fields("Region") = Range("H" & StartRow).Value 'The Region
    .Fields("MyField") = Range("I" & StartRow).Value 'New Field
    
    ' add more fields if necessary...
    .Update ' stores the new record
    End With
    StartRow = StartRow + 1 ' next row
    Loop
    rs.Close
    Set rs = Nothing
    cn.Close
    MsgBox "Data has been updated!", vbInformation
    Set cn = Nothing
    Last edited by anthony1312002; 02-27-2012 at 11:50 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1