更新时间:2023-11-09 20:36:28
您有一个ADO记录集Rs
,其中包含要添加到Access表中的数据.与其尝试修复INSERT
语句以添加每一行,不如为目标表打开DAO记录集并通过在DAO记录集中添加新行来存储每个ADO行中的值,将更加容易.尽管这仍然是 RBAR( 方法,它应该比为每行建立并执行INSERT
语句快得多.
You have an ADO Recordset, Rs
, which contains data you want to add to your Access table. Instead of trying to fix the INSERT
statement to add each row, it should be easier to open a DAO Recordset for the destination table and store the values from each ADO row by adding a new row the the DAO Recordset. Although this is still a RBAR (row by agonizing row) approach, it should be significantly faster than building and executing an INSERT
statement for each row.
首先,请确保将Option Explicit
添加到模块的声明"部分.
First of all, make sure to add Option Explicit
to your module's Declarations section.
Option Compare Database
Option Explicit
然后使用此代码将ADO Recordset数据追加到表中.
Then use this code to append the ADO Recordset data to your table.
Dim db As DAO.Database
Dim rsDao As DAO.Recordset
Set db = CurrentDb
Set rsDao = db.OpenRecordset("tblNoNotes", _
dbOpenTable, dbAppendOnly + dbFailOnError)
Do While Not Rs.EOF
rsDao.AddNew
rsDao!Provider.Value = Rs!Provider.Value
rsDao!Facility.Value = Rs!Facility.Value
rsDao!TicketNumber.Value = Rs!TicketNumber.Value
rsDao!Charges.Value = Rs!Charges.Value
rsDao!FinancialClass.Value = Rs!FinancialClass.Value
rsDao!CPT.Value = Rs!CPT.Value
rsDao!CPTDescription.Value = Rs!CPTDescription.Value
rsDao!PatientFullName.Value = Rs!PatientFullName.Value
rsDao!DateOfEntry.Value = Rs!DateOfEntry.Value
rsDao.Update
Rs.MoveNext
Loop
rsDao.Close
Set rsDao = Nothing
Set db = Nothing
请注意,这种方法意味着您不必担心PatientFullName
是否包含逗号或撇号...,也不必担心正确引用字段值以生成有效的INSERT
语句.您只需将值从一个记录集字段存储到另一记录集的相应字段中即可.
Note this approach means you needn't worry about whether PatientFullName
contains a comma, or apostrophe ... or struggle with properly quoting field values to produce a valid INSERT
statement. You simply store the value from one recordset field to the appropriate field in another recordset.