Function ProcessStr(str As String)
Dim pos As Integer
Dim stedest As String
pos = InStr(str, "'")
While pos > 0
str = Mid(str, 1, pos) & "'" & Mid(str, pos + 1)
pos = InStr(pos + 2, str, "'")
Wend
ProcessStr = str
End Function
update employee set fname=" Paolo''''f"
where emp_id='PMA42628M'
update employee set fname=" Paolo''f"
where emp_id='PMA42628M'
程序如下:
首先把前面的函数加入。在窗体的通用中声明如下变量:
Dim cnn1 As ADODB.Connection '连接
Dim mycommand As ADODB.Command '命令
Dim rstByQuery As ADODB.Recordset '结果集
Dim strCnn As String '连接字符串
Private Sub Form_Load()
Set cnn1 = New ADODB.Connection '生成一个连接
strCnn = "driver={SQL Server};" & _
"server=ZYX_pc;uid=sa;pwd=PCDC;database=pubs" '
没有系统数据源使用连接字符串
'strCnn = "DSN=mydsn;UID=sa;PWD=;"
'DATABASE=pubs;Driver={SQL Server};SERVER=gzl_pc" '
如果系统数据源MYDSN指向PUBS数据库,也可以这样用
cnn1.Open strCnn, , , 0 '打开连接
End Sub
Private Sub Command1_Click() '演示字符处理
Dim i As Integer
Dim j As Integer
Set parm = New ADODB.Parameter
Set mycommand = New ADODB.Command
Dim str As String
str = Combo1.Text
str = ProcessStr (str)
mycommand.ActiveConnection = cnn1 '
指定该command 的当前活动连接
mycommand.CommandText = " select * from
employee where fname = '" & str & "'"
mycommand.CommandType = adCmdText '表明command 类型
Set rstByQuery = New ADODB.Recordset
Set rstByQuery = mycommand.Execute()
i = 0
Do While Not rstByQuery.EOF
i = i + 1 ' i 中保存记录个数
rstByQuery.MoveNext
Loop
MSFlexGrid1.Rows = i + 1 '动态设置MSFlexGrid的行和列