在Access中保留单引号 [英] Preserving Single Quotes in Access

查看:147
本文介绍了在Access中保留单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Access 2010中创建了一个窗体,该窗体用于将数据插入到现有表中.该表包含一个关键字"字段,源"组合框和一个代码"文本框,在其中写入要插入的数据,并且有一个用于执行查询的按钮.表单的代码为:

I have created a form in Access 2010 that is used to insert data into an existing table. The table contains a Keywords field, Source combo box, and a Code text box where i write the data to be inserted and there is a button for executing the query. The code for the form is:

Private Sub cmd_go_Click()
Dim insertstring As String
insertstring = "INSERT INTO KWTable (KW, Source, Code) VALUES('" & text_key.Value & "','" & combo_source.Value & "','" & txt_code.Value & "');"
DoCmd.RunSQL insertstring
End Sub

代码很简单,它将数据输入到表中,因此我可以引用它以备将来使用.现在我遇到的问题是,当我尝试添加在SQL Server中使用的较长代码时,我遇到了语法缺失的表达式错误,我认为这是单引号引起的,因为该代码来自SQL.我收到错误消息是因为当我尝试存储我在SQL Server中使用的代码时,它使用单引号引起访问权限无法识别.我认为,如果我尝试在插入形式的代码中编写一些有助于将单引号转换为双引号的内容,然后将其重新转换回单引号将有助于解决问题.我只是不知道该怎么做,真的可以使用一些帮助.

The code is simple, it inputs the data to the table so i can reference it for future use. Now the problem I am having is that when I try to add long bits of code that I use in SQL Server i get a syntax missing expression error which I am assuming is coming from the single quotes since the code is from SQL. I am getting the error because when i am trying to store a code i used in SQL Server it uses single quotes which access does not recognise. I think if I try to write in the code for the insert form something to help convert the single quotes into double quotes, then reconvert them back to single quoteswill help solve the problem. I just cant figure out how to do it and could really use some help.

谢谢

推荐答案

通过使用参数查询,可以避免在插入的文本中包含引号的麻烦.

You can avoid trouble with included quotes in your inserted text by using a parameter query.

cmd_go_Click()考虑这样的方法.

Dim strInsert As String
Dim db As DAO.database
Dim qdf As DAO.QueryDef

strInsert = "PARAMETERS pKW TEXT(255), pSource TEXT(255), pCode TEXT(255);" & vbCrLf & _
"INSERT INTO KWTable (KW, Source, Code) VALUES (pKW, pSource, pCode);"
'Debug.Print strInsert
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strInsert)
qdf.Parameters("pKW") = Me.text_key.value
qdf.Parameters("pSource") = Me.combo_source.value
qdf.Parameters("pCode") = Me.txt_code.value
qdf.Execute dbFailOnError
Set qdf = Nothing
Set db = Nothing

但是,我不明白JoinCells()的位置.

However, I don't understand how JoinCells() fits in.

这篇关于在Access中保留单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆