如图,验证账号密码的VB代码怎么写呢
我是直接连接的数据库文件,但是不知道怎么写代码调用文件里的数据,combobox的填的也不知道对不对,不对的话求帮忙改正数据库是SQL的mdf文件直接连接的...
我是直接连接的数据库文件,但是不知道怎么写代码调用文件里的数据,combobox的填的也不知道对不对,不对的话求帮忙改正
数据库是SQL的mdf文件直接连接的 展开
数据库是SQL的mdf文件直接连接的 展开
2个回答
展开全部
下面代码是我自己写的,自己做学生管理用到的。你可以借鉴一下。
ExecuteSQL是另外建的模块,另起回复。
Private Sub Cmd_ok_Click()
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String
Dim UserName As String
miCount = miCount + 1
UserName = ""
If Trim(Txt_yhm.Text = "") Then
MsgBox "请输入用户名!", vbOKOnly + vbExclamation, "警告"
YZ = YZ + 1
Txt_yhm.SetFocus
Else
txtSQL = "select * from user_Info where user_ID = '" & Txt_yhm & "'"
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
YZ = YZ + 1
Txt_yhm.SetFocus
Else
If Trim(mrc.Fields(1)) = Trim(Txt_Mm.Text) Then
MFrm_Main.Show
OK = True
mrc.Close
Me.Hide
UserName = Trim(Txt_yhm.Text)
Else
MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
YZ = YZ + 1
Txt_Mm.SetFocus
Txt_Mm.Text = ""
End If
End If
End If
If miCount = 3 Then
If miCount = YZ Then
MsgBox "!", vbOKOnly + vbExclamation, "警告"
Me.Hide
Unload Me
Else
MFrm_Main.Show
End If
End If
Exit Sub
End Sub
------------------------------------------------------------------------------------------
Public FmainForm As MFrm_Main
Public UserName As String
Public Function ConnectString() As String
ConnectString = "FileDSN=student.dsn;UID=sa;PWD=19970807"
End Function
Sub main()
Dim flogin As New Frm_Login '显示登录船窗体实例
flogin.Show vbModal
If Not flogin.OK Then
End
End If
Unload flogin
Set FmainForm = New MFrm_Main
FmainForm.Show
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 '非Select语句
cnn.ExecuteSQL '数据量不大时,可以在连接上,直接执行SQL语句
MsgString = sTokens(0) & " query successful"
'虽然MsgString不是返回值,但传递方式是ByRef,实参地址和这个地址相同
Else 'Select语句
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
'得到临时表,游标指向第一条记录
'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
ExecuteSQL是另外建的模块,另起回复。
Private Sub Cmd_ok_Click()
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String
Dim UserName As String
miCount = miCount + 1
UserName = ""
If Trim(Txt_yhm.Text = "") Then
MsgBox "请输入用户名!", vbOKOnly + vbExclamation, "警告"
YZ = YZ + 1
Txt_yhm.SetFocus
Else
txtSQL = "select * from user_Info where user_ID = '" & Txt_yhm & "'"
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
YZ = YZ + 1
Txt_yhm.SetFocus
Else
If Trim(mrc.Fields(1)) = Trim(Txt_Mm.Text) Then
MFrm_Main.Show
OK = True
mrc.Close
Me.Hide
UserName = Trim(Txt_yhm.Text)
Else
MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
YZ = YZ + 1
Txt_Mm.SetFocus
Txt_Mm.Text = ""
End If
End If
End If
If miCount = 3 Then
If miCount = YZ Then
MsgBox "!", vbOKOnly + vbExclamation, "警告"
Me.Hide
Unload Me
Else
MFrm_Main.Show
End If
End If
Exit Sub
End Sub
------------------------------------------------------------------------------------------
Public FmainForm As MFrm_Main
Public UserName As String
Public Function ConnectString() As String
ConnectString = "FileDSN=student.dsn;UID=sa;PWD=19970807"
End Function
Sub main()
Dim flogin As New Frm_Login '显示登录船窗体实例
flogin.Show vbModal
If Not flogin.OK Then
End
End If
Unload flogin
Set FmainForm = New MFrm_Main
FmainForm.Show
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 '非Select语句
cnn.ExecuteSQL '数据量不大时,可以在连接上,直接执行SQL语句
MsgString = sTokens(0) & " query successful"
'虽然MsgString不是返回值,但传递方式是ByRef,实参地址和这个地址相同
Else 'Select语句
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
'得到临时表,游标指向第一条记录
'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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询