0

我在 access 2010 中有一个 ado 创建的记录集,它从 sql server 2008 r2 上的存储过程返回 9 个不同的字段。

我正在尝试使用此记录集(确实填充)将所有记录插入到与输出匹配的表中。我的问题是其中两个字段是名称字段,其中包含逗号。例如,Smith, Joseph——我需要将该逗号插入到相应的字段中。现在它会因为字段中的逗号而引发错误。

这是我正在使用的代码:

Option Compare Database

'Executes the filtering routine
Private Sub cmdApplyFilter_Click()

'If txtStartDate.Value And txtEndDate.Value Is Not Null Then
'    QuickFilter
'Else
'    DefaultRun
'End If
QuickFilter
'********** Filter as you type **********

'Private Sub txtFilter_Change()
'   QuickFilter
'End Sub


End Sub

'Perform the actual filtering on the subform
Private Sub QuickFilter()

Dim Sql As String
Dim filter As String

If txtStartDate = vbNullString Then
    'Reset the filter if the textbox is empty
    'This will be the default sql statement to fill the subreport
    SubForm.Form.FilterOn = False
Else
    'Some common substitutions that users may have already inserted as wildchars
    filter = Replace(txtStartDate, "%", "*")
    filter = Replace("*" & filter & "*", "**", "*")
    'Construct the filter for the sql statement
    '/*********** GROUP BY GOES HERE ***********/

    'Assign the filter to the subform
    'SubForm.Form.filter = Sql
    'SubFomr.Form.FilterOn = True
End If
End Sub


Private Sub Form_Load()
   'Sets up the connection with the sql server database retrieves the stored procedure,       executes it and puts the result set into a table
   Dim Conn As ADODB.Connection
   Dim Cmd As ADODB.Command
   Dim Rs As ADODB.Recordset
   Dim rs1 As ADODB.Recordset
   Dim Connect As String
   Dim filter As String


   Connect = "Provider =SQLNCLI10; Data Source=10.50.50.140; Initial Catalog=CCVG; User Id = oe; Password = Orth03c0; "

   'Establish the connection with sql server
   Set Conn = New ADODB.Connection
   Conn.ConnectionString = Connect
  Conn.Open

   'Open the recorset
   Set Cmd = New ADODB.Command
   Cmd.ActiveConnection = Conn
   Cmd.CommandText = "dbo.cusGenNoNotesReport"
   Cmd.CommandType = adCmdStoredProc

   Set Rs = Cmd.Execute()








   Dim x As Integer

   If Not Rs.BOF And Not Rs.EOF Then
    If Not Rs.BOF Then Rs.MoveFirst
    Do Until Rs.EOF
        For x = 0 To Rs.Fields.Count - 1
            MsgBox Rs.Fields(x)
            'DoCmd.RunSQL "INSERT INTO tblNoNotes (Provider, Facility, TicketNumber,       Charges, FinancialClass, CPT, CPTDescription, PatientFullName, DateOfEntry) SELECT " & Rs.Fields(x).Value & ""
        Next x
            Rs.MoveNext
    Loop
End If


   'Process results from recordset, then close it.
   'DoCmd.RunSQL "INSERT INTO tblNoNotes (Provider, Facility, TicketNumber, Charges,     FinancialClass, CPT, CPTDescription, PatientFullName, DateOfEntry) VALUES (""" & Rs![Provider] & """,""" & Rs![Facility] & """ & Rs![TicketNumber] & """, """ & Rs![Charges] & """, """ & Rs![FinancialClass] & """, """ & Rs![CPT] & """, """ & Rs![CPTDescription] & """, """ & Rs![PatientFullName] & """, """ & Rs![DateOfEntry] & """ )"

   Rs.Open
   Rs.Close
   Conn.Close
   Set Rs = Nothing
   Set Cmd = Nothing
   Set Conn = Nothing


End Sub
4

2 回答 2

2

您有一个 ADO 记录集,Rs其中包含要添加到 Access 表中的数据。与其尝试修复INSERT添加每一行的语句,不如为目标表打开一个 DAO Recordset 并通过在 DAO Recordset 中添加一个新行来存储每个 ADO 行的值。虽然这仍然是一个RBAR(逐行)方法,但它应该比INSERT为每一行构建和执行语句要快得多。

首先,确保添加Option Explicit到模块的声明部分。

Option Compare Database
Option Explicit

然后使用此代码将 ADO Recordset 数据附加到您的表中。

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语句。您只需将一个记录集字段的值存储到另一个记录集中的适当字段。

于 2013-10-08T21:45:04.053 回答
1

我认为您在这里抱怨的真正问题是您在 ADO 记录集中的数据中包含引号(有时称为撇号)。任何时候引号都可能存在于您的数据中,您需要在 SQL 语句中使用数据之前检查并转义它们。您不仅需要了解插入,还需要了解执行过滤和创建 WHERE 语句。例如:

Replace(Rs![PatientFullName], "'", "''")

一个更简单的方法是创建自己的小函数。“PQ”代表填充引号。你可以随意命名它。

PQ(rs![PatientFullName])

Public Function PQ(s as String) as String
    PQ = Replace(s, "'", "''")
End Function

但我也同意 HansUp 的观点,即使用记录集进行插入要容易得多。我基本上不再使用 SQL Insert 语句了,除了我没有选择的地方,比如 SQL Server T-SQL。

请注意,如果您确实想使用插入语句,则应考虑使用以下内容:

CurrentDb.Execute "INSERT INTO Statement Goes Here", dbFailOnError

这被认为是比 更强大的解决方案DoCmd.RunSQL,主要是因为它在底层数据库引擎而不是 Access 接口的上下文中运行。使用CurrentDb.Execute可防止您必须使用DoCmd.SetWarning语句来关闭警告。

于 2013-10-09T01:21:44.097 回答