user authentication in visual basic 2005

Hi,
I have added a login form to my visual basic 2005 application with an Access table as a backbone, and have added the following code under the SUBMIT button.

If UserName.Text = "test" AndAlso Password.Text = "test" Then
        Dim frmMain As New Main
       frmMain.Show()
       Me.Close()
      End If

However, what I need is for direct access authentication without me filling in "test" for username and password.
Can someone please help me as it is very urgent.
LVL 1
systems_axAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Munawar HussainPrincipal Software EngineerCommented:
in Winform application for custome and easy way to secure app is using the way as you are doing above. .. but if you dont want to fill it out then why you have login screen .. ? just make the frmMain as startup form?

if you want it should be autologed in then .. you may may fill the fields at form load
and then just hit the enter button ..

thanks
0
Jorge PaulinoIT Pro/DeveloperCommented:
If I understoud right you want to retrive from anyplace the name and pass. I'm right ?
0
systems_axAuthor Commented:
Hi jpaulino,
I did an extensive research and followed the path Microsoft had provided within code with no vail
' (See http://go.microsoft.com/fwlink/?LinkId=35339).  

i do not want the authorization to be dependent on the values in code such as if username ="lastname" and password="password"..

. So, I need to develop the code in design time that lets the user enter  "whatever" and match what they enter against the access database username and password.  Can you please help me with that?

I am leaning towards something like this:

Dim fldUserID As String
Dim fldPassword As String
If fldUserID = "fldUserID Like '%" And fldPassword = "fldPassword Like '%" Then
Dim frmMain As New Main
frmMain.Show()
Me.Close()

and of course this code does not work.
thank you.

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Jorge PaulinoIT Pro/DeveloperCommented:
Ok and where do you want to store the username and password (access, SQL, Oracle, file, ...) ?
0
systems_axAuthor Commented:
Hi jpaulino,
I have an Access table:
tblUsers:
fldUserID and fldPassword columns only.

thank you
0
Jorge PaulinoIT Pro/DeveloperCommented:
Try something like this:

Dim query As String = "SELECT * FROM tblUsers WHERE fldUserID = '" & UserName.Text & "' AND fldPassword = '" & Password.Text "'"
Dim strConn As String = strConnection ' where strConnection is your connection string
Dim conn As OleDbConnection = New OleDbConnection(strConn)
Dim cmd As OleDbCommand = New OleDbCommand(query, conn)
Dim x As Integer = cmd.ExecuteNonQuery()
If x >= 1 Then
   MessageBox.Show("Valid login")
Else
   MessageBox.Show("The username or password are not valid!")
End If
conn.Close
0
systems_axAuthor Commented:
jpaulino,
it does not work

the message ""The username or password are not valid!" always comes up.  I did set up all the connections that are appropriate.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Ok,

Have you compare the values from the db and the values in the textboxes ?

Attention the the uppercase and lowercases.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Also you can try to change tho this (just to tests) and read the values from the db. If you think that the problem is the sql statement then you may also change to "SELECT * FROM tblUsers"

Dim query As String = "SELECT * FROM tblUsers WHERE fldUserID = '" & UserName.Text & "' AND fldPassword = '" & Password.Text "'"
Dim strConn As String = strConnection ' where strConnection is your connection string
Dim conn As OleDbConnection = New OleDbConnection(strConn)
Dim cmd As OleDbCommand = New OleDbCommand(query, conn)
Dim reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While OleDbDataReader.Read()
     MessageBox.Show(reader("fldUserID").ToString)
     MessageBox.Show(reader("fldPassword").ToString)
End While
conn.Close
0
systems_axAuthor Commented:
when I ran your original code, it said that "ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.",
So then I put in " conn.Open():
I receive "Syntax error in string in query expression 'fldUserID = 'paper' AND fldPassword = 'paper."
any ideas?
0
Jorge PaulinoIT Pro/DeveloperCommented:
Opps I forgot to open the connection! Well and what are the result of the query ?  Can you post here what you have now ?
0
systems_axAuthor Commented:
Jpaulino,
thank you for replying, here is what I have in its entirety:
Please let me know what I am doing wrong.

I added and changed the code and now I am receiving ""The username or password are not valid!" with the current set up, so the program does not break but it gives me a message that I am not entering the correct information,which I am.
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data.Common
Imports System.Windows.Forms
 
Public Class LoginUser
    Private ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\test.mdb"
   
    Private Conn As New OleDbConnection(ConStr)
 
#Region "Connection"
    Private Sub OpenDB()
        If Conn.State = ConnectionState.Closed Then
            Try
                Conn.Open()
            Catch ex As Exception
                MsgBox("Error: " & ex.Message, MsgBoxStyle.Exclamation, "Connection Error")
            End Try
        End If
    End Sub
    Private Sub CloseDB()
        If Conn.State = ConnectionState.Open Then
            Try
                Conn.Close()
            Catch ex As Exception
                MsgBox("Error: " & ex.Message, MsgBoxStyle.Exclamation, "Connection Error")
            End Try
        End If
    End Sub
#End Region
 
 
    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.
 
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        'Me.Close()
        Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\test.mdb")
        conn.Open()
 
        Dim query As String = "SELECT * FROM tblUsers WHERE fldUserID = '" & txtUserName.Text & "' AND fldPassword = '" & txtPassword.Text & "'"
 
 
       Dim cmd As OleDbCommand = New OleDbCommand(query, Conn)
        Dim x As Integer = cmd.ExecuteNonQuery()
        If x >= 1 Then
            MessageBox.Show("Valid login")
        Else
            MessageBox.Show("The username or password are not valid!")
        End If
        Conn.Close()
    End Sub
 
    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub
 
End Class

Open in new window

0
Jorge PaulinoIT Pro/DeveloperCommented:
Everything seams ok. Can you upload the file and the db ?
0
systems_axAuthor Commented:
I receive error "The extension of the uploaded file is not in the list of accepted extensions." when attempting to launch the application.

have you had a chance to test the solution yourself? I am lost as to why I keep receiving "invalid login" message.
0
Jorge PaulinoIT Pro/DeveloperCommented:
>> have you had a chance to test the solution yourself?
I have allot of application with something similiar (in access and SQL).

Can you upload the project and db in www.ee-stuff.com ?
0
systems_axAuthor Commented:
jpaulino,
I have been working on this one screen all day and still cannot figure it out.
I tried uploading but there is unknown file that I have that is not being liked.  I even renamed the mdb to bak with no vail.  It will not let me upload
0
Jorge PaulinoIT Pro/DeveloperCommented:
You have to zip the files
0
systems_axAuthor Commented:
I did it turns out that these are the only allowed extensions: bmp doc gif jpeg jpg log pdf png txt xls.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Go to www.ee-stuff.com and login. Then zip your files and upload it. You have to make some comments and indicate this question code 23154298.

Then you will recieve a direct link to the file.

Post that here.
0
systems_axAuthor Commented:
thank you, I did and here are the links:
View all files for Question ID: 23154298
https://filedb.experts-exchange.com/incoming/ee-stuff/6805-WindowsApplication1.zip

Direct link to your file
https://filedb.experts-exchange.com/incoming/ee-stuff/6805-WindowsApplication1.zip 

thank you again
0
Jorge PaulinoIT Pro/DeveloperCommented:
I'm terrible sorry but I didn't notice one thing ... you are trying to select and ExecuteNonQuery is just for UPDATE, INSERT, or DELETE. I'm sorry once again :-(

So many hours in the computer that the brain is crushing.

I will post the code tested! (one minute)
0
Jorge PaulinoIT Pro/DeveloperCommented:
I'm sorry once again. Here's the code changed
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\WindowsApplication1\WindowsApplication1\WindowsApplication1\test.mdb")
        Dim reader As OleDbDataReader
 
        Try
            conn.Open()
            Dim query As String = "SELECT * FROM tblUsers WHERE fldUserID = """ & txtUserName.Text & """ AND fldPassword = """ & txtPassword.Text & """"
            Dim cmd As OleDbCommand = New OleDbCommand(query, conn)
            reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            If reader.HasRows Then
                MessageBox.Show("Valid login")
            Else
                MessageBox.Show("The username or password are not valid!")
            End If
            conn.Close()
            reader.Close()
 
        Catch ex As Exception
            MessageBox.Show(ex.Message)
 
        Finally
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
            reader = Nothing
            conn = Nothing
        End Try
 
    End Sub

Open in new window

0
Jorge PaulinoIT Pro/DeveloperCommented:
I didn't sow your db. I had to create a new one.

Don't forget to change the path for you db in the connection string.
0
systems_axAuthor Commented:
jpaulino,
thank you so much it sure did work perfectly.
can I ask you another ? regarding the login screen or should I open a new question?
thank you
0
Jorge PaulinoIT Pro/DeveloperCommented:
>> can I ask you another ? regarding the login screen or should I open a new question?

If I know the answer theres no problem!
0
Jorge PaulinoIT Pro/DeveloperCommented:
Still about the yesterday confusion you can read a previous question that I answer (http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23140920.html). Some times it appends. :-)
0
systems_axAuthor Commented:
thank you, please let me know if I should post it under new question.

On my main screen I have a button that will open a screen for creating a login, which does work. How do I display the "login screen that you helped me with" to open only after an account has been created.  So, if the user does not click on this button to create an account, the login form will not display as there is no data stored in access table users login form.  How do I manipulate that?
thank you
0
Jorge PaulinoIT Pro/DeveloperCommented:
You want to open the login if there's that on the users table on the start up ?
0
systems_axAuthor Commented:
right after the user creates his first login I would like the software to know to open the login screen at startup when the application is ran next time.
is it possible to do, I know that I need to set something like:

under the CReate Login form:
if login is created then
open the login screen for autentication next time software is launched.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Well the procedure is almoust the same that the example above. You have to look if there's any user in the db and then prompt for the login. What you have to do is change the SQL statement to  "SELECT * FROM tblUsers"
Then when you look If reader.HasRows Then you have at least a user.

If reader.HasRows Then
  ' open login form
Else
 ' open create account form
End If

Is this that you need ?
0
systems_axAuthor Commented:
jpaulino,
but where do I put this statement as I do have a splash screen that comes up first and is it possible for the login form to open first before the splash screen comes up or will it be easier to open the splash screen and then open the login form?
0
Jorge PaulinoIT Pro/DeveloperCommented:
It's the same (open the splash screen and then open the login form) or the opposit way, but I think it's better to open the splash screen first. In the end of the actions that you have (or the timer) add that code.
0
systems_axAuthor Commented:
This is my code for ApplicationEvents.vb:
what happens is I can open the program and the splash screen deos come on first.  Then after I create a login and re-open the program the login screen and the main screen open at the same time after the splash dissappeared.  what am I missing?
thank you very much
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            Dim splash_screen As SplashScreen1
            splash_screen = DirectCast(My.Application.SplashScreen, SplashScreen1)
            'Wait one second three times.
            For progress As Integer = 0 To 100 Step 10
                Dim stop_time As Date = Now.AddSeconds(0.25)
                Do While Now < stop_time
                    Application.DoEvents()
                Loop
            Next progress
 
            Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb")
            Dim reader As OleDbDataReader
            Try
                conn.Open()
                Dim query As String = "SELECT * FROM tblUsers"
                Dim cmd As OleDbCommand = New OleDbCommand(query, conn)
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
 
                If reader.HasRows Then
                    'MessageBox.Show("Valid login")
                    Dim formLogin As New LoginUser
                    formLogin.Show()
                    Dim formMain As New Main
                    formMain.Close()
                Else
                   End If
                conn.Close()
                reader.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                If conn.State = ConnectionState.Open Then
                    conn.Close()
                End If
                reader = Nothing
                conn = Nothing
            End Try
        End Sub
    End Class

Open in new window

0
Jorge PaulinoIT Pro/DeveloperCommented:
Change this:
formLogin.Show()

To:
formLogin.ShowDialog()

In the formLogin if the login/password its not OK then you do End (check on form unload also)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
systems_axAuthor Commented:
the login form does open, but the problem now is that the main forms opens twice at the same time.  is it because the startup form is set to the main form?
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data.Common
Imports System.Windows.Forms
 
Namespace My
 
    ' The following events are availble for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication
        ' Override OnInitialize to set the splash screen's
        Protected Overrides Function OnInitialize(ByVal commandLineArgs As _
          System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean
            ' Display the splash screen for at least 6 seconds.
            Me.MinimumSplashScreenDisplayTime = 6000
            ' Continue initialization as usual.
            Return MyBase.OnInitialize(commandLineArgs)
        End Function
 
        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            Dim splash_screen As SplashScreen1
            splash_screen = DirectCast(My.Application.SplashScreen, SplashScreen1)
            'Wait one second three times.
            For progress As Integer = 0 To 100 Step 10
                ' Update the progress display.
                'splash_screen.Progress = progress
                ' Wait a little while.
                Dim stop_time As Date = Now.AddSeconds(0.25)
                Do While Now < stop_time
                    Application.DoEvents()
                Loop
                'Next progress
 
                Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=NutriDIETPro.mdb")
                Dim reader As OleDbDataReader
                Try
                    conn.Open()
                    Dim query As String = "SELECT * FROM tblUsers"
 
                    Dim cmd As OleDbCommand = New OleDbCommand(query, conn)
                    reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
 
                    If reader.HasRows Then
                        'MessageBox.Show("Valid login")
                        Dim formLogin As New LoginUser
                        'formLogin.Show()
                        formLogin.ShowDialog()
 
 
                        'Dim formMain As New Main
                        'formMain.Close()
                    Else
                        'Dim formMain As New Main
                        'formMain.Show()
                    End If
                    conn.Close()
                    reader.Close()
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                Finally
                    If conn.State = ConnectionState.Open Then
                        conn.Close()
                    End If
                    reader = Nothing
                    conn = Nothing
                End Try
            Next progress
        End Sub
    End Class
End Namespace

Open in new window

0
Jorge PaulinoIT Pro/DeveloperCommented:
The application is too big and can you upload it (www.ee-stuff.com) ?

You just showing the login form and I cannot see the main form, the load sequence, etc.
0
Jorge PaulinoIT Pro/DeveloperCommented:
It you have problems to upload you can make a copy, delete private stuff and clean the db. Don't forget to upload also the db.
0
systems_axAuthor Commented:
hi jpaulino,
here is the code for the main form, my main form only has 4 buttons that open the other 4 forms.
Option Strict Off
Public Class Main
    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         
    End Sub

    Private Sub btnClientProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm As New frmAddClient
        frm.Show()
    End Sub

    Private Sub btnMenus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm3 As New frmMenu
        frm3.Show()
    End Sub

    Private Sub btnRecipes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm1 As New frmAccess
        frm1.Show()
    End Sub
   End Class

my application properties are:
1) Startup form: Main
2) enable application framework is checked
3) authentication mode: windows
4) splash screen: splashscreen1


thank you, I am assuming that code must be placed under the main form login?

0
Jorge PaulinoIT Pro/DeveloperCommented:
>> 4) splash screen: splashscreen1
Remove this
0
systems_axAuthor Commented:
jpaulino,
I need the splash so I cannot take it off, but I tried setting the "splash screen" to "NONE" and the main form still opened twice after the login form dissappeared.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Can you upload the project and the db ?
0
systems_axAuthor Commented:
jpaulino,
i figured it out, turns out i was calling form main under the login form, but why everytime I press "exit" on the login screen, my main form opens anyhow?
I have "me.close" under the "exit" button.
0
systems_axAuthor Commented:
jpaulio,
I will post the exit button question under a different question.
thank you for your great help.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Ok, I will follow.

Thanks for the grade!
0
systems_axAuthor Commented:
hi jpaulino,
I need your help with the login screen again.
can you help?
0
Jorge PaulinoIT Pro/DeveloperCommented:
Ok, no problem!
0
systems_axAuthor Commented:
thank you, i have posted the question here, so that you receive the points.: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23195860.html

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.