简体   繁体   English

将数据导出到Excel VBA中的Access

[英]Export data to Access from Excel VBA

I am brand new to access and struggling to understand how to relate access table/column/row formatting to excel exporting in VBA. 我是访问的新手,并且正在努力了解如何将访问表/列/行格式与VBA中的excel导出相关联。

I have created an access table called Data and three columns Food, Drinks, Color . 我创建了一个名为Data的访问表,并创建了3列Food,Drinks和Color列。

存取表

I would like to export these range of cells to Access from my excel spreadsheet: 我想将这些单元格范围从我的excel电子表格导出到Access:

Foodrng = Workbooks(xlFile).Sheets("ToBeExported").Range("D6")
Drinksrng = Workbooks(xlFile).Sheets("ToBeExported").Range("E6")
Colorrng= Workbooks(xlFile).Sheets("ToBeExported").Range("B12:B21")

Everything online says I should use this for because of my version: 网上的所有内容都表明我应该使用此版本,因为我的版本是:

strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;"

I would like to use INSERT TO formatting to write to my database because I will be expanding the database columns quite a bit, and I feel this is the easiest way to keep track of which is going where. 我想使用INSERT TO格式写入数据库,因为我将扩展数据库列很多,并且我认为这是跟踪哪个行进去的最简单方法。

strSql = "INSERT INTO Data (Food, Drinks, Color) VALUES (Foodrng, Drinksrng,Colorrng)"

I always get a syntax error when executing: 执行时总是会出现语法错误:

Set rs = cn.Execute(strSql)

What is the correct way to export to the Access database using the above method? 使用上述方法导出到Access数据库的正确方法是什么? Any/all information will be super helpful as I am brand new to Access 由于我是Access的新手,任何/所有信息都将非常有帮助

My full code: 我的完整代码:

    Foodrng = Workbooks(xlFile).Sheets("ToBeExported").Range("D6")
    Drinksrng = Workbooks(xlFile).Sheets("ToBeExported").Range("E6")
    Colorrng= Workbooks(xlFile).Sheets("ToBeExported").Range("B12:B21")

    Set cn = CreateObject("ADODB.Connection")
    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source= C:\Users\User1\MyDBase.accdb"
    cn.Open strConnection

   strSql = "INSERT INTO Data (Food, Drinks, Color) VALUES (Foodrng, Drinksrng,Colorrng)"

    Set rs = cn.Execute(strSql)
    'MsgBox rs.Fields(0) & " rows in MyTable"
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing

When inserting using queries, you need to pass values using parameters. 使用查询插入时,需要使用参数传递值。 I highly recommend using recordsets over insert queries. 我强烈建议在插入查询中使用记录集。

A normal insert query can only insert one row at a time. 普通的插入查询一次只能插入一行。 You will need to adjust the code to insert one row at a time. 您将需要调整代码以一次插入一行。 You can either use a recordset, or execute a query for each row. 您可以使用记录集,也可以对每一行执行查询。

Foodrng = Workbooks(xlFile).Sheets("ToBeExported").Range("D6") 'Adjust ranges to select single cells
Drinksrng = Workbooks(xlFile).Sheets("ToBeExported").Range("E6")
Colorrng= Workbooks(xlFile).Sheets("ToBeExported").Range("B12:B21")

Set cn = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source= C:\Users\User1\MyDBase.accdb"
cn.Open strConnection

strSql = "INSERT INTO [Data] ([Food], [Drinks], [Color]) VALUES (?, ?, ?)"
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
With cmd
    Set .ActiveConnection = cn
    .CommandText = strSql
    .Parameters.Append .CreateParameter(, adVarWChar, adParamInput, , foodRng) 'adVarWChar for text 
    .Parameters.Append .CreateParameter(, adInteger, adParamInput, , Drinksrng) 'adInteger for whole numbers (long or integer)
    .Parameters.Append .CreateParameter(, adInteger, adParamInput, , Colorrng)
    .Execute
End With
cn.Close

There maybe also another way, or two, perhaps worth consideration. 也许还有另一种或两种方式,也许值得考虑。

The first would be to set up your Excel spreadsheet as a linked table in Access - maybe it can be done with VBA. 首先是将Excel电子表格设置为Access中的链接表-也许可以使用VBA来完成。 This would save you the requirement to copy the data. 这将节省您复制数据的需求。 You might also be able to set up your target worksheet and use insert..select directly from ADODB to insert the data into Excel from Access. 您也许还可以设置目标工作表并直接使用ADODB中的insert..select将数据从Access中插入Excel。

The second, would be to completely avoid Access altogether if your requirements allow for this. 第二,如果您的要求允许的话,将完全完全避免使用Access。 Excel can be used as a database to some extent and supports SQL querying. Excel可以在某种程度上用作数据库并支持SQL查询。 https://selectcompare.com/blogs/news/write-select-statements-for-excel-spreadsheets https://selectcompare.com/blogs/news/write-select-statements-for-excel-spreadsheets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM