使用VBA将数据从Excel中插入到Access 2010中。

问题描述 投票:1回答:2

我正在寻找一个解决方案,将Excel中的数据插入到Access 2010中。Excel中的数据是在一个表格中。在填写表格后,Excel中的VBA会将数据作为新记录插入到Access表中。

  1. 我不想使用Access中的函数--从Excel导入。
  2. 我不想在Excel中存储数据--对象表。

我看了好几个题目,但都不是我想要的。谁能帮我解决这个问题?

excel vba ms-access excel-vba import
2个回答
0
投票

使用DAO避免打开Excel表的内存泄漏( http:/support.microsoft.comkb319998 ),例如。

'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

0
投票

你可以参考 https:/stackoverflow.coma6195093412289278。

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




© www.soinside.com 2019 - 2024. All rights reserved.