'****我写过一个把一个字符串里的一个单引号变成两个单引号的函数**** Private Function ConvertString(strSource As String) Dim strnew As String Dim intNum As Integer strnew = "" If InStr(strSource, "'") > 0 Then For intNum = 1 To Len(strSource) If Mid(strSource, intNum, 1) = "'" Then strnew = strnew & "''" Else strnew = strnew & Mid(strSource, intNum, 1) End If ConvertString = strnew Next Else ConvertString = strSource End If End Function'例如 dim strTemp as String dim strRtn as String strTemp="I'm sorry!" strRtn = ConvertString(strTemp) '此时strRtn的值为"I''m sorry!"':)此函数稍作修改可以转换很多特殊字符
遇到过,字段名中带 / 号
后来用fields(n)解决
期待高手
實際操作時用replace替换
处理Select语句中的单引号
在数据库应用中, 经常要动态生成 Select 语句,典型的情况:
SqlString = "Select * from myBas where Name = '" & Text1 & "'"
好啦, 问题出现了, 如果在录入的 Text1 中有一个单引号,结果是把 SqlString 发给数据库时, 将出错!其实要做的防范很简单, 增加一个函数:FUNCTION CheckString (s) as String
pos = InStr(s, "'")
While pos > 0
s = Mid(s, 1, pos) & "'" & Mid(s, pos + 1)
pos = InStr(pos + 2, s, "'")
WendCheckString="'" & s & "'"
END FUNCTION以后在动态生成 Select 语句, 使用:
SqlString = "Select * from myBas where Name = " & CheckString(Text1)
e.g.,
strSQL = "UPDATE yourTable SET yourField = '" & replace(text1.text, "'", "''")
cnn.execute strSQL
e.g.,
strSQL = "UPDATE yourTable SET yourField = '" & replace(text1.text, "'", "''") & "'"
cnn.execute strSQL
Private Function ConvertString(strSource As String)
Dim strnew As String
Dim intNum As Integer
strnew = ""
If InStr(strSource, "'") > 0 Then
For intNum = 1 To Len(strSource)
If Mid(strSource, intNum, 1) = "'" Then
strnew = strnew & "''"
Else
strnew = strnew & Mid(strSource, intNum, 1)
End If
ConvertString = strnew
Next
Else
ConvertString = strSource
End If
End Function'例如
dim strTemp as String
dim strRtn as String
strTemp="I'm sorry!"
strRtn = ConvertString(strTemp)
'此时strRtn的值为"I''m sorry!"':)此函数稍作修改可以转换很多特殊字符