天天看点

【VB实例】登录功能 连接Access/SQL Server数据库 Visual Basic 编程

作者:凌霄百科
【VB实例】登录功能 连接Access/SQL Server数据库 Visual Basic 编程

登录1

Private Sub Command登录_Click()

If Text密码 = "" Then

MsgBox "登录密码不能为空"

Exit Sub

End If

If Text密码 = "abc123" Then

MsgBox "登录成功"

Unload Form登录1

Form系统主页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数据库

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 '循环表的内容

On Error Resume Next

user_name = login_rs.Fields("账号").Value '账号赋值到公共变量

MsgBox "登录成功"

Unload Me '关闭登录窗体

Form系统主页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数据库

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 '循环表的内容

On Error Resume Next

If 密码text = login_rs.Fields("密码").Value Then

user_name = login_rs.Fields("账号").Value '账号赋值到公共变量

MsgBox "登录成功"

Unload Me '关闭登录窗体

Form系统主页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 '关闭登录窗体

Form系统主页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数据库

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 '循环表的内容

'On Error Resume Next

user_name = login_rs.Fields("账号").Value '账号赋值到公共变量

MsgBox "登录成功"

Unload Me '关闭登录窗体

Form系统主页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 '关闭登录窗体

Form系统主页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

系统主页

Private Sub Form_Load()

Text账号.Text = user_name

End Sub

公共变量

Public user_name As String '记录登录账号

继续阅读