Results 1 to 1 of 1

Update Access Table from VBA Excel

Threaded View

  1. #1
    Valued Forum Contributor saravnepali's Avatar
    Join Date
    01-14-2019
    Location
    Sydney Australia
    MS-Off Ver
    2010
    Posts
    447

    Update Access Table from VBA Excel

    I cam across this code to update access table from VBA.

    It works fine but this need to have the values supplied on the code.
           " VALUES ('John','Smith',42)"
    Is it possible to amend the code to have the values from Excel Cells say Range A1 to A3

    Sub INSERT_to_Table()
    '!!!!!! Add Reference to Microsoft ActiveX Data Objects 2.x Library!!!!!!
     
    Dim strConnectString        As String
    Dim objConnection           As ADODB.Connection
    Dim strDbPath               As String
    Dim strTblName              As String
    Dim strSQL                  As String
    Dim ErrorMessage
     
    'Set database name and DB connection string--------
    strDbPath = ThisWorkbook.Path & "\TestDB.accdb"
    '==================================================
    strConnectString = "Provider = Microsoft.ACE.OLEDB.12.0;" & _
                        "Data Source=" & ThisWorkbook.Path & "\TestDB.accdb;" & _
                        "Jet OLEDB:Database Password='****';"
                        
                        '& _
                      '  "Mode=Share Exclusive"
    '        strConnectString = "Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDbPath & _
    '    ";MS Access;PWD=asdf"
    'Connect Database; insert a new table
    Set objConnection = New ADODB.Connection
    On Error GoTo ErrorMessage
    
    With objConnection
        .Open strConnectString
        .Execute "INSERT INTO TEST" & _
                " (FirstName,LastName,Age)" & _
                " VALUES ('John','Smith',42)"
                MsgBox "Records Updated." & nowtime
    End With
     
    Set objConnection = Nothing
     
    Exit Sub
     
    ErrorMessage:
    MsgBox "OPIS: " & Err.Description & Chr(10) & _
        "NUMER: " & Err.Number
    Set objConnection = Nothing
     
    End Sub
    My actual spreadsheet has 30 columns, so wanted to have the values received from the excel sheet

    Also, would be nice if we could avoid to enter the column header on the code
                " (FirstName,LastName,Age)" & _
    as it has 30 columns but vba to automatically get from column 1 to 30
    Last edited by saravnepali; 07-04-2020 at 07:21 AM.
    If you think someone helped you, click on the "* Add Reputation" as a way to say thank you.

    If your problem is solved, go to Thread Tools and select Mark This Thread Solved

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 02-15-2016, 05:47 PM
  2. Excel VBA to update table in Access 2013
    By SUMIF in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-21-2015, 01:04 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