我一直在寻找一种将数据从Excel推送到Access数据库的方法。我找到了下面的代码,但我不知道如何改进它。
Dim wsQS As Worksheet
Dim sConnect As String
Dim sCommand As String
Dim adoCn As ADODB.Connection
Set wsQS = Worksheets("QueryStrings")
Set adoCn = New ADODB.Connection
sConnect = wsQS.Range("rngConnect").Value
sCommand = wsQS.Range("rngCommand").Value
' Get ADO connection to the workbook
adoCn.Open sConnect
' Append data from Excel worksheet
adoCn.Execute sCommand
' Close the connection to the workbook
adoCn.Close
Set adoCn = Nothing
Worksheets("CopyToDB").Range("DataToExport").Offset(1, 0).ClearContents
Worksheets("Proj DB").Activate
Set wsQS = Nothing
建筑
------------------
| web |
| page |
------------------
|
|
|
Python and BS4(Data Extraction)
|
|
|
------------------
| Excel |
| data |
------------------
|
|
|
Python to Push Data(Oracle/Access)
|
|
|
------------------
| Any |
| DB |
------------------
答案 0 :(得分:2)
几乎可以在任何地方(VBScript,VBA)运行并在Access中从Excel创建表格的内容如何?您几乎可以运行任何有效的SQL。
Set cn=CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=z:\docs\test.accdb"
sSQL = "SELECT * INTO FromExcel " _
& "FROM [Excel 8.0;HDR=YES;DATABASE=Z:\Docs\Test.xls].[Sheet1$]"
cn.Execute sSQL, recs
MsgBox "Records: " & recs
Jet SQL
Fundamental Microsoft Jet SQL for Access 2000
Intermediate Microsoft Jet SQL for Access 2000
Advanced Microsoft Jet SQL for Access 2000
答案 1 :(得分:0)
我已经尝试了@ Remou的建议,但从未设法使用我的配置(Office 2010)。这是我从Microsoft Dev Forums 偷来的,它对我来说非常适合每种类型的SQL查询(选择/插入/删除等)
Public Sub updateAccess()
Dim con As New ADODB.Connection
Dim connectionString As String
Dim sql, newTable As String
Filename = "C:\test.accdb"
connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" & Filename
con.Open connectionString
' Save current table ("BEFORE") to another table ("BEFORE_yyyymmdd_hh_mmss")
newTable = "BEFORE_" & Format(Date, "yyyymmdd") & "_" & Format(Now, "hhmmss")
sql = "SELECT CODE, STORE INTO " & newTable & " FROM BEFORE"
con.Execute sql
' Delete rows of current table ("BEFORE")
sql = "DELETE FROM BEFORE"
con.Execute sql
' Insert new rows into current table ("BEFORE") from my Excel Sheet
sql = "INSERT INTO BEFORE ([CODE], [STORE]) " & _
"SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & ThisWorkbook.FullName & "].[" & ThisWorkbook.Sheets("CODE_BY_STORE").Name & "$]"
con.Execute sql
con.Close
Set con = Nothing
End Sub
这就像一个魅力!希望这会有所帮助...