我使用VB.NET和microsoft访问作为我的数据库,我是VB.NET的新手,我试图根据当前角色制作不同的访问级别。例如,登录后,Admin将继续执行页面如果用户登录,他们将被定向到另一个页面。我已经在我的数据库中设置了不同的角色,我在网上发现了一些编码来帮助我
但是我一直收到错误'UserRole'没有声明。由于其保护级别,它可能无法访问。我需要先声明UserRole吗?如果是的话,我该怎么做? 任何帮助将不胜感激,谢谢:)
Public Class Form1 Dim loginerror As String Dim UserRole As String 公共功能登录() Dim DBconn作为新的ADODB.Connection 昏暗的用户为新的ADODB.Recordset
Dim Username As String
Dim userDB As String
Dim passDB As String
Dim UserFound As Boolean
DBconn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = '" & Application.StartupPath & "\LoginDB.mdb'")
user.Open("UserTable", DBconn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
UserFound = False
login = False
Username = "Username = '" & txtuser.Text & "'" '
Do
user.Find(Username)
If user.BOF = False And user.EOF = False Then
userDB = user.Fields("Username").Value.ToString
passDB = user.Fields("Password").Value.ToString
If userDB <> txtuser.Text Then
user.MoveNext()
Else
UserFound = True
If passDB = txtpass.Text Then
UserRole = user.Fields("roles").Value.ToString
user.Close()
DBconn.Close()
Return True
Else
loginerror = "Invalid Password"
user.Close()
DBconn.Close()
Return False
End If
End If
Else
loginerror = "Invalid Username"
user.Close()
DBconn.Close()
Return False
End If
Loop Until UserFound = True
user.Close()
DBconn.Close()
Return False
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If login() = True And UserRole = "admin" Then
adminwelcome.Show()
Me.Close()
ElseIf login() = True Then
Welcome.Show()
Me.Close()
Else
MessageBox.Show(loginerror, "Login Message")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AcceptButton = Button1
Me.Show()
Application.DoEvents()
txtuser.Focus()
End Sub
Private Sub txtpass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.TextChanged
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub txtuser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuser.TextChanged
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
End Sub
结束班
答案 0 :(得分:0)
我假设您正在从数据库中提取 UserRole 。您收到此错误,因为尚未声明该变量。
我会提取该信息(用户的角色)并将其存储到名为 UserRole 的类级别变量中。然后,您将能够在Button1_Click子中访问它。
Public Class Form1
Dim loginerror As String
Dim UserRole As String
Public Function login()
Dim DBconn As New ADODB.Connection
Dim user As New ADODB.Recordset
Dim Username As String
Dim userDB As String
Dim passDB As String
Dim UserFound As Boolean
DBconn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = '" & Application.StartupPath & "\LoginDB.mdb'")
user.Open("UserTable", DBconn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
UserFound = False
login = False
Username = "Username = '" & txtuser.Text & "'" '
Do
user.Find(Username)
If user.BOF = False And user.EOF = False Then
userDB = user.Fields("Username").Value.ToString
passDB = user.Fields("Password").Value.ToString
If userDB <> txtuser.Text Then
user.MoveNext()
Else
UserFound = True
If passDB = txtpass.Text Then
UserRole = user.Fields("UserRole").Value.ToString
user.Close()
DBconn.Close()
Return True
Else
loginerror = "Invalid Password"
user.Close()
DBconn.Close()
Return False
End If
End If
Else
loginerror = "Invalid Username"
user.Close()
DBconn.Close()
Return False
End If
Loop Until UserFound = True
user.Close()
DBconn.Close()
Return False
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If login() = True And UserRole = "admin" Then
adminwelcome.Show()
Me.Close()
ElseIf login() = True Then
Welcome.Show()
Me.Close()
Else
MessageBox.Show(loginerror, "Login Message")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AcceptButton = Button1
Me.Show()
Application.DoEvents()
txtuser.Focus()
End Sub
Private Sub txtpass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.TextChanged
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub txtuser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuser.TextChanged
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
End Sub
End Class