Codes

Haahz

Registered User.
Local time
Today, 15:42
Joined
Feb 9, 2009
Messages
11
Hi, I'm sorry about this very basic question, you will most likely think I'm stupid, but I've got a code to make a Login Screen, and I know I have to put it in Visual Basic, but I haven't a clue where it goes. So could someone tell me.

Sorry for this silly question, I have looked around a bit but cannot find anything I'm looking for. :(

I'm on Access 2007 by the way, not sure if that makes any difference.

Code:
Private Sub CommandButton1_Click()
     
     If IsValidUser() = True Then
          OpenNextForm
     Else
          MsgBox "Invalid Username/Password"
     End If

End Sub

Private Function IsValidUser() As Boolean

     Dim rs As Recordset
     Dim sSQL As String

     sSQL = "SELECT COUNT(*) FROM tblUser WHERE UserName = 'leigh" & txtUsername & "' AND Password = 'help" & txtPassword & "' "

     Set rs = db.OpenRecordset(sSQL)

     If rs(0) > 0 Then
          IsValidUser = True
     Else
          IsValidUser = False
     End If

     rs.Close
     Set rs = Nothing

End Function
 
Hi, I'm sorry about this very basic question, you will most likely think I'm stupid, but I've got a code to make a Login Screen, and I know I have to put it in Visual Basic, but I haven't a clue where it goes. So could someone tell me.

Sorry for this silly question, I have looked around a bit but cannot find anything I'm looking for. :(

I'm on Access 2007 by the way, not sure if that makes any difference.

Code:
Private Sub CommandButton1_Click()
     
     If IsValidUser() = True Then
          OpenNextForm
     Else
          MsgBox "Invalid Username/Password"
     End If

End Sub

Private Function IsValidUser() As Boolean

     Dim rs As Recordset
     Dim sSQL As String

     sSQL = "SELECT COUNT(*) FROM tblUser WHERE UserName = 'leigh" & txtUsername & "' AND Password = 'help" & txtPassword & "' "

     Set rs = db.OpenRecordset(sSQL)

     If rs(0) > 0 Then
          IsValidUser = True
     Else
          IsValidUser = False
     End If

     rs.Close
     Set rs = Nothing

End Function



The first part of the code is code that goes in the On Click event odf a command button:


Code:
Private Sub CommandButton1_Click()

  '**** validate user - start      
      If IsValidUser() = True Then
          OpenNextForm
     Else
          MsgBox "Invalid Username/Password"
     End If
  ' **** validate user - end

End Sub

This code need to be behind the form. You coudl paste in below the "End Sub" line above.

Code:
Private Function IsValidUser() As Boolean

     Dim rs As Recordset
     Dim sSQL As String

     sSQL = "SELECT COUNT(*) FROM tblUser WHERE UserName = 'leigh" & txtUsername & "' AND Password = 'help" & txtPassword & "' "

     Set rs = db.OpenRecordset(sSQL)

     If rs(0) > 0 Then
          IsValidUser = True
     Else
          IsValidUser = False
     End If

     rs.Close
     Set rs = Nothing

End Function
 

Users who are viewing this thread

Back
Top Bottom