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

ms access vba code

P: 8
Pls i use a vba code in ms access to login into my form. but i want if student login it should bring out such student record. pls what are my going to add to the below code to enable each student to login into their form. thank you.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Private Sub Command1_Click()
  3. Dim User As String
  4. Dim UserLevel As Integer
  5. Dim TempPass As String
  6. Dim ID As Integer
  7. Dim UserName As String
  8. Dim TempID As String
  9. If IsNull(Me.txtUserName) Then
  10.  MsgBox "Please enter UserName", vb
  11.  Me.txtUserName.SetFocus
  12. ElseIf IsNull(Me.txtPassword) Then
  13.  MsgBox "Please enter Password", vb
  14.  Me.txtPassword.SetFocus
  15. Else
  16.  If (IsNull(DLookup("UserLogin", "t
  17.  MsgBox "Invalid Username or Passwo
  18.  Else
  19.  TempID = Me.txtUserName.Value
  20.  UserName = DLookup("[UserName]", "
  21.  UserLevel = DLookup("[UserType]",
  22.  TempPass = DLookup("[UserPassword]
  23.  UserLogin = DLookup("[UserLogin]",
  24.  DoCmd.Close
  25.  If (TempPass = "password") Then
  26.  MsgBox "Please change Password", v
  27.  DoCmd.OpenForm "frmUserinfo", , ,
  28.  Else
  29.  'open different form according to
  30.  If UserLevel = 1 Then ' for admin
  31.  DoCmd.OpenForm "Admin Form"
  32.  Else
  33.  DoCmd.OpenForm "Navigation Form"
  34.  End If
  35.  End If
  36.  End If
  37. End If
  38. End Sub
  39. Private Sub Form_Load()
  40. Me.txtUserName.SetFocus
  41. End Sub
Aug 1 '17 #1
Share this Question
Share on Google+
6 Replies


ADezii
Expert 5K+
P: 8,624
Much of your Code is truncated, please repost the Code in its entirety.
Aug 5 '17 #2

P: 8
@ADezii
ok tnx for ur respond i will do that
Aug 11 '17 #3

P: 8
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub Command1_Click()
  4. Dim User As String
  5. Dim UserLevel As Integer
  6. Dim TempPass As String
  7. Dim ID As Integer
  8. Dim UserName As String
  9. Dim TempID As String
  10.  
  11. If IsNull(Me.txtUserName) Then
  12.  MsgBox "Please enter UserName", vbInformation, "Username required"
  13.  Me.txtUserName.SetFocus
  14. ElseIf IsNull(Me.txtPassword) Then
  15.  MsgBox "Please enter Password", vbInformation, "Password required"
  16.  Me.txtPassword.SetFocus
  17. Else
  18.  If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin = '" & Me.txtUserName.Value & "' And UserPassword = '" & Me.txtPassword.Value & "'"))) Then
  19.  MsgBox "Invalid Username or Password!"
  20.  Else
  21.  TempID = Me.txtUserName.Value
  22.  UserName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
  23.  UserLevel = DLookup("[UserType]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
  24.  TempPass = DLookup("[UserPassword]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
  25.  UserLogin = DLookup("[UserLogin]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
  26.  DoCmd.Close
  27.  If (TempPass = "password") Then
  28.  MsgBox "Please change Password", vbInformation, "New password required"
  29.  DoCmd.OpenForm "frmUserinfo", , , "[UserLogin] = " & UserLogin
  30.  Else
  31.  'open different form according to user level
  32.  If UserLevel = 1 Then ' for admin
  33.  DoCmd.OpenForm "Admin Form"
  34.  Else
  35.  DoCmd.OpenForm "Navigation Form"
  36.  End If
  37.  
  38.  End If
  39.  End If
  40. End If
  41. End Sub
  42.  
  43. Private Sub Form_Load()
  44. Me.txtUserName.SetFocus
  45. End Sub
Aug 11 '17 #4

NeoPa
Expert Mod 15k+
P: 31,419
@Aym2u2.

I've just sent you a PM. Please read it and confirm receipt by clicking on the box.

I omitted to include an injunction to express yourself in proper English where possible. Txtspk is fine for phones but not acceptable here. It's harder to understand and annoying for most who try to read it. Not everyone here is even familiar with it. Many of those that are find it irritating. Our rules prohibit its use explicitly.

PS. Just to be absolutely clear, this is not about how well you speak and understand English. We have no problem with people trying to communicate in English and just not being very good. This is about the unnecessarily abbreviated nature of writing Pls instead of Please; ur instead of you're or you are; etc.

Clearly you are a polite person. I can see that. Also you aren't aware of all our rules so I'm explaining some of them for you in order that you don't upset people and you get more people willing to offer you help.
Aug 12 '17 #5

NeoPa
Expert Mod 15k+
P: 31,419
I believe you mentioned elsewhere that you're new to Access and VBA. In that case one of the most helpful tips I can give you is to ensure all variables are properly declared (Require Variable Declaration). This will save you a great deal of unnecessary grief.
Aug 12 '17 #6

P: 8
Firstly, I Create a table tblUser with a UserName, UserLogin, UserSecurity and UserPassword fields. and tblSecurity with SecurityID and SecurityLvl.

Secondly i have a form with ID, FirstName, MiddleName, LastName, Country etc.

So Using the BELOW code only open Form with ID 1, assuming my userLogin is aym2u2, my Password is 1234 and is on ID. 5 in the tblUser what are my going to do to open Form corresponding to ID 5.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub Command1_Click()
  4. Dim User As String
  5. Dim UserLevel As Integer
  6. Dim TempPass As String
  7. Dim ID As Integer
  8. Dim UserName As String
  9. Dim TempID As String
  10.  
  11.  
  12. If IsNull(Me.txtUserName) Then
  13.  MsgBox "Please enter UserName", vbInformation, "Username required"
  14.  Me.txtUserName.SetFocus
  15. ElseIf IsNull(Me.txtPassword) Then
  16.  MsgBox "Please enter Password", vbInformation, "Password required"
  17.  Me.txtPassword.SetFocus
  18. Else
  19.  If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin = '" & Me.txtUserName.Value & "' And UserPassword = '" & Me.txtPassword.Value & "'"))) Then
  20.  MsgBox "Invalid Username or Password!"
  21.  Else
  22.  TempID = Me.txtUserName.Value
  23.  UserName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
  24.  UserLevel = DLookup("[UserType]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
  25.  TempPass = DLookup("[UserPassword]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
  26.  UserLogin = DLookup("[UserLogin]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
  27.  DoCmd.Close
  28.  If (TempPass = "password") Then
  29.  MsgBox "Please change Password", vbInformation, "New password required"
  30.  DoCmd.OpenForm "frmUserinfo", , , "[UserLogin] = " & UserLogin
  31.  Else
  32.  'open different form according to user level
  33.  If UserLevel = 1 Then ' for admin
  34.  DoCmd.OpenForm "Admin Form"
  35.  Else
  36.  DoCmd.OpenForm "Navigation Form"
  37.  End If
  38.  
  39.  End If
  40.  End If
  41. End If
  42. End Sub
  43.  
  44. Private Sub Form_Load()
  45. Me.txtUserName.SetFocus
  46. End Sub
Aug 16 '17 #7

NeoPa
Expert Mod 15k+
P: 31,419
Clearly you have no worries about saving yourself unnecessary grief as per my last post. Let me see if I can explain it another way. When experts see code posted without that important line at the top (Option Explicit), as explained in the linked article, they will do one of two things :
  1. Advise you that it's very unwise to do anything with such code.
  2. Assume you're beyond help and ignore your posts.

So. Not only is it wise to do for your own sake and sanity, it will also increase your chances of anyone bothering to help you when you post.

Ultimately though, it's entirely your call. You have all the relevant information available to you so you can take full responsibility for your choice.
Aug 16 '17 #8

Post your reply

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