一、窗口界面設(shè)計 建立一個窗體,定義為login。 窗體分別加入以下控件: 1.1 選擇數(shù)據(jù)庫的控件--ComboBox 1.2 用戶名控件--Text1 1.3 密碼控件--Text2 注意:passwordchar設(shè)置為* 1.4 確定按鈕控件 1.5 【退出】 1.6 【注冊】 1.7 數(shù)據(jù)源控件 1.8 時鐘控件 二、建議一個pass.mdb數(shù)據(jù)庫 三、代碼編寫 Private Sub Command1_Click() '==============================本地數(shù)據(jù)庫== If Trim(sql_LR.text) = Trim("本地數(shù)據(jù)庫") Then Dim sql As String Dim conn As New ADODB.Connection Dim rs_login As New ADODB.Recordset Dim connectionstring As String connectionstring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Pass.mdb; Jet OLEDB:Database Password=csu" conn.Open connectionstring sql_local = sql_LR.text If Trim(username.text) = "" Then MsgBox "輸入的用戶名為空", vbOKOnly + vbExclamation, "錯誤??!" username.SetFocus Else sql = "select * from 登陸信息 where 用戶名='" & username.text & "'" rs_login.Open sql, conn, adOpenKeyset, adLockPessimistic If rs_login.EOF = True Then MsgBox "輸入的用戶名不存在", vbOKOnly + vbExclamation, "錯誤??!" username.text = "" username.SetFocus Else If Trim(rs_login.Fields(2)) = Trim(password.text) Then name1 = username.text rs_login.Close login.Hide main_w.Show sql_local = sql_LR.text Else MsgBox "密碼錯誤,請重新輸入", vbOKOnly + vbExclamation, "錯誤??!" password.SetFocus Set conn = Nothing End If End If End If End If '================云端數(shù)據(jù)庫 If Trim(sql_LR.text) <> Trim("本地數(shù)據(jù)庫") Then Dim sql2 As String Dim conn3 As New ADODB.Connection Dim rs As New ADODB.Recordset Dim connectionstring3 As String connectionstring3 = "Provider = MSDASQL.1;Persist Security Info=False;Data Source=aliyun_sql;Initial Catalog=soft_market" conn.Open connectionstring3 If Trim(Text1.text) = "" Then MsgBox "輸入的用戶名為空", vbOKOnly + vbExclamation, "錯誤!!" Text1.SetFocus Else sql2 = "select * from 登陸信息 where 用戶名='" & Text1.text & "'" rs.Open sql2, conn, adOpenKeyset, adLockPessimistic '=====授權(quán) If Trim(rs.Fields(6)) = Trim("1") Then remark = True End If If Trim(rs.Fields(6)) <> Trim("1") Then remark = False End If sql_local = sql_LR.text If rs.EOF = True Then MsgBox "輸入的用戶名不存在", vbOKOnly + vbExclamation, "錯誤??!" Text1.text = "" Text1.SetFocus Else If Trim(rs.Fields(2)) = Trim(password.text) Then name1 = Text1.text rs.Close Set conn3 = Nothing login.Hide main_w.Show sql_local = sql_LR.text Else MsgBox "密碼錯誤,請重新輸入", vbOKOnly + vbExclamation, "錯誤??!" password.SetFocus Set conn = Nothing End If End If End If End If End Sub Private Sub Form_Load() '====================選擇數(shù)據(jù)庫 sql_LR.text = "本地數(shù)據(jù)庫" username.Visible = True Text1.Visible = False With sql_LR .AddItem "本地數(shù)據(jù)庫" .AddItem "云端數(shù)據(jù)庫" End With '=============================獲取本地用戶名,初始化 If Trim(sql_LR.text) = Trim("本地數(shù)據(jù)庫") Then Dim Cnn1 As ADODB.Connection Dim Rst1 As ADODB.Recordset Set Cnn1 = New ADODB.Connection Dim connectionstring2 As String connectionstring2 = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Price.mdb; Jet OLEDB:Database Password=csu" Cnn1.Open connectionstring2 Set Rst1 = New ADODB.Recordset Rst1.CursorType = adOpenKey Dim connstring3 As String connstring3 = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Pass.mdb; Jet OLEDB:Database Password=csu" Adodc1.connectionstring = connstring3 Adodc1.CommandType = adCmdText Adodc1.RecordSource = "select * from 登陸信息" Adodc1.Refresh If Adodc1.Recordset.RecordCount > 0 Then username.Clear Adodc1.Recordset.MoveFirst Do While Not Adodc1.Recordset.EOF username.AddItem (Adodc1.Recordset.Fields(1)) '這里把字段名修改成數(shù)據(jù)表里的實際字段名 Adodc1.Recordset.MoveNext Loop Adodc1.Recordset.MoveFirst End If username.text = Adodc1.Recordset.Fields(1) End If End Sub Private Sub Form_Unload(Cancel As Integer) End End Sub Private Sub reg_Click() reg_w.Show End Sub Private Sub sql_LR_Click() If Trim(sql_LR.text) <> Trim("本地數(shù)據(jù)庫") Then reg.Visible = True username.Visible = False Text1.Visible = True End If If Trim(sql_LR.text) = Trim("本地數(shù)據(jù)庫") Then reg.Visible = False username.Visible = True Text1.Visible = False End If '====================================獲取云端用戶名 If Trim(sql_LR.text) <> Trim("本地數(shù)據(jù)庫") Then Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Set cn = ConnMySQL("honeytree", "Honey123!", "soft_maket", "47.99.45.61") If cn.State = adStateOpen Then MsgBox "打開遠(yuǎn)程MySQL數(shù)據(jù)庫成功" If cn Is Nothing Then If Err.Number <> 0 Then MsgBox Err.Number & " " & Err.Description, vbOKOnly & "連接MySQL發(fā)生錯誤" MsgBox "連接成功", vbOKOnly & "提示" Else MsgBox "連接失敗", vbOKOnly & "提示" End If Exit Sub End If '云端用戶名 Dim connstring1 As String connstring1 = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Pass.mdb; Jet OLEDB:Database Password=csu" Adodc1.connectionstring = connstring1 Adodc1.CommandType = adCmdText Adodc1.RecordSource = "select * from 登陸信息" Adodc1.Refresh If Adodc1.Recordset.RecordCount > 0 Then username.Clear Adodc1.Recordset.MoveFirst Do While Not Adodc1.Recordset.EOF username.AddItem (Adodc1.Recordset.Fields(1)) '這里把字段名修改成數(shù)據(jù)表里的實際字段名 Adodc1.Recordset.MoveNext Loop Adodc1.Recordset.MoveFirst End If username.text = Adodc1.Recordset.Fields(1) End If '=============================獲取本地用戶名 If Trim(sql_LR.text) = Trim("本地數(shù)據(jù)庫") Then Dim Cnn1 As ADODB.Connection Dim Rst1 As ADODB.Recordset Set Cnn1 = New ADODB.Connection Dim connectionstring2 As String connectionstring2 = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Price.mdb; Jet OLEDB:Database Password=csu" Cnn1.Open connectionstring2 Set Rst1 = New ADODB.Recordset Rst1.CursorType = adOpenKey Dim connstring3 As String connstring3 = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Pass.mdb; Jet OLEDB:Database Password=csu" Adodc1.connectionstring = connstring3 Adodc1.CommandType = adCmdText Adodc1.RecordSource = "select * from 登陸信息" Adodc1.Refresh If Adodc1.Recordset.RecordCount > 0 Then username.Clear Adodc1.Recordset.MoveFirst Do While Not Adodc1.Recordset.EOF username.AddItem (Adodc1.Recordset.Fields(1)) '這里把字段名修改成數(shù)據(jù)表里的實際字段名 Adodc1.Recordset.MoveNext Loop Adodc1.Recordset.MoveFirst End If username.text = Adodc1.Recordset.Fields(1) End If End Sub Private Sub Timer1_Timer() login.Caption = "歡迎使用" & " " & "現(xiàn)在時間是:" & " " & Now() & " " & weekday1 End Sub Private Sub 取消_Click() 'MsgBox "您已成功退出!", vbOKOnly + vbExclamation, "提示" Unload Me End Sub 四、運行效果 點擊用戶名的選擇,可以看到數(shù)據(jù)庫已經(jīng)有的賬號 注冊一個賬號 |
|
來自: 機(jī)電工控交流 > 《待分類》