By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,315 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

User name and password validation

P: 7
Please could some one provide me the code to validate the user name and password fields using Vb for MS access.

Fields ::
User name :: text box
Password :: text box
Login :: button

The form should accept the user name and password from the text box , validate the same against a table on MS access if successful should allow the user to proceed to next form else display a alert/error message.


Thanks a lot,
Mithun.
Sep 20 '07 #1
Share this Question
Share on Google+
14 Replies


debasisdas
Expert 5K+
P: 8,127
Kindly post the code that you have tried to solve this problem, to get help from our experts
Sep 20 '07 #2

P: 7
Code containing errors
Expand|Select|Wrap|Line Numbers
  1. Private Sub Login1_Click()
  2. On Error GoTo Err_Login1_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stLinkCriteria As String
  6.     text3.SetFocus
  7.  
  8.     If text3.Text = user_validate.user_name And text5.Text = user_validate.Password Then
  9.  
  10.     stDocName = "Applications"
  11.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  12.     Else
  13.     MsgBox "Please enter correct user name and password", vbOKOnly
  14.  
  15.  
  16. Exit_Login1_Click:
  17.     Exit Sub
  18.  
  19. Err_Login1_Click:
  20.     MsgBox Err.Description
  21.     Resume Exit_Login1_Click
  22.     End If
  23. End Sub
Sep 20 '07 #3

QVeen72
Expert 100+
P: 1,445
Hi,

Remove the error handling and keep a break point and tell us which line u r getting the error...?

What is this :
user_validate.user_name
(TableName.FieldName) or (FormName.ControlName)... ?


REgards
Veena
Sep 20 '07 #4

P: 15
how secure are you wanting this to be you may want to consider some encrytion as the password and username can easly be retrived by a hex editor this way
Sep 20 '07 #5

P: 7
Hi,

Remove the error handling and keep a break point and tell us which line u r getting the error...?

What is this :
user_validate.user_name
(TableName.FieldName) or (FormName.ControlName)... ?


REgards
Veena

user_validate.user_name
(TableName.FieldName)
Sep 20 '07 #6

QVeen72
Expert 100+
P: 1,445
Hi,

Then, u simply cant give, TableName.FieldName,
U have to declare a Record set Open Recordset with the "Where Crieteria", if u find any Records, then allow to proceed or else, Prompt again, giving appropriate message . SQL Statement shud be something like this :

Expand|Select|Wrap|Line Numbers
  1. sSQL = "Select * From UserTable Where UserName = '" & TxtUserName.Text & "' And Password = '" & txtpwd.text & "'"
  2.  
Regards
Veena
Sep 20 '07 #7

P: 7
Trying with the following code....
getting an error as "Object required"

* * * code * * *

Expand|Select|Wrap|Line Numbers
  1. Private Sub Login_Click()
  2. On Error GoTo Err_Login_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stLinkCriteria As String
  6.     Dim user_name As String
  7.     Dim passwor As String
  8.  
  9.  
  10. If user_validate.user_name = Text3.Text And user_validate.Password = Text5.Text Then
  11. sSQL = "Select * From User_validate Where User_Name = '" & Text3.Text & "' And Password = '" & Text5.Text & "'"
  12.  
  13.     stDocName = "Applications"
  14.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  15.  
  16.     Else
  17.     MsgBox "Invalid user id or password, please try again", vbOKOnly
  18.  
  19.  
  20.  
  21. Exit_Login_Click:
  22.     Exit Sub
  23.  
  24. Err_Login_Click:
  25.     MsgBox Err.Description
  26.     Resume Exit_Login_Click
  27.     End If
  28.  
  29. End Sub
Sep 20 '07 #8

QVeen72
Expert 100+
P: 1,445
Hi Mithun,

Open a Recordset with the given SQL Statement, in ur code, open recordset line is missing..

REgards
Veena
Sep 20 '07 #9

P: 7
Hi Mithun,

Open a Recordset with the given SQL Statement, in ur code, open recordset line is missing..

REgards
Veena

Hi Veena,
I'm a begineer with VB. Please could you let me know how i can Open a Recordset with the given SQL Statement.
Thanks a lot for the guidance and support.
regards,
Mithun
Sep 20 '07 #10

QVeen72
Expert 100+
P: 1,445
Hi,

I have not used mucg of VBA, I always work on VB6,
Anyway, Check This Basic Code:
I assume u r using ADO

Expand|Select|Wrap|Line Numbers
  1. Dim rs As ADODB.Recordset
  2. Dim sSQL As String
  3. Set rs = New ADODB.Recordset
  4. sSQL ="Select * From MyUserTable Where UserName = '" _
  5.     & Trim(txtUserName.Text) & "' And Pwd ='" & Trim(txtPwd.Text) & "'"
  6. rs.Open sSQL, CurrentProject.Connection
  7. If RS.EOF Then
  8.    Msgbox "User Not Found"
  9.    'Prompt again for user name /pwd
  10. Else
  11.    'User Found
  12.    'Write Code to Open ur Application
  13. End If
  14. rs.Close
  15. Set rs = Nothing
  16.  
  17.  
U can refine the Code..

REgards
Veena
Sep 21 '07 #11

P: 7
Hi Veena,
Thanks a lot for the help and guidance.....
I have a question with regards to setting the focus on an object "
Please could you let me know whether I can explicitly set focus on more than one object at a time. i.e., user name and password fields"

Thanks and regards,
Mithun

The following code is working fine... except for
.... it is requesting me to set the focus on the text box (Password) field too....
Please could you let me know whether I can explicitly set focus on more than one object at a time.if so how?


Private Sub Login_Click()
On Error GoTo Err_Login_Click

Dim stDocName As String
Dim stLinkCriteria As String
Dim user_name As String
Dim password As String
Dim temp As String

Dim rs As ADODB.Recordset
Dim sSQL As String
Combo2.SetFocus
Set rs = New ADODB.Recordset
sSQL = "Select * from user_validate Where user_Name = '" & Combo2.SelText & "'"
rs.Open sSQL, CurrentProject.Connection
' rs.MoveFirst


If rs.EOF Then
MsgBox "User Not Found"
'Prompt again for user name /pwd
Else
'MsgBox "User Found"
'Write Code to Open ur Application
End If
rs.Close
Set rs = Nothing

' commented **** Mithun *****
' Combo2.SetFocus

'sSQL = "SELECT user_validate.user_name FROM user_validate"
'temp = Combo2.SelText
'temp = user_validate.user_name

'If user_validate.user_name = Combo2.SelText And user_validate.password = Text4.Text Then
'sSQL = "Select * From User_validate Where User_Name = '" & Combo2.SelText & "' And Password = '" & Text4.Text & "'"

' stDocName = "dhome"
' DoCmd.OpenForm stDocName, , , stLinkCriteria

' Else
'MsgBox "Invalid user id or password, please try again", vbOKOnly

'End of commented section **** Mithun *****

Exit_Login_Click:
Exit Sub

Err_Login_Click:
MsgBox Err.Description
Resume Exit_Login_Click
' End If

End Sub
Sep 24 '07 #12

hariharanmca
100+
P: 1,977
Hi Veena,
Thanks a lot for the help and guidance.....
I have a question with regards to setting the focus on an object "
Please could you let me know whether I can explicitly set focus on more than one object at a time. i.e., user name and password fields"
To make set focus

Expand|Select|Wrap|Line Numbers
  1. txtCtrlName.SetFocus
You cannot make set focus for two object at a time
Sep 24 '07 #13

P: 2
Please could some one provide me the code to validate the user name and password fields using Vb for MS access.

Fields ::
User name :: text box
Password :: text box
Login :: button

The form should accept the user name and password from the text box , validate the same against a table on MS access if successful should allow the user to proceed to next form else display a alert/error message.


Thanks a lot,
Mithun.

Reply from Mr.Jack ( Diploma engineer ) ongoing with AMIE

after dragging controls on active control form


on login button write this following code

a=trim(val(username.text)
b=trim(val(password.text)
while adodc1.recordset.eof<>true
if adodc1.recordset.fields("username")=a then
if adodc1.recordset.fields("password")=b then
login.setfocus
exit sub
else
adodc1.recordset.movenext
end if
else
adodc1.recordset.movenext
end if
wend
msgbox " invalid login",vbinformation
username.text=""
password.text=""
username.setfocus
Sep 24 '07 #14

P: 7
HI,
Thank you every one for the help and guidance provided. in getting this task completed.

Mentioned below is the final version of the code.

regards,
Mithun

Private Sub Login_Click()
On Error GoTo Err_Login_Click

Dim stDocName As String
Dim stLinkCriteria As String
Dim user_name As String
Dim Password As String
Dim temp As String

Dim rs As ADODB.Recordset
Dim rss As ADODB.Recordset

Dim sSQL As String
Dim tSQL As String
Dim x As String

Combo2.SetFocus
Set rs = New ADODB.Recordset
sSQL = "Select password from user_validate Where user_Name = '" & Combo2.SelText & "'"
rs.Open sSQL, CurrentProject.Connection
If rs.EOF Then
MsgBox "Either the user name or password entered are invalid. Please try again. ", vbOKOnly
Else
rs.MoveFirst

While rs.EOF = False
x = rs(0).Value
rs.MoveNext
Wend




Text4.SetFocus

Set rss = New ADODB.Recordset
tSQL = "select * from user_validate where password = ' " & Text4.Text & "'"

rss.Open sSQL, CurrentProject.Connection

If x <> Text4.Text Then
MsgBox "User Not Found"

'Prompt again for user name /pwd
Else
Combo2.SetFocus

MsgBox " " & Combo2.SelText & " Welcome to DESTINATION ONE ", vbOKOnly
DoCmd.Close
stDocName = "dhome"
DoCmd.OpenForm stDocName, , , stLinkCriteria
'Write Code to Open ur Application
End If
End If


rs.Close
Set rs = Nothing


Exit_Login_Click:
Exit Sub

Err_Login_Click:
MsgBox Err.Description
Resume Exit_Login_Click
' End If


End Sub
Sep 26 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.