我在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
答案 0 :(得分:3)
您有一个ADO记录集Rs
,其中包含要添加到Access表的数据。不是试图修复INSERT
语句来添加每一行,而是应该更容易为目标表打开DAO Recordset,并通过在DAO Recordset中添加新行来存储每个ADO行的值。虽然这仍然是RBAR (row by agonizing row)方法,但它应该比为每行构建和执行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
语句。您只需将值从一个记录集字段存储到另一个记录集中的相应字段。
答案 1 :(得分:1)
我认为你在这里抱怨的真正问题是你的ADO Recordset中的数据中有引号(有时称为撇号)。在使用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。
请注意,如果您确实想使用insert语句,则应考虑使用以下内容:
CurrentDb.Execute "INSERT INTO Statement Goes Here", dbFailOnError
这被认为是比DoCmd.RunSQL
更强大的解决方案,主要是因为它在底层数据库引擎而不是Access接口的上下文中运行。使用CurrentDb.Execute
可以防止您必须使用DoCmd.SetWarning
语句来关闭警告。