• September 22, 2021

PHSARIT

Bring Technology Ideas with you

 Loading image using Dlookup from Table in Login Form in MS Access

Usually people want to show user in the login form whether, it does have account or not as the image below:

    Create a login form as below:

      There are few tools need to be used: Two Textbox controls, Two Command Buttons, and Picture box

      Rename the control as you wishes then in Login Event is:

      Private Sub cmdLogin_Click()
      Call login
      End Sub

      Then Create a sub login as below:

      Sub login()
      If Me.txtusername.Value = DLookup("F_Name", "Tbl_Student", "F_Name='" & Me.txtusername.Value & "'") Then
         Me.imageuser.Picture = DLookup("User_Image", "Tbl_Student", "F_Name='" & Me.txtusername.Value & "'")
         Else
         MsgBox ("Not found!"), vbCritical
         Exit Sub
         End If
      Set rs = db.OpenRecordset("select * from Tbl_Student where F_Name='" & Me.txtusername.Value & "'")
      If rs.RecordCount <= 0 Then
          MsgBox "Username is incorrect! Contact your adminstrator!", vbCritical
          Me.txtusername.SetFocus
      Else
          If rs("Password") <> Me.txtpassword.Value Then
          MsgBox "Password is incorrect", vbCritical
          Me.txtpassword.SetFocus
          Else
          MsgBox ("Login Successfully"), vbInformation
          DoCmd.Close acForm, "Login", acSaveYes
          DoCmd.OpenForm "Start-Up Form", acNormal
          End If
      End If

      In Form Load event and Option Compare Database, please declare

      Option Compare Database
      Dim db As DAO.Database
      Dim rs As DAO.Recordset
      Private Sub Form_Load()
      Set db = CurrentDb
      End Sub

      The full source code:

      Option Compare Database
      Dim db As DAO.Database
      Dim rs As DAO.Recordset
      
      Private Sub Form_Load()
      Set db = CurrentDb
      End Sub
      Sub login()
      If Me.txtusername.Value = DLookup("F_Name", "Tbl_Student", "F_Name='" & Me.txtusername.Value & "'") Then
         Me.imageuser.Picture = DLookup("User_Image", "Tbl_Student", "F_Name='" & Me.txtusername.Value & "'")
         Else
         MsgBox ("Not found!"), vbCritical
         Exit Sub
         End If
      Set rs = db.OpenRecordset("select * from Tbl_Student where F_Name='" & Me.txtusername.Value & "'")
      If rs.RecordCount <= 0 Then
          MsgBox "Username is incorrect! Contact your adminstrator!", vbCritical
          Me.txtusername.SetFocus
      Else
          If rs("Password") <> Me.txtpassword.Value Then
          MsgBox "Password is incorrect", vbCritical
          Me.txtpassword.SetFocus
          Else
          MsgBox ("Login Successfully"), vbInformation
          DoCmd.Close acForm, "Login", acSaveYes
          DoCmd.OpenForm "Start-Up Form", acNormal
          End If
      End If
      
      End Sub
      Private Sub cmdLogin_Click()
      Call login
      End Sub

      Related post