+ Reply to Thread
Results 1 to 5 of 5

Excel VBA to update table in Access 2013

Hybrid View

  1. #1
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Excel VBA to update table in Access 2013

    Hi Sumif, here's some basic code to get you started as far as opening a database connection, and deleting records from the table: (requires you to add a reference to Microsoft ActiveX Data Objects Library (I use 6.1 generally))

    Its just a quick sample code, and I didn't test but use code like this quite often so it ought to work.

    Sub sumif_access_update()
    
    Dim dbs As ADODB.Connection
    Dim rset1 As ADODB.Recordset
    Dim sql As String
    Dim i As Long
    Dim ws As Worksheet
    
    
    Set dbs = New ADODB.Connection
    
    'open connection to the database:
    dbs.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=X:\mydatabase.accdb"
    
    sql = "Delete * from [myTABLE]" 'just setting the string to the sql command that will delete all records from the table
    dbs.Execute sql 'executes the sql statement
    sql = "SELECT * frm [myTABLE]" 'recycling the sql variable to now be used to open a recordset
    Set rset1 = New ADODB.Recordset
    'open recordset to do the updating
    rset1.Open dbs, sql, adOpenStatic, adLockOptimistic
    Set ws = Worksheets("Sheet1")
    'loop through data rows
    For i = 2 To 50 'just random numbers, representing the first and last row of data
        With rset1
            .AddNew 'inserts new record
            .Fields(0) = ws.Range("A" & i).Value 'instead of ".fields(0)" you could also use "![FieldName]"
            .Fields(1) = ws.Range("B" & i).Value
            'etc for all fiels in db
            .Update 'updates new record
        End With
    Next i
    rset1.Close
    Set rset1 = Nothing
    dbs.Close
    Set dbs = Nothing
    
    End Sub
    Last edited by Arkadi; 12-21-2015 at 10:38 AM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  2. #2
    Registered User
    Join Date
    07-27-2015
    Location
    England
    MS-Off Ver
    Office 2010
    Posts
    40

    Re: Excel VBA to update table in Access 2013

    Thanks Arkadi, i'll work with your code & post back how i get on.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Excel 2013, can't access VBA help
    By Danino in forum Excel General
    Replies: 4
    Last Post: 07-06-2015, 10:31 AM
  2. Excel 2013 and Access 2013 as Pivot Table External Data Source
    By Grimnebulin in forum Access Tables & Databases
    Replies: 1
    Last Post: 05-27-2015, 02:13 PM
  3. Trying to update an access table from Excel based on unique ID. rst.Update not working
    By Newbie0924 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-22-2015, 04:15 PM
  4. Replies: 1
    Last Post: 12-02-2014, 05:46 AM
  5. [SOLVED] update table in access from excel using sql
    By specialk9203 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-19-2014, 03:44 PM
  6. Update Access Table from Excel using DAO
    By vish2025 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-15-2010, 06:02 AM
  7. Update & Append Access Table Using Excel Macro - ADO
    By erock24 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-19-2010, 04:49 AM

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