[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
Re: Login Control in Visual basic 6
Quote:
Originally Posted by
GirishSharma
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
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?
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
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
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
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.
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.
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
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.
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"
Re: Login Control in Visual basic 6
Doh! Ya beat me to it Hack :-) Go Lions!!
Re: Login Control in Visual basic 6
Quote:
Originally Posted by
Hack
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
Re: Login Control in Visual basic 6
Wrap an error handler around it and pop up a message box if an error occurs.
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
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:
..
..
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
'I have no idea what this means, but it would go here
'... Next part of command button's click...
Me.Hide
form2.Show
Exit Sub
OpenError:
MsgBox "The following error occured: " & vbNewLine _
& "Error # " & Err.Number & vbNewLine _
& Err.Description, _
vbCritical, _
"Open Error"
'cnConnection.Close 'unnecessary - if you have an error then no connection got made
' so there is nothing to close
End Sub
Re: Login Control in Visual basic 6
Quote:
Originally Posted by
Hack
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:
..
..
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
'I am just adding this line
cnConnection.Close ' If it is succeed then connection should be closed (resource saving)
Me.Hide
form2.Show
Exit Sub
OpenError:
MsgBox "The following error occured: " & vbNewLine _
& "Error # " & Err.Number & vbNewLine _
& Err.Description, _
vbCritical, _
"Open Error"
'cnConnection.Close 'unnecessary - if you have an error then no connection got made
' so there is nothing to close
End Sub
Thank you Hack for your support.
Regards
Girish Sharma :wave: