【问题标题】:Using ADO VBA to Insert Excel Fields into Access Table使用 ADO VBA 将 Excel 字段插入 Access 表
【发布时间】:2012-07-11 12:38:25
【问题描述】:

我正在尝试使用 ADO 从 Excel 文件中访问和读取一些内容。我了解如何打开它并执行 SELECT * 并将其放入 Recordset 对象中。我不明白的是,如果我选择一组信息,如何访问该记录集中的特定字段。

代码:

Private Sub SaveReq_Click()
' 
' Saves the current entry to the database
' Into the TABLE 'pr_req_table'
' 

' Open a connection to the database
dim data_base as Database
set data_base = OpenDatabase(CurrentProject.Path & "\test_database.accdb")

Sub InsertRecord()
Dim data_base As Database
Set data_base = OpenDatabase(CurrentProject.Path & "\test_database.accdb")

' Grab all information from form
' Add information to pr_req_table
Dim qd As QueryDef
Set qd = data_base.CreateQueryDef("")
qd.sql = "INSERT INTO pr_req_table(pr_no, pr_date, pr_owner, pr_link, pr_signed) " & _
    "values([p1],[p2],[p3],[p4],[p5])"
qd.Parameters("p1").Value = pr_num.Value
qd.Parameters("p2").Value = Format(pr_date.Value, "mm/dd/yyyy")
qd.Parameters("p3").Value = List22.Value
qd.Parameters("p4").Value = "Excel Copy #" & elec_copy.Value
qd.Parameters("p5").Value =  "Signed Copy #" & sign_copy.Value 
qd.Execute


' The following section reads from the elec_copy field's hyperlink
' It scans the Excel file for items it needs to include into the table
' It enters those cells into the TABLE 'items_needed_table'
'
' Slects row by row, and if the item has been marked TRUE, inserts
' That row into the TABLE 'items_needed_table'


' Open a connection to Excel
On Error Resume Next

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

Set objConnection = CreateObject("ADODB.Connection")

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & elec_copy.Value & ";" & _
    "Extended Properties=""Excel 8.0;HDR=Yes;"";"

' Decalre a RecordSet Object
Set objRecordSet = CreateObject("ADODB.Recordset")

' Grab all Rows in the Plain_VDR Sheet where 'needed' column == TRUE
objRecordset.Open "Select line_no, desc, weeks FROM [Plain_VDR$] Where needed = TRUE", _
    objConnection, adOpenStatic, adLockOptimistic, adCmdText

' Declare a loop counter for row?
Dim x as Integer
x = 0

' Write the information pulled, into the TABLE 'items_needed_table' in Access Database
Do Until objRecordset.EOF
        qd.sql = "INSERT INTO items_needed_table(pr_no, line_no, desc, weeks) " & _
        "Values([p1],[p2],[p3])"
        ' p1 was declared earlier in code, same value as before
        qd.Parameters("p2").Value = objRecorset.(ROW_X, "line_no")
        qd.Parameters("p3").Value = objRecordset.(ROW_X, "desc")
        qd.Parameters("p4").Value = objRecordset.(ROW_X, "weeks")
        qd.Execute
        x = x + 1
Loop

' Close Database connection
data_base.Close

End Sub

我主要关心的是“直到”循环部分。怀疑我可以插入整个选择,因为“pr_no”没有在 Excel 文件中定义,而是在 Access 数据库中定义,所以我想我需要为 Excel 文件中的每一行循环该命令。 我需要使用什么来为 Recordset 对象中的每行和字段的参数分配值?

提前感谢您的帮助!

内森

【问题讨论】:

  • 如果 pr_no 是自动编号,则无需包含它。为什么不通过查询一次插入所有数据,或者只是将范围链接为 Access 中的表并运行足够普通的 Access 查询?
  • pr_no 不是自动编号。无法一次插入所有数据,因为我需要为每一行分配一个 pr_no。每次输入新条目时,Excel 文件都会有所不同,这就是为什么每次输入新条目时我都需要运行它。

标签: excel ms-access vba ado ms-access-2010


【解决方案1】:

在您的连接字符串中,您说 HDR=Yes,这意味着您的范围的第一行包含您的字段名称,所以,非常粗略:

Do Until objRecordset.EOF
    qd.Sql = "INSERT INTO items_needed_table(pr_no, line_no, desc, weeks) " & _
    "Values([p1],[p2],[p3])"
    ' p1 was declared earlier in code, same value as before
    '**No it was not, the earlier stuff is mostly irrelevant

    qd.Parameters("p2").Value = objRecorset.Fields("line_no")
    qd.Parameters("p3").Value = objRecordset.Fields("desc")
    qd.Parameters("p4").Value = objRecordset.Fields("weeks")
    qd.Execute
    ''You are moving through a recordset, not a worksheet
    objRecordset.MoveNext
Loop

如果这就是您从 Excel 中选择的所有内容,则可以通过一个查询插入它,因为您没有更改 pr_num。

【讨论】:

  • 谢谢@Remou。所以你是说 ("p1") 的值在上面的执行命令之后为空?
  • 它可能是也可能不是,这有点难以解开:) 我以为你已经重置了查询,但也许你没有。
猜你喜欢
  • 2012-07-26
  • 1970-01-01
  • 2021-02-25
  • 2011-11-16
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2012-11-22
相关资源
最近更新 更多