2013-07-15 54 views
0

我已经创建了一个系统,而且我也是vb.net的新手。它工作正常。问题是,我需要限制1.)用户和2.)管理主页。VB.NET上的访问级别

例如,在登录后,管理员将进行 主页,如果用户登录时,他们将被引导到不同的页面

我还是新来vb.net(如6周前),我使用微软访问作为我的数据库。我在网上找到的很多代码是非常复杂和技术性的。我只需要简单的vb.net代码

任何帮助将不胜感激,谢谢! 我已经发布我的代码为我的登录表单,所以你们可以进一步了解我的编码:

Public Class Form1 
    Dim loginerror 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 
        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 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 

以防万一,这是我为我的主要形式编码:

Public Class Mainform 
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
    Select MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) 
     Case Windows.Forms.DialogResult.Yes 
     Case Windows.Forms.DialogResult.No 
      e.Cancel = True 
    End Select 

End Sub 

Private Sub Mainform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Me.WorkTableAdapter.Fill(Me.LoginDBDataSet.Work) 

    Me.AssetTableAdapter.Fill(Me.LoginDBDataSet.Asset) 

    Me.HistoryTableAdapter.Fill(Me.LoginDBDataSet.History) 

    Me.InventoryTableAdapter.Fill(Me.LoginDBDataSet.Inventory) 

    Me.Equipment1TableAdapter.Fill(Me.LoginDBDataSet.Equipment1) 


End Sub 
Private Sub mainform_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown 
    If e.KeyCode = Keys.Escape Then Me.Close() 

End Sub 

Private Sub Equipment1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Equipment1BindingNavigatorSaveItem.Click 
    Me.Validate() 
    Me.Equipment1BindingSource.EndEdit() 
    Me.TableAdapterManager.UpdateAll(Me.LoginDBDataSet) 

End Sub 

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click 
    Mainform_Load(Me, New System.EventArgs) 
End Sub 

Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click 
    Form1.Show() 
    Me.Dispose() 
    Me.Close() 

End Sub 

Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click 

End Sub 

Private Sub IDLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 

End Sub 

Private Sub TabPage5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage5.Click 

End Sub 

Private Sub Equipment_NameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 

End Sub 

Private Sub HistoryDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) 

End Sub 

Private Sub InventoryDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) 

End Sub 

Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged 

End Sub 

Private Sub Machine_IDTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 

End Sub 

Private Sub _WO_CompletedLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 

End Sub 

Private Sub _WO_CompletedCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 

End Sub 

Private Sub _WO_CompletedCheckBox_CheckedChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) 

End Sub 

Private Sub Machine_IDTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Machine_IDTextBox1.TextChanged 

End Sub 

Private Sub _WO_CompletedCheckBox_CheckedChanged_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _WO_CompletedCheckBox.CheckedChanged 

End Sub 
End Class 

回答

1

您需要确定如果用户是管理员或不(通常是你的user表应该附加的用户如管理,员工等角色)

这样的事情,当你尝试登录

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
If login() = True and UserRole='Administrator' Then 
    Welcome.Show() 
    Me.Close() 
elseif login() = True then 
    WelcomeUser.Show() 
    Me.Close() 
Else 
    MessageBox.Show(loginerror, "Login Message") 
End If 

末次

+0

是谢谢你,我做了什么,你告诉我,但我不断收到“UserRole的”未声明的错误。由于它的保护级别,它可能无法访问 – Greenhorn

+0

UserRole只是我的术语,可能是用户表中字符串的值。例如,您的用户表有一个名为UserRole的列,其值为员工,管理员等。获取登录的特定用户的价值,然后检查他是否是管理员(如我上面的示例中所述),然后打开管理员的表单。或者如果没有其他情况,请为普通用户打开表单。 –