1

I am looking for a solution to inserting data from Excel into Access 2010. The Data in Excel are in a form. After filling in the form, VBA in Excel will insert data to the Access table as a new record.

  1. I don't want to use function in Access - import from Excel.
  2. I don't want to store data in Excel - object table.

I looked at several topics, but none were what I was looking for. Could somebody help me with this?

braX
  • 11,506
  • 5
  • 20
  • 33
VALL2
  • 11
  • 1
  • 3

2 Answers2

0

Use DAO to avoid memory leaks with an open Excel sheet ( http://support.microsoft.com/kb/319998 ), for example:

'Reference: Microsoft Office x.x Access Database Engine Object Library
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim sDb As String
Dim sSQL As String
Dim qdf As QueryDef

sDb = "Z:\Docs\Test.accdb"

Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase(sDb)

''A stored query would be better
sSQL = "Parameters p1 Text, p2 Datetime; " _
& "INSERT INTO Table1 (AText,ADate) Values ([p1],[p2])"

Set qdf = db.CreateQueryDef("", sSQL)

qdf.Parameters!p1 = "ABC"
qdf.Parameters!p2 = #2013/1/17#
qdf.Execute dbFailOnError
Debug.Print qdf.RecordsAffected
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
0

You may Refer to https://stackoverflow.com/a/61950934/12289278

Sub ImportJEData()
Dim cnn As ADODB.Connection 'dim the ADO collection class
Dim rst As ADODB.Recordset 'dim the ADO recordset class
Dim dbPath
Dim x As Long
Dim var
Dim PayIDnxtRow As Long

'add error handling
On Error GoTo errHandler:

'Variables for file path and last row of data
dbPath = Sheets("Update Version").Range("b1").Value
Set var = Sheets("JE FORM").Range("F14")

PayIDnxtRow = Sheets("MAX").Range("c1").Value

'Initialise the collection class variable
Set cnn = New ADODB.Connection

'Create the ADODB recordset object.
'Set rst = New ADODB.Recordset 'assign memory to the recordset

'Connection class is equipped with a —method— named Open
'—-4 aguments—- ConnectionString, UserID, Password, Options
'ConnectionString formula—-Key1=Value1;Key2=Value2;Key_n=Value_n;
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
'two primary providers used in ADO SQLOLEDB —-Microsoft.JET.OLEDB.4.0 —-Microsoft.ACE.OLEDB.12.0
'OLE stands for Object Linking and Embedding, Database



Do
On Error Resume Next 'reset Err.obj.

     'Get the Max ID +1
    Set rst = Nothing
    Set rst = New ADODB.Recordset 'assign memory to the recordset
    SQL = "SELECT Max(ApNumber)+1 FROM PayVoucherID "
    rst.Open SQL, cnn

    'Check if the recordset is empty.
    'Copy Recordset to the Temporary Cell
    Sheets("MAX").Range("A2").CopyFromRecordset rst

    'Insert the Data to Database And Check If no Errors
    Sql2 = "INSERT INTO PayVoucherID(ApNumber)Values('" & Sheets("MAX").Range("A2") & "') "
    cnn.Execute Sql2

Loop Until (Err.Number = 0)

xlFilepath = Application.ThisWorkbook.FullName

SSql = "INSERT INTO PaypaymentID(Apnumber) " & _
"SELECT * FROM [Excel 12.0 Macro;HDR=YES;DATABASE=" & xlFilepath & "].[MAX$G1:G15000] where APNumber > 1"

cnn.Execute SSql

 Set rst = Nothing
Set rst = New ADODB.Recordset 'assign memory to the recordset

 SQL = "Select PayID From PayPaymentID where APNumber = " & _ 
Sheets("LEDGERTEMPFORM").Range("B8") & " order by PayID "

rst.Open SQL, cnn
Sheets("PaySeries").Range("B2").CopyFromRecordset rst

rst.Close
' Close the connection
cnn.Close
'clear memory
Set rst = Nothing
Set cnn = Nothing

'communicate with the user
'MsgBox " The data has been successfully sent to the access database"

'Update the sheet
Application.ScreenUpdating = True

On Error GoTo 0
Exit Sub
errHandler:

'clear memory
Set rst = Nothing
Set cnn = Nothing
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Export_Data"
End Sub




WIL
  • 117
  • 9