Click here to Skip to main content
15,667,885 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
I want to know how to redirect on admin form or user form etc on the login form by using by using VB.Net and access database

What I have tried:

Private Sub Btlogin_Click(sender As System.Object, e As System.EventArgs) Handles Btlogin.Click
        Dim warning As String = "Warning!" & vbNewLine & vbNewLine + _
            "If attempt var = 3" + vbNewLine + _
            "System will shut down."
        If (Textuser.Text = "") And (Textpassword.Text = "") Then
            attempt = attempt + 1
            MsgBox("Please input username and password!" & vbNewLine & vbNewLine + _
                   warning, MsgBoxStyle.Exclamation, "Soryy!" & _
                   "Now attempt var = " & attempt)
        ElseIf (Textuser.Text = "") Then
            attempt = attempt + 1
            MsgBox("Please input username!" & vbNewLine & vbNewLine + _
                   warning, MsgBoxStyle.Information, "Soryy!" & _
                   "Now attempt var = " & attempt)
        ElseIf (Textpassword.Text = "") Then
            attempt = attempt + 1
            MsgBox("Please input password!" & vbNewLine & vbNewLine + _
                   warning, MsgBoxStyle.Information, "Soryy ! " & _
                   "Now attempt = " & attempt)
            Dim strname = Textuser.Text
            Dim strpass = Textpassword.Text
            With objcon
                If .State = ConnectionState.Closed Then
                    .ConnectionString = strconnection
                    ' MsgBox("connectionState.Open", MsgBoxStyle.Information, "Connected")
                End If
            End With
            strSQL = "Select * From admin Where user_name='" & Textuser.Text & "' And password = '" & Textpassword.Text & "'"
            da = New OleDbDataAdapter(strSQL, objcon)
            da.Fill(ds, "admin")

            If ds.Tables("admin").Rows.Count <> 0 Then
                'play Sound
                My.Computer.Audio.Play(Application.StartupPath + "\Sound\kamal.wav")
                MaximizeBox = True
                MinimizeBox = True
                MsgBox("Log in Successful!", MsgBoxStyle.OkOnly, _
                       "Welcome " + strname)
                Me.Visible = False
                MaximizeBox = False
                MinimizeBox = False
                attempt = attempt +
                    MsgBox("Ooop!" & strname & " _&_ " & strpass + vbNewLine + vbNewLine + _
                           "The username and password you entered" + vbNewLine + _
                           "Is not valid" + vbNewLine + _
                           "Please try again!", MsgBoxStyle.Exclamation, "Invalid")
            End If
        End If

        If attempt = 3 Then
            MsgBox("Windows is shutting down...", MsgBoxStyle.Critical, "Good bye!")
        End If

    End Sub
Updated 16-Jul-18 2:25am
CHill60 16-Jul-18 8:25am    
What is wrong with what you have tried?
Richard Deeming 17-Jul-18 12:18pm    
SQL Injection; plain-text password storage; selecting all columns from the table just to test how many rows were returned...

I think the better question would be what isn't wrong with the code! :)
CHill60 17-Jul-18 12:28pm    
Oh I agree. I didn't even bother analysing the code - I was trying to get the OP to explain what their problem was rather than the rather tiring "I don't know how and it's not in Google" scenario

1 solution

Don't know what problem you think you have, but the ones I can see are far worse.

1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you? And on a login page? That's just plain stupid as it doesn't even require me to log in to destroy your DB...

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - the code is C#, but it's pretty obvious.

Fix them throughout your app, and then start worrying about moving on - if you don't you will lose your DB.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900