添加记录以访问 Table(不一致的结果)
Adding Records to Access Table (Inconsistent Results)
我正在 Excel 中从事一个 VBA 项目,作为记录管理工具。在一种情况下,需要将记录添加到具有不同字段信息的同一 Access DB 中的 2 tables。我能够成功添加到第一个 table (Tours_Items),它可以通过遍历可用行来添加多条记录。但是,我无法将以下记录添加到它们各自的 table (Tours_Tours) 中。执行代码时出现以下错误:“运行-time error'438': Object doesn't support this 属性 or method”.
在 Tours_Items 上执行的 VBA 在 table 名称和 [=22= 所需的循环功能之外几乎与 Tours_Tours 相同] 所以我完全不明白为什么当更复杂的执行时它不能正确执行。潜艇运行一个接一个; Tours_Items 然后 Tours_Tours。我什至试过只执行 Tours_Tours 但我仍然得到同样的错误。
字段与 Access DB 字段正确匹配(我已经三重检查),即使没有匹配,VBA 也不会抛出对象错误。 Access DB 字段已适当配置以接受要添加到记录中的数据类型。
代码:
Sub DBAddTours_Items()
Dim DBFile, Tbl As String
Dim SQL As String
Dim j, NxtRowItems, NxtItemRw As Integer
Dim con, rs As Object
Dim wsToursTtl As Worksheet
Set wsToursTtl = Worksheets("Tour Subtotal")
'Fill Items to DB
'Connect to the Database
Set con = CreateObject("ADODB.connection")
'Set File and tables
DBFile = ThisWorkbook.path & "\" & "DailyInfo.accdb"
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBFile
'Define
NxtRowItems = WorksheetFunction.CountA(wsToursTtl.Range("B:B"))
NxtItemRw = WorksheetFunction.CountA(Sheets("Tours_Items").Range("A:A")) + 1
'Create the ADODB recordset object.
Set rs = CreateObject("ADODB.Recordset")
'Define Table and SQL
Tbl = "Tours_Items"
SQL = "SELECT * FROM " & Tbl
'Set the necessary recordset properties.
With rs
.CursorType = 1 'adOpenKeyset on early binding
.LockType = 3 'adLockOptimistic on early binding
'Open the recordset.
.Open SQL, con
'Loop through and add all items
For j = 2 To NxtRowItems
.AddNew
.Fields("ItemID") = (j - 3) + NxtItemRw
.Fields("TourID") = wsToursTtl.Cells(j, 1)
.Fields("ItemNum") = wsToursTtl.Cells(j, 2)
.Fields("Admission") = wsToursTtl.Cells(j, 3)
.Fields("Ticket") = wsToursTtl.Cells(j, 4)
.Fields("Premium/BAT") = wsToursTtl.Cells(j, 5)
.Fields("Tour") = wsToursTtl.Cells(j, 6)
.Fields("Price") = wsToursTtl.Cells(j, 7)
.Update
Next j
'Close Recordset
.Close
End With
'Close the connection.
con.Close
'Release Variables
Set rs = Nothing
Set con = Nothing
SQL = ""
Tbl = ""
End Sub
Sub DBAddTours_Tours()
Dim DBFile, Tbl As String
Dim SQL As String
Dim con, rs As Object
Dim wsToursTtl As Worksheet
Set wsToursTtl = Worksheets("Tour Subtotal")
'Fill Tour info Tours_Tours
'Connect to the Database
Set con = CreateObject("ADODB.connection")
'Set File and tables
DBFile = ThisWorkbook.path & "\" & "DailyInfo.accdb"
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBFile
'Create the ADODB recordset object.
Set rs = CreateObject("ADODB.Recordset")
'Define Table and SQL
Tbl = "Tours_Tours"
SQL = "SELECT * FROM " & Tbl
'Set the necessary recordset properties.
With rs
.CursorType = 1 'adOpenKeyset on early binding
.LockType = 3 'adLockOptimistic on early binding
'Open the recordset.
.Open SQL, con
'Add new record to table
.AddNew
.Field("TourID") = wsToursTtl.Range("TourID_Dest").Value
.Field("TourDate") = wsToursTtl.Range("TourDate_Dest").Value
.Field("TourTime") = wsToursTtl.Range("TourTime_Dest").Value
.Field("Premium/Bat/Disc") = Me.PremiumBAT_Lbl.Caption
.Field("TourType") = wsToursTtl.Range("TourType_Dest").Value
.Field("GuestName") = wsToursTtl.Range("GuestName_Dest").Value
.Field("GuestAddress") = wsToursTtl.Range("GuestAddress_Dest").Value
.Field("GuestPhone") = wsToursTtl.Range("GuestPhone_Dest").Value
.Field("GuestEmail") = wsToursTtl.Range("GuestEmail_Dest").Value
.Field("GuestMember") = wsToursTtl.Range("GuestMem_Dest").Value
.Field("TourComments") = wsToursTtl.Range("Comments_Dest").Value
.Field("SaleDate") = wsToursTtl.Range("SaleDate_Dest").Value
.Field("TransactionDetails") = wsToursTtl.Range("Trxn_Dest").Value
.Field("SoldBy") = wsToursTtl.Range("SoldBy_Dest").Value
.Field("PaymentType") = wsToursTtl.Range("PayType_Dest").Value
.Field("BookedNum") = 2
.Field("LastModified") = Now
.Field("ModifiedBy") = Application.UserName
.Update
'Close Recordset
.Close
End With
'Close the connection.
con.Close
'Release Variables
Set rs = Nothing
Set con = Nothing
SQL = ""
Tbl = ""
End Sub
非常感谢任何帮助!
访问字段的正确集合是.Fields
复数。
所以 DBAddTours_Items
是正确的,在您的其他程序中,DBAddTours_Tours
您调用了 .Field
singular。哪个不存在。
在此处找到的文档:https://docs.microsoft.com/en-us/sql/ado/guide/data/the-fields-collection?view=sql-server-ver15
我正在 Excel 中从事一个 VBA 项目,作为记录管理工具。在一种情况下,需要将记录添加到具有不同字段信息的同一 Access DB 中的 2 tables。我能够成功添加到第一个 table (Tours_Items),它可以通过遍历可用行来添加多条记录。但是,我无法将以下记录添加到它们各自的 table (Tours_Tours) 中。执行代码时出现以下错误:“运行-time error'438': Object doesn't support this 属性 or method”.
在 Tours_Items 上执行的 VBA 在 table 名称和 [=22= 所需的循环功能之外几乎与 Tours_Tours 相同] 所以我完全不明白为什么当更复杂的执行时它不能正确执行。潜艇运行一个接一个; Tours_Items 然后 Tours_Tours。我什至试过只执行 Tours_Tours 但我仍然得到同样的错误。
字段与 Access DB 字段正确匹配(我已经三重检查),即使没有匹配,VBA 也不会抛出对象错误。 Access DB 字段已适当配置以接受要添加到记录中的数据类型。
代码:
Sub DBAddTours_Items()
Dim DBFile, Tbl As String
Dim SQL As String
Dim j, NxtRowItems, NxtItemRw As Integer
Dim con, rs As Object
Dim wsToursTtl As Worksheet
Set wsToursTtl = Worksheets("Tour Subtotal")
'Fill Items to DB
'Connect to the Database
Set con = CreateObject("ADODB.connection")
'Set File and tables
DBFile = ThisWorkbook.path & "\" & "DailyInfo.accdb"
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBFile
'Define
NxtRowItems = WorksheetFunction.CountA(wsToursTtl.Range("B:B"))
NxtItemRw = WorksheetFunction.CountA(Sheets("Tours_Items").Range("A:A")) + 1
'Create the ADODB recordset object.
Set rs = CreateObject("ADODB.Recordset")
'Define Table and SQL
Tbl = "Tours_Items"
SQL = "SELECT * FROM " & Tbl
'Set the necessary recordset properties.
With rs
.CursorType = 1 'adOpenKeyset on early binding
.LockType = 3 'adLockOptimistic on early binding
'Open the recordset.
.Open SQL, con
'Loop through and add all items
For j = 2 To NxtRowItems
.AddNew
.Fields("ItemID") = (j - 3) + NxtItemRw
.Fields("TourID") = wsToursTtl.Cells(j, 1)
.Fields("ItemNum") = wsToursTtl.Cells(j, 2)
.Fields("Admission") = wsToursTtl.Cells(j, 3)
.Fields("Ticket") = wsToursTtl.Cells(j, 4)
.Fields("Premium/BAT") = wsToursTtl.Cells(j, 5)
.Fields("Tour") = wsToursTtl.Cells(j, 6)
.Fields("Price") = wsToursTtl.Cells(j, 7)
.Update
Next j
'Close Recordset
.Close
End With
'Close the connection.
con.Close
'Release Variables
Set rs = Nothing
Set con = Nothing
SQL = ""
Tbl = ""
End Sub
Sub DBAddTours_Tours()
Dim DBFile, Tbl As String
Dim SQL As String
Dim con, rs As Object
Dim wsToursTtl As Worksheet
Set wsToursTtl = Worksheets("Tour Subtotal")
'Fill Tour info Tours_Tours
'Connect to the Database
Set con = CreateObject("ADODB.connection")
'Set File and tables
DBFile = ThisWorkbook.path & "\" & "DailyInfo.accdb"
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBFile
'Create the ADODB recordset object.
Set rs = CreateObject("ADODB.Recordset")
'Define Table and SQL
Tbl = "Tours_Tours"
SQL = "SELECT * FROM " & Tbl
'Set the necessary recordset properties.
With rs
.CursorType = 1 'adOpenKeyset on early binding
.LockType = 3 'adLockOptimistic on early binding
'Open the recordset.
.Open SQL, con
'Add new record to table
.AddNew
.Field("TourID") = wsToursTtl.Range("TourID_Dest").Value
.Field("TourDate") = wsToursTtl.Range("TourDate_Dest").Value
.Field("TourTime") = wsToursTtl.Range("TourTime_Dest").Value
.Field("Premium/Bat/Disc") = Me.PremiumBAT_Lbl.Caption
.Field("TourType") = wsToursTtl.Range("TourType_Dest").Value
.Field("GuestName") = wsToursTtl.Range("GuestName_Dest").Value
.Field("GuestAddress") = wsToursTtl.Range("GuestAddress_Dest").Value
.Field("GuestPhone") = wsToursTtl.Range("GuestPhone_Dest").Value
.Field("GuestEmail") = wsToursTtl.Range("GuestEmail_Dest").Value
.Field("GuestMember") = wsToursTtl.Range("GuestMem_Dest").Value
.Field("TourComments") = wsToursTtl.Range("Comments_Dest").Value
.Field("SaleDate") = wsToursTtl.Range("SaleDate_Dest").Value
.Field("TransactionDetails") = wsToursTtl.Range("Trxn_Dest").Value
.Field("SoldBy") = wsToursTtl.Range("SoldBy_Dest").Value
.Field("PaymentType") = wsToursTtl.Range("PayType_Dest").Value
.Field("BookedNum") = 2
.Field("LastModified") = Now
.Field("ModifiedBy") = Application.UserName
.Update
'Close Recordset
.Close
End With
'Close the connection.
con.Close
'Release Variables
Set rs = Nothing
Set con = Nothing
SQL = ""
Tbl = ""
End Sub
非常感谢任何帮助!
访问字段的正确集合是.Fields
复数。
所以 DBAddTours_Items
是正确的,在您的其他程序中,DBAddTours_Tours
您调用了 .Field
singular。哪个不存在。
在此处找到的文档:https://docs.microsoft.com/en-us/sql/ado/guide/data/the-fields-collection?view=sql-server-ver15