Results 1 to 16 of 16

Thread: [RESOLVED] Login Control in Visual basic 6

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    33

    Resolved [RESOLVED] Login Control in Visual basic 6

    Hi,

    I am using VB 6. There are two text boxes (one for username and another for password) and two command buttons (login and cancel) on one form; i.e. something like login control in .NET environment. Is there any built in control available in VB 6 or any built in template by which users will be authenticated on the database by provided username and password. I don't want to use traditional method i.e. matching usernametxt.text = table's user_name and/or passtext.txt = table's password columns.

    User will send the login request through VB on login button's click and if username and password are correct then next steps else error will be returned by SQL Server Database 2005 or 2000.

    Please help me.

    Thank you.

    Regards
    Girish Sharma

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Login Control in Visual basic 6

    Quote Originally Posted by GirishSharma View Post
    Is there any built in control available in VB 6 or any built in template by which users will be authenticated on the database by provided username and password.
    Nope...
    Quote Originally Posted by GirishSharma View Post
    I don't want to use traditional method i.e. matching usernametxt.text = table's user_name and/or passtext.txt = table's password columns.
    Well, thats going to be a problem then 'cause thats the only way you can do it.

    Why are you using Vb6 rather than VB.NET?

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    33

    Re: Login Control in Visual basic 6

    Thanks for your reply and time given to my question.

    Actually i don't know how what code to authenticate from VB6 to SQL Server; i mean if username and password exits on the database then next i.e. form2.show else database error in msgbox.

    >Why are you using Vb6 rather than VB.NET?
    Because i am not that much familiar with VB.NET till now and much more application has almost been designed in VB6 only.

    Kindly give me code/link by which i may get the way to login from VB6 to SQL Server.

    Thanks again.

    Regards
    Girish Sharma

  4. #4
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Re: Login Control in Visual basic 6

    You need to get the correct password from the database before you try this code.

    Code:
    Private Sub cmdLogin_Click()
    Dim real_password As String
    Dim user_username As String, user_password As String
    Dim UserFound As Boolean
    
    'fill real_password with the original username and password from the database
    'for username, you will need to search (case insensitive) the username provided by the person, from the database
    
    'if the username (provided by the person) exists in database then read the real password in real_password variable and also set UserFound variable to True
    'if the username (provided by the person) is not found in the database then set UserFound as False
    
    
    If UserFound = True And txtPassword.Text = real_password Then
       Me.Visible = False
       frmMainForm.Visible = True
    ElseIf UserFound = True Then
       Msgbox "Sorry the password you provided is incorrect.", vbExclamation, "Invalid Password"
    Else
       MsgBox "The user you specified, does not exist in the databse, vbExclamation, "Invalid User"
    End If
    End Sub
    If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.

    If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.

    For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    33

    Re: Login Control in Visual basic 6

    Thanks for your reply. Probably either i am not elaborating the question properly or i am not understanding your helps. Let me explain again.

    LoginForm is the startup object of project. Two text boxes and two commands are there. What VB6 code i should write here so that if username are exists on SQL Server database exist then it will form2.show else database error in msgbox. Application users will input username as the same of database user and passwords on the form and then on click of button, code part will try to login on database, if username and password are correct then form2.show else SQL Serverdatabase error with error code should be message text of msgbox.] Something like Try Catch End Try in vb.net please.

    Regards
    Girish Sharma

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Login Control in Visual basic 6

    Just run a simple SELECT query against the database using the contents of the textboxes as criteria. If you get a record back, they are good...if you get no records back, well, then, they are not so good.

  7. #7
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Re: Login Control in Visual basic 6

    We could provide you with the raw coding too, but the main thing is whether you know how to search the database for username values.
    If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.

    If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.

    For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    33

    Re: Login Control in Visual basic 6

    Probably now it seems that i am asking spoon feeding, but no, its not like that. We have following lines in one of form :

    Here i don't want to use "sa" and pwd "girish", it should be text1.text (i.e. username and text2.text i.e. password of form1) Now if i open a recordset or use command object, if its runs then form2.show else database error should be reflected in the message box; something like i says :

    Dim cmddata As ADODB.Command
    Dim cnConnection As ADODB.Connection
    Dim rsdata As ADODB.Recordset
    Set cmddata = New ADODB.Command
    Set rsdata = New ADODB.Recordset
    Set cnConnection = New ADODB.Connection
    cnConnection.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=girish;Initial Catalog=ScheduleTask;Data Source=MASTERPLACE\MYSERVER"
    cnConnection.Open
    now

    if it opens (no errors) then
    form2.show
    else
    what is the problem at database end should be part of message box.
    End if

    I knows the database username and their passwords, but wish to login from vb.

    Regards
    Girish Sharma

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Login Control in Visual basic 6

    cnConnection.ConnectionString ="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & Text1.Text & ";pwd=" & Text2.Text & ";Initial Catalog=ScheduleTask;Data Source=MASTERPLACE\MYSERVER"

    You really should name your textboxes. Text1 should be renamed to txtUserName and Text2 should be renamed to txtPassword. That why they have names that actually mean something.

  10. #10
    Lively Member
    Join Date
    Jan 2009
    Posts
    93

    Re: Login Control in Visual basic 6

    So what you want to do is this to concatenate the ConnectionString with what the user types in the text boxes..correct or not?

    cnConnection.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & text1.text & ";pwd=" & text2.text & ";Initial Catalog=ScheduleTask;Data Source=MASTERPLACE\MYSERVER"

  11. #11
    Lively Member
    Join Date
    Jan 2009
    Posts
    93

    Re: Login Control in Visual basic 6

    Doh! Ya beat me to it Hack :-) Go Lions!!

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    33

    Re: Login Control in Visual basic 6

    Quote Originally Posted by Hack View Post
    cnConnection.ConnectionString ="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & Text1.Text & ";pwd=" & Text2.Text & ";Initial Catalog=ScheduleTask;Data Source=MASTERPLACE\MYSERVER"

    You really should name your textboxes. Text1 should be renamed to txtUserName and Text2 should be renamed to txtPassword. That why they have names that actually mean something.
    Thanks again for your continue support. It worked but suppose i says "xyz" in username textbox and "anypassword" in pass textbox; then there are no such users and/or password then it may saying :

    [DBNETLIB][ConnectionOpen (Connect)).] SQL Server Does not exist
    Or
    Login failed for user 'girish'.
    Or
    Any Database related error
    Or
    Any Network related error
    Or
    Any thing....

    So rather than error box, error should be come in message box.

    Regards
    Girish Sharma

  13. #13
    Lively Member
    Join Date
    Jan 2009
    Posts
    93

    Re: Login Control in Visual basic 6

    Wrap an error handler around it and pop up a message box if an error occurs.

  14. #14

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    33

    Re: Login Control in Visual basic 6

    Where and how should i wrap error handler :
    ..
    ..
    Set cnConnection = New ADODB.Connection

    cnConnection.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & id_txt.Text & ";pwd=" & pass_txt.Text & ";Initial Catalog=ScheduleTask;Data Source=MASTERPLACE\MYSERVER"

    On Error GoTo OpenError
    cnConnection.Open

    ProcExit:
    Exit Sub


    OpenError:
    MsgBox "The following error occured: " & vbNewLine _
    & "Error # " & Err.Number & vbNewLine _
    & Err.Description, _
    vbCritical, _
    "Open Error"
    Resume ProcExit

    cnConnection.Close
    Me.Hide
    from2.Show

    ... Next part of command button's click...
    End Sub

    Regards
    Girish Sharma

  15. #15
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Login Control in Visual basic 6

    You have the location correct...but you don't need all that stuff in the error trap. You are just going to exit out anyway.
    vb Code:
    1. ..
    2. ..
    3. Set cnConnection = New ADODB.Connection
    4.  
    5. cnConnection.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & id_txt.Text & ";pwd=" & pass_txt.Text & ";Initial Catalog=ScheduleTask;Data Source=MASTERPLACE\MYSERVER"
    6.  
    7. On Error GoTo OpenError
    8. cnConnection.Open
    9. 'I have no idea what this means, but it would go here
    10. '... Next part of command button's click...
    11. Me.Hide
    12. form2.Show
    13. Exit Sub
    14. OpenError:
    15. MsgBox "The following error occured: " & vbNewLine _
    16. & "Error # " & Err.Number & vbNewLine _
    17. & Err.Description, _
    18. vbCritical, _
    19. "Open Error"
    20.  
    21. 'cnConnection.Close 'unnecessary - if you have an error then no connection got made
    22. ' so there is nothing to close
    23. End Sub

  16. #16

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    33

    Re: Login Control in Visual basic 6

    Quote Originally Posted by Hack View Post
    You have the location correct...but you don't need all that stuff in the error trap. You are just going to exit out anyway.
    vb Code:
    1. ..
    2. ..
    3. Set cnConnection = New ADODB.Connection
    4.  
    5. cnConnection.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & id_txt.Text & ";pwd=" & pass_txt.Text & ";Initial Catalog=ScheduleTask;Data Source=MASTERPLACE\MYSERVER"
    6.  
    7. On Error GoTo OpenError
    8. cnConnection.Open
    9. 'I am just adding this line
    10. cnConnection.Close ' If it is succeed then connection should be closed (resource saving)
    11. Me.Hide
    12. form2.Show
    13. Exit Sub
    14. OpenError:
    15. MsgBox "The following error occured: " & vbNewLine _
    16. & "Error # " & Err.Number & vbNewLine _
    17. & Err.Description, _
    18. vbCritical, _
    19. "Open Error"
    20.  
    21. 'cnConnection.Close 'unnecessary - if you have an error then no connection got made
    22. ' so there is nothing to close
    23. End Sub
    Thank you Hack for your support.

    Regards
    Girish Sharma

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width