【VB實例】登錄功能 連接Access/SQL Server數(shù)據(jù)庫 Visual Basic 編程


登錄1
Private Sub Command登錄_Click()
If Text密碼 = "" Then
MsgBox "登錄密碼不能為空"
Exit Sub
End If
If Text密碼 = "abc123" Then
? ?MsgBox "登錄成功"
? ?Unload Form登錄1
? ?Form系統(tǒng)主頁1.Show 1
Else
? ?MsgBox "登錄密碼不正確"
End If
End Sub
登錄2
Public login_count As Long
Private Sub Command登錄_Click()
Dim 賬號text As String? '定義變量存儲賬號
Dim 密碼text As String? '定義變量存儲密碼
If Trim(Text賬號) <> "" Then? '輸入賬號不能為空
賬號text = Me.Text賬號? '存儲錄入賬號到變量中(可拓展更多判斷,如字符長度等)
Else
MsgBox "賬號不能為空!"
Exit Sub
End If
If Trim(Me.Text密碼) <> "" Then? '輸入密碼不能為空
? ? If Len(Trim(Me.Text密碼)) < 6 Then
? ? ? ? MsgBox "密碼長度不能小于6位!"
? ? ? ? Exit Sub
? ? End If
密碼text = Me.Text密碼? '存儲錄入密碼到變量中(可拓展更多判斷,如字符長度等)
Else
MsgBox "密碼不能為空!"
Exit Sub
End If
'-賬號密碼驗證
Dim login_conn As New ADODB.Connection? '連接到ACCESS數(shù)據(jù)庫
With login_conn? ? ? ? ?'mdb格式連接
? ? .ConnectionString = "Provider = microsoft.jet.oledb.4.0;data source=" & App.Path & "\db_data.mdb;Jet OLEDB:DataBase password=abc123;persist security info=false"
? ? .Open
End With
Dim login_rs As New ADODB.Recordset
Dim login_sql As String
login_sql = "select * from 用戶表 where 賬號= '" & Me.Text賬號 & "' and 密碼='" & Me.Text密碼 & "'"? ? ?'查詢用戶表
login_rs.Open login_sql, login_conn, adOpenDynamic, adLockOptimistic
If login_rs.EOF = False Then '循環(huán)表的內(nèi)容
On Error Resume Next
user_name = login_rs.Fields("賬號").Value? ? ? '賬號賦值到公共變量
MsgBox "登錄成功"
Unload Me? '關(guān)閉登錄窗體
Form系統(tǒng)主頁2.Show 1
Else
MsgBox "賬號或密碼錯誤,請重新登錄"
login_count = login_count + 1? ?'登錄錯誤3次,退出
? ? If login_count = 3 Then
? ? ? ? MsgBox "賬號或密碼錯誤達3次"
? ? ? ? Unload Me
? ? End If
End If
login_rs.Close
Set login_rs = Nothing
login_conn.Close
Set login_conn = Nothing
Exit Sub
登錄3
Public login_count As Long
Private Sub Command登錄_Click()
Dim 賬號text As String? '定義變量存儲賬號
Dim 密碼text As String? '定義變量存儲密碼
If Trim(Text賬號) <> "" Then? '輸入賬號不能為空
賬號text = Me.Text賬號? '存儲錄入賬號到變量中(可拓展更多判斷,如字符長度等)
Else
MsgBox "賬號不能為空!"
Exit Sub
End If
If Trim(Me.Text密碼) <> "" Then? '輸入密碼不能為空
? ? If Len(Trim(Me.Text密碼)) < 6 Then
? ? ? ? MsgBox "密碼長度不能小于6位!"
? ? ? ? Exit Sub
? ? End If
密碼text = Me.Text密碼? '存儲錄入密碼到變量中(可拓展更多判斷,如字符長度等)
Else
MsgBox "密碼不能為空!"
Exit Sub
End If
'-賬號密碼驗證
Dim login_conn As New ADODB.Connection? '連接到ACCESS數(shù)據(jù)庫
With login_conn? ? ? ? ?'mdb格式連接
? ? .ConnectionString = "Provider = microsoft.jet.oledb.4.0;data source=" & App.Path & "\db_data.mdb;Jet OLEDB:DataBase password=abc123;persist security info=false"
? ? .Open
End With
Dim login_rs As New ADODB.Recordset
Dim login_sql As String
login_sql = "select * from 用戶表 where 賬號= '" & Me.Text賬號 & "'"? ? ?'查詢用戶表
login_rs.Open login_sql, login_conn, adOpenDynamic, adLockOptimistic
If login_rs.EOF = False Then '循環(huán)表的內(nèi)容
On Error Resume Next
? ? If 密碼text = login_rs.Fields("密碼").Value Then
? ? ? ? user_name = login_rs.Fields("賬號").Value? ? ? '賬號賦值到公共變量
? ? ? ? MsgBox "登錄成功"
? ? ? ? Unload Me? '關(guān)閉登錄窗體
? ? ? ? Form系統(tǒng)主頁3.Show 1
? ? Else
? ? ? ? MsgBox "密碼錯誤"
? ? ? ? login_count = login_count + 1? ?'登錄錯誤3次,退出
? ? ? ? If login_count = 3 Then
? ? ? ? ? ? MsgBox "密碼錯誤達3次"
? ? ? ? ? ? Unload Me
? ? ? ? End If
? ? End If
Else
MsgBox "賬號不存在"
End If
login_rs.Close
Set login_rs = Nothing
login_conn.Close
Set login_conn = Nothing
Exit Sub
登錄失敗錯誤:
MsgBox Err.Description
End Sub
登錄4
Private Sub Command登錄_Click()
Dim 賬號text As String? '定義變量存儲賬號
Dim 密碼text As String? '定義變量存儲密碼
If Trim(Text賬號) <> "" Then? '輸入賬號不能為空
賬號text = Me.Text賬號? '存儲錄入賬號到變量中(可拓展更多判斷,如字符長度等)
Else
MsgBox "賬號不能為空!"
Exit Sub
End If
If Trim(Me.Text密碼) <> "" Then? '輸入密碼不能為空
? ? If Len(Trim(Me.Text密碼)) < 6 Then
? ? ? ? MsgBox "密碼長度不能小于6位!"
? ? ? ? Exit Sub
? ? End If
密碼text = Me.Text密碼? '存儲錄入密碼到變量中(可拓展更多判斷,如字符長度等)
Else
MsgBox "密碼不能為空!"
Exit Sub
End If
'-賬號密碼驗證
Adodc1.RecordSource = "Select * from 用戶表 Where 賬號='" & 賬號text & "'"? ? '篩選ADO控件記錄
Adodc1.Refresh
If Adodc1.Recordset.RecordCount = 1 Then '
? ? If 密碼text = Adodc1.Recordset.Fields("密碼").Value Then
? ? ? ? user_name = 賬號text? ? ? '賬號賦值到公共變量
? ? ? ? MsgBox "登錄成功"
? ? ? ? Unload Me? '關(guān)閉登錄窗體
? ? ? ? Form系統(tǒng)主頁3.Show 1
? ? Else
? ? ? ? MsgBox "密碼錯誤"
? ? ? ? login_count = login_count + 1? ?'登錄錯誤3次,退出
? ? ? ? If login_count = 3 Then
? ? ? ? ? ? MsgBox "密碼錯誤達3次"
? ? ? ? ? ? Unload Me
? ? ? ? End If
? ? End If
Else
MsgBox "賬號不存在"
End If
Exit Sub
登錄失敗錯誤:
MsgBox Err.Description
End Sub
登錄5
Public login_count As Long
Private Sub Command登錄_Click()
Dim 賬號text As String? '定義變量存儲賬號
Dim 密碼text As String? '定義變量存儲密碼
If Trim(Text賬號) <> "" Then? '輸入賬號不能為空
賬號text = Me.Text賬號? '存儲錄入賬號到變量中(可拓展更多判斷,如字符長度等)
Else
MsgBox "賬號不能為空!"
Exit Sub
End If
If Trim(Me.Text密碼) <> "" Then? '輸入密碼不能為空
? ? If Len(Trim(Me.Text密碼)) < 6 Then
? ? ? ? MsgBox "密碼長度不能小于6位!"
? ? ? ? Exit Sub
? ? End If
密碼text = Me.Text密碼? '存儲錄入密碼到變量中(可拓展更多判斷,如字符長度等)
Else
MsgBox "密碼不能為空!"
Exit Sub
End If
'-賬號密碼驗證
Dim login_conn As New ADODB.Connection? '連接到ACCESS數(shù)據(jù)庫
With login_conn? ? ? ? ?'連接
? ? .ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=123123;Initial Catalog=test1;Data Source=(local)"
? ? .Open
End With
Dim login_rs As New ADODB.Recordset
Dim login_sql As String
login_sql = "select * from 用戶表 where 賬號= '" & Me.Text賬號 & "' and 密碼='" & Me.Text密碼 & "'"? ? ?'查詢用戶表
login_rs.Open login_sql, login_conn, adOpenDynamic, adLockOptimistic
If login_rs.EOF = False Then '循環(huán)表的內(nèi)容
'On Error Resume Next
user_name = login_rs.Fields("賬號").Value? ? ? '賬號賦值到公共變量
MsgBox "登錄成功"
Unload Me? '關(guān)閉登錄窗體
Form系統(tǒng)主頁3.Show 1
Else
MsgBox "賬號或密碼錯誤,請重新登錄"
login_count = login_count + 1? ?'登錄錯誤3次,退出
? ? If login_count = 3 Then
? ? ? ? MsgBox "賬號或密碼錯誤達3次"
? ? ? ? Unload Me
? ? End If
End If
login_rs.Close
Set login_rs = Nothing
login_conn.Close
Set login_conn = Nothing
Exit Sub
登錄6
Public login_count As Long
Private Sub Command登錄_Click()
Dim 賬號text As String? '定義變量存儲賬號
Dim 密碼text As String? '定義變量存儲密碼
If Trim(Text賬號) <> "" Then? '輸入賬號不能為空
賬號text = Me.Text賬號? '存儲錄入賬號到變量中(可拓展更多判斷,如字符長度等)
Else
MsgBox "賬號不能為空!"
Exit Sub
End If
If Trim(Me.Text密碼) <> "" Then? '輸入密碼不能為空
? ? If Len(Trim(Me.Text密碼)) < 6 Then
? ? ? ? MsgBox "密碼長度不能小于6位!"
? ? ? ? Exit Sub
? ? End If
密碼text = Me.Text密碼? '存儲錄入密碼到變量中
Else
MsgBox "密碼不能為空!"
Exit Sub
End If
'-賬號密碼驗證
Adodc1.RecordSource = "Select * from 用戶表 Where 賬號='" & 賬號text & "'"? ? '篩選ADO控件記錄
Adodc1.Refresh
If Adodc1.Recordset.RecordCount = 1 Then '
'? ? MsgBox 密碼text & Adodc1.Recordset.Fields("密碼").Value
? ? Dim A1 As String
? ? A1 = CStr(Adodc1.Recordset.Fields("密碼").Value)
? ? If 密碼text = Trim(A1) Then
? ? ? ? user_name = 賬號text? ? ?'賬號賦值到公共變量
? ? ? ? MsgBox "登錄成功"
? ? ? ? Unload Me? '關(guān)閉登錄窗體
? ? ? ? Form系統(tǒng)主頁3.Show 1
? ? Else
? ? ? ? MsgBox "密碼錯誤"
? ? ? ? login_count = login_count + 1? ?'登錄錯誤3次,退出
? ? ? ? If login_count = 3 Then
? ? ? ? ? ? MsgBox "密碼錯誤達3次"
? ? ? ? ? ? Unload Me
? ? ? ? End If
? ? End If
Else
MsgBox "賬號不存在"
End If
Exit Sub
登錄失敗錯誤:
MsgBox Err.Description
End Sub
系統(tǒng)主頁
Private Sub Form_Load()
Text賬號.Text = user_name
End Sub
公共變量
Public user_name As String? '記錄登錄賬號