登陆 Private Sub cmdOK_Click() Dim txtSQL As String Dim mrc As ADODB.Recordset Dim MsgText As String 'ToDo: create test for correct password 'check for correct password
UserName = "" If Trim(txtUserName.Text = "") Then MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告" txtUserName.SetFocus Else txtSQL = "select * from ypusers where user_ID = '" & txtUserName.Text & "'" Set mrc = ExecuteSQL(txtSQL, MsgText) If mrc.EOF = True Then MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告" txtUserName.SetFocus Else If Trim(mrc.Fields(1)) = Trim(txtPassword.Text) Then OK = True mrc.Close Me.Hide UserName = Trim(txtUserName.Text) Else MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告" txtPassword.SetFocus txtPassword.Text = "" End If End If End If
miCount = miCount + 1 If miCount = 3 Then Me.Hide End If Exit Sub End Sub 模块 Public Function ExecuteSQL(ByVal SQL _ As String, MsgString As String) _ As ADODB.Recordset 'executes SQL and returns 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 Testtxt(txt As String) As Boolean If Trim(txt) = "" Then Testtxt = False Else Testtxt = True End If End Function Sub Main() Dim fLogin As New frmLogin fLogin.Show vbModal If Not fLogin.OK Then 'Login Failed so exit app End End If Unload fLogin Set fMainForm = New frmMain fMainForm.Show End SubPublic Function ConnectString() _ As String 'returns a DB ConnectString ConnectString = "FileDSN=ypMis.dsn;UID=gcmrp_sys;PWD=" End Function注:在部分电脑上运行正常
建义不使用dsn连接文件,你改用ADO连接字符串。
If Trim(txtUserName.Text = "") Then MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告" txtUserName.SetFocus Else txtSQL = "select * from ypusers where user_ID = '" & txtUserName.Text & "'" set mrc = new adodb.recordset '这一行是我加的,你试试吧。我比较同意楼上的看法,还是用ADO连接比较好。 Set mrc = ExecuteSQL(txtSQL, MsgText) If mrc.EOF = True Then MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告" txtUserName.SetFocus
我感觉用ADO连接比较好, dim cn as new adodb.connection with cn .provider="microsoft ole db provider for sql server" .connectionstring="server=(local);uid=sa;pwd=;database=platform" .open end with dim rs as new adodb.recordset with rs .source="select *******" .activeconnection=cn .cursorlocation=aduserclient .locktype=adopendynamic .locktype=adlockpessimistic .open end with
Private Sub cmdOK_Click()
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String
'ToDo: create test for correct password
'check for correct password
UserName = ""
If Trim(txtUserName.Text = "") Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
txtSQL = "select * from ypusers where user_ID = '" & txtUserName.Text & "'"
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = True Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
If Trim(mrc.Fields(1)) = Trim(txtPassword.Text) Then
OK = True
mrc.Close
Me.Hide
UserName = Trim(txtUserName.Text)
Else
MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtPassword.SetFocus
txtPassword.Text = ""
End If
End If
End If
miCount = miCount + 1
If miCount = 3 Then
Me.Hide
End If
Exit Sub
End Sub
模块
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns 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 Testtxt(txt As String) As Boolean
If Trim(txt) = "" Then
Testtxt = False
Else
Testtxt = True
End If
End Function
Sub Main()
Dim fLogin As New frmLogin
fLogin.Show vbModal
If Not fLogin.OK Then
'Login Failed so exit app
End
End If
Unload fLogin
Set fMainForm = New frmMain
fMainForm.Show
End SubPublic Function ConnectString() _
As String
'returns a DB ConnectString
ConnectString = "FileDSN=ypMis.dsn;UID=gcmrp_sys;PWD="
End Function注:在部分电脑上运行正常
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
txtSQL = "select * from ypusers where user_ID = '" & txtUserName.Text & "'"
set mrc = new adodb.recordset '这一行是我加的,你试试吧。我比较同意楼上的看法,还是用ADO连接比较好。
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = True Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
ConnectString = "FileDSN=ypMis.dsn"
'============================
cnn.Open ConnectString,txtUserName.Text,txtPassword.Text
dim cn as new adodb.connection
with cn
.provider="microsoft ole db provider for sql server"
.connectionstring="server=(local);uid=sa;pwd=;database=platform"
.open
end with
dim rs as new adodb.recordset
with rs
.source="select *******"
.activeconnection=cn
.cursorlocation=aduserclient
.locktype=adopendynamic
.locktype=adlockpessimistic
.open
end with