标题: 为什么会提示with块未设置? [打印本页] 作者: jhzlzc29 时间: 2003-11-21 04:20 标题: 为什么会提示with块未设置? 前台为mdb,后台为SQL2000
引用函数为:
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", _
UCase$(stokens(0))) Then
cnn.Execute SQL
msgString = stokens(0) & _
"query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
'rst.movelast 'get recordcount
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
Public Function connectstring() As String
connectstring = "filedsn=ZJGL.dsn;uid=sa;pwd="
End Function
执行代码为:
Private Sub cmdConnect_Click()
Dim txtsql As String
Dim mrc As ADODB.Recordset
Dim msgtext As String
username = ""
If IsNull(Me.txtUsername) Then
MsgBox "没有这个用户或未受权,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUsername.SetFocus
Else
txtsql = "select * from dbo.职工资料 where 职工ID='" & txtUsername & "'"
Set mrc = executeSQL(txtsql, msgtext)
If mrc.EOF = True Then'运行到这里提示with块未设置
MsgBox "没有这个用户或未受权,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUsername.SetFocus
Else
If Trim(mrc.Fields(20)) = Trim(txtPassword.Text) Then
ok = True
username = Trim(mrc.Fields(1))
userid = Trim(txtUsername.Text)
mrc.Close
Else
MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtPassword.SetFocus
txtPassword.Text = ""
micount = micount + 1
If micount = 3 Then
MsgBox "输入密码不正确,已经3次!", vbOKOnly + vbExclamation, "警告"
DoCmd.Close
DoCmd.Quit
End If
End If
End If
End If
End Sub