Then和IF放在同一行
If InStr("INSERT,DELETE,UPDATE,EXECUTE", UCase $ (sTokens(0))) \\无效字符。提示是$
Then \\缺少:行号或标签或语句或语句结束
可能是这个个IF 和 THEN不在一行上,也可能是你没有加载ADODB
这个问题我开始也遇到过的,在给出答案前先说一声,你第一行的那个词抄书抄错了,是ExecuteSQL,还有你必须确定加载了控件,下面是代码:
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_ Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE,EXECUTE", UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & "查询成功"
Else
Set rst = New ADODB.Recordset
rst.Open trim$(SQL), cnn, adOpenKeyset,adLockOptimistic
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & "条记录"
End If
ExecuteSQL_Exit:
Set rst = Nothing
set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误:" & Err.Description
Resume ExecuteSQL_Exit
End Function
希望能帮到你,至于ConnectString函数的声明,似乎应该在一个模块中。
你老火,写这本书的人更老火,rs,con,command这几个东西用得那么混乱。