使用VBA插入带有单引号的语句吗? [英] Insert into statement with an apostophe using VBA?

查看:98
本文介绍了使用VBA插入带有单引号的语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文本框的表格.我将用户在文本框中输入的内容插入表格中.如果用户在标有"Me.ProjectName"的文本框中输入撇号,则会出现错误.我的代码是:

I have a form with textboxes. I am inserting what the user enters into the textbox into a table. If the user enters an apostrophe in the textbox labeled "Me.ProjectName", I get an error. My code is:

   CurrentDb.Execute "INSERT INTO Table1(ProjectNumber, Title) " & _
        " VALUES('" & ProjectNumber & "','" & Me.ProjectName & "')"

推荐答案

您应该 转义 ,用两个引号将引号替换为可能包含引号的字符串:

You should escape your strings possibly containing quotes by replacing a quote with 2 quotes:

  Dim SQL As String

  SQL = "INSERT INTO Table1(ProjectNumber, Title) " & _
    " VALUES('" & ProjectNumber & "','" & Replace(Me.ProjectName, "'", "''")  & "')"

  CurrentDb.Execute SQL

这篇关于使用VBA插入带有单引号的语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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