Ms access 当两个字段包含逗号时,如何使用ADO在表中插入多个字段

Ms access 当两个字段包含逗号时,如何使用ADO在表中插入多个字段,ms-access,stored-procedures,sql-server-2008-r2,vba,ms-access-2010,Ms Access,Stored Procedures,Sql Server 2008 R2,Vba,Ms Access 2010,我在access 2010中创建了一个ado记录集,它从sql server 2008 r2上的存储过程返回9个不同的字段 我试图使用这个记录集(它会填充)将所有记录插入到与输出匹配的表中。我的问题是其中两个字段是名称字段,其中包含逗号。例如,Smith,Joseph--我需要在适当的字段中插入逗号。现在它抛出一个错误,因为字段中有逗号 以下是我正在使用的代码: Option Compare Database 'Executes the filtering routine Private Su

我在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

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

首先,确保将
选项Explicit
添加到模块的声明部分

选项比较数据库
选项显式
然后使用此代码将ADO记录集数据附加到表中

Dim db作为DAO.Database
将rsDao设置为DAO.Recordset
Set db=CurrentDb
Set rsDao=db.OpenRecordset(“tblNoNotes”_
dbOpenTable,dbAppendOnly+dbFailOnError)
做而不做
rsDao.AddNew
rsDao!Provider.Value=Rs!提供者。值
rsDao!设施价值=卢比!设施价值
rsDao!TicketNumber.Value=Rs!票号值
rsDao!费用。价值=卢比!费用,价值
rsDao!FinancialClass.Value=Rs!金融类价值
rsDao!CPT.Value=Rs!CPT值
rsDao!CPTDescription.Value=Rs!CPTDescription.Value
rsDao!PatientFullName.Value=Rs!PatientFullName.Value
rsDao!DateOfEntry.Value=Rs!入境日期价值
rsDao.Update
下一个
环
关上
设置rsDao=Nothing
Set db=Nothing

注意:这种方法意味着您不必担心
PatientFullName
是否包含逗号或撇号。。。或者努力正确引用字段值以生成有效的
INSERT
语句。您只需将一个记录集字段中的值存储到另一个记录集中的相应字段。

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

首先,确保将
选项Explicit
添加到模块的声明部分

选项比较数据库
选项显式
然后使用此代码将ADO记录集数据附加到表中

Dim db作为DAO.Database
将rsDao设置为DAO.Recordset
Set db=CurrentDb
Set rsDao=db.OpenRecordset(“tblNoNotes”_
dbOpenTable,dbAppendOnly+dbFailOnError)
做而不做
rsDao.AddNew
rsDao!Provider.Value=Rs!提供者。值
rsDao!设施价值=卢比!设施价值
rsDao!TicketNumber.Value=Rs!票号值
rsDao!费用。价值=卢比!费用,价值
rsDao!FinancialClass.Value=Rs!金融类价值
rsDao!CPT.Value=Rs!CPT值
rsDao!CPTDescription.Value=Rs!CPTDescription.Value
rsDao!PatientFullName.Value=Rs!PatientFullName.Value
rsDao!DateOfEntry.Value=Rs!入境日期价值
rsDao.Update
下一个
环
关上
设置rsDao=Nothing
Set db=Nothing

注意:这种方法意味着您不必担心
PatientFullName
是否包含逗号或撇号。。。或者努力正确引用字段值以生成有效的
INSERT
语句。您只需将一个记录集字段中的值存储到另一个记录集中的相应字段。

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

首先,确保将
选项Explicit
添加到模块的声明部分

选项比较数据库
选项显式
然后使用此代码将ADO记录集数据附加到表中

Dim db作为DAO.Database
将rsDao设置为DAO.Recordset
Set db=CurrentDb
Set rsDao=db.OpenRecordset(“tblNoNotes”_
dbOpenTable,dbAppendOnly+dbFailOnError)
做而不做
rsDao.AddNew
rsDao!Provider.Value=Rs!提供者。值
rsDao!设施价值=卢比!设施价值
rsDao!TicketNumber.Value=Rs!票号值
rsDao!费用。价值=卢比!费用,价值
rsDao!FinancialClass.Value=Rs!金融类价值
rsDao!CPT.Value=Rs!CPT值
rsDao!CPTDescription.Value=Rs!CPTDescription.Value
rsDao!PatientFullName.Value=Rs!PatientFullName.Value
rsDao!DateOfEntry.Value=Rs!入境日期价值
rsDao.Update
下一个
环
关上
设置rsDao=Nothing
Set db=Nothing
注意:这种方法意味着您不必担心
PatientFullName
是否包含逗号或撇号。。。或者努力正确引用字段值以生成有效的
INSERT
语句。您只需将值从一个记录集字段存储到另一个记录集中的相应字段。

您有一个
CurrentDb.Execute "INSERT INTO Statement Goes Here", dbFailOnError