简体   繁体   English

如何将存储为Excel VBA程序中变量的数据传输到Access数据库

[英]How to transfer data stored as variables in an Excel VBA program to an Access Database

I have data from a series of Excel VBA calculations stored as variables in the VBA code. 我有一系列Excel VBA计算得出的数据作为变量存储在VBA代码中。 What I want to do at the end of the code is to transfer the data from these variables into an MS Access database as a new record. 我在代码末尾要做的是将这些变量中的数据作为新记录传输到MS Access数据库中。

The Access database already exists and is ready. Access数据库已经存在并且已经准备就绪。 Do I have to put the data in the variables into cells on a worksheet before I can transfer them to Access? 我必须先将变量中的数据放入工作表中的单元格中,然后才能将其传输到Access吗?

Here are an example of the variables in the VBA code that I am trying to store in the database (variable name corresponds to the database column heading) 这是我尝试存储在数据库中的VBA代码中的变量的示例(变量名称对应于数据库列标题)

Dim customerName As String
Dim customerAge As Integer
Dim customerSpend As Double

customerName = "Tim"
customerAge = 26
customerSpend = 12876

This is the code I've managed to write so far to try and transfer the data, but I'm stuck at the 'Range' part, as they are variables and not a range of cells: 到目前为止,这是我设法编写的代码,用于尝试传输数据,但是由于它们是变量而不是单元格范围,因此我一直停留在“范围”部分:

Dim strPath As String
Dim objAccess As Object

strPath = "C:\db1.accdb"
Set objAccess = CreateObject("Access.Application")
Call objAccess.OpenCurrentDatabase(strPath)
objAccess.Visible = True


Call DoCmd.TransferSpreadsheet(acImport, acSpreadsheetTypeExcel12, CustomerDetails, Range)

How do I transfer the data stored in the variables in the VBA code into the corresponding columns in the Access database to make a new record? 如何将VBA代码中变量中存储的数据转移到Access数据库中的相应列中以进行新记录?

Currently, you are treating Access as its MS Office frontend GUI application and not as a relational backend database that can receive SQL calls. 当前,您将Access视为其MS Office 前端 GUI应用程序,而不是视为可以接收SQL调用的关系后端数据库。 DoCmd.TransferSpreadsheet is an Access application method requiring a saved Excel spreadsheet which you do not use. DoCmd.TransferSpreadsheet是一种Access应用程序方法,需要您不使用的已保存Excel电子表格。

However, you can pass variable values into an Access table using an SQL append query. 但是,您可以使用SQL附加查询将变量值传递到Access表中。 And not just Access, any application layer code like VBA can connect to a database like Access and append/update/delete table data with SQL. 不仅是Access,任何应用程序层代码(例如VBA)都可以连接到Access这样的数据库,并使用SQL附加/更新/删除表数据。 Below demonstrates with DAO, the default Access database API, using the industry best practice of query parameterization. 下面通过DAO(默认的Access数据库API)演示了如何使用行业最佳查询参数化实践。

Sub RunSQL()
    Dim objAccess  As Object
    Dim db As Object, qdef As Object
    Dim strPath As String, strSQL As String
    Dim customerName As String, customerAge As Integer, customerSpend As Double

    customerName = "Tim": customerAge = 26: customerSpend = 12876
    strPath = "C:\db1.accdb"        

    ' OPEN ACCESS APPLICATION
    Set objAccess = CreateObject("Access.Application")
    objAccess.OpenCurrentDatabase strPath

    ' OPEN INTERNAL DATABASE
    Set db = objAccess.CurrentDb

    ' PREPARE STATEMENT
    strSQL = "PARAMETERS [nameparam] TEXT(255), [ageparam] INTEGER, [spendparam] DOUBLE;" _
               & " INSERT INTO CustomerDetails ([name], [age], [spend]);"

    ' BUILD TEMP QUERYDEF
    Set qdef = db.CreateQueryDef("", strSQL)

    ' BIND PARAMS TO VARIABLES
    qdef!nameparam = customerName
    qdef!ageparam = customerAge
    qdef!spendparam = customerSpend

    ' EXECUTE ACTION QUERY
    qdef.Execute

    Set qdef = Nothing
    Set db = Nothing
    Set objAccess = Nothing
End Sub

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

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