Results 1 to 2 of 2
  1. #1
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154

    copying contents of an excel vba array into existing access database table

    Hi,

    I have a spreadsheet with the following sample data:

    Click image for larger version. 

Name:	Capture.PNG 
Views:	13 
Size:	4.5 KB 
ID:	47659

    I have managed to populate this data into an array.
    I have also managed to connect to my access database using excel vba.
    Please note I have purposely left column B (Age) blank because my real data set which consists of thousands of records and many columns will also have blank fields.
    All I am trying to do right now is get the principle working which is to populate the data into an array and then insert the whole contents of the array into an existing access database table without using loops because the actual data set is huge and looping will be inefficient.



    This is my code so far:

    Code:
    Sub test()
    
    Dim oConn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    
    Set oConn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    
    Dim conStr As String
    conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\mfil\proddfs\AppSense\Profiles\Users\patma01\Desktop\sample.accdb;"
    oConn.Open conStr
    
    
    Dim x As Variant
    x = ThisWorkbook.Sheets(1).UsedRange
    
    
    With rs
        .Open "Table1", oConn, adOpenDynamic, adLockOptimistic
        .AddNew Array(x)     ----------------------------------------------------------> this is where my code breaks 
        .Update
        .Close
    End With
    
    
    
    
    oConn.Close
    Set oConn = Nothing
    Set rs = Nothing
    
    
    End Sub

    As per my comment in the code above, this is the error message I get:

    Click image for larger version. 

Name:	Capture2.PNG 
Views:	13 
Size:	6.6 KB 
ID:	47660


    Please can you help.
    Thank you in advance.

  2. #2
    Join Date
    Apr 2017
    Posts
    1,792
    Much easier way:
    Link your Excel table into Access Database;
    Run a procedure in Access, which reads data from linked table, makes any wanted transformations when there is a need for this, and then inserts correct data into Access DB tables (NB! Linked Excel table(s) are used only for data import!).

    In case import process must be repeated in future, you leave linked table present in Access DB, and data there will be updated manually, or copied over, or the Excel file is copied over with new one with same table structure, using name which was used by linked table earlier. In case the process is used only once, to delete the linked table afterwards.

    A good idea will be to have the Access import procedure checking that imported data are really new - e.g. using queries like (on fly - I'm not sure about exact syntax, as for years now I use SQL Server as BE) Insert Into AccessTable at (FieldList) SELECT let.FieldList FROM LinkedExcelTable let LEFT JOIN AccessTable at0 ON at0.SomeFields = let.SomeFields WHERE at0.PrimaryKey Is Null

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

Similar Threads

  1. Replies: 10
    Last Post: 03-15-2020, 10:23 AM
  2. Update existing table in Access from Excel using VBA
    By rscott1989 in forum Import/Export Data
    Replies: 3
    Last Post: 11-25-2015, 01:20 PM
  3. Replies: 7
    Last Post: 03-22-2015, 02:29 AM
  4. retrieve table contents from an array
    By bonecone in forum Programming
    Replies: 1
    Last Post: 05-22-2012, 12:51 PM
  5. Replies: 5
    Last Post: 12-08-2011, 10:52 AM

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