Data Entry Form Connected With Database (Excel Sheet)




VBA Code -

Option Explicit

Dim curRecNo As Long

Private Sub btnFirst_Click()
    
    If Me.cmbEmpName.ListCount > 0 Then
    
        Me.cmbEmpName.ListIndex = 0
        
        curRecNo = Me.cmbEmpName.ListIndex + 1
        Me.lblCurrentRecNo.Caption = "Record No.: " & curRecNo
        
    End If
    
End Sub

Private Sub btnLast_Click()
    If Me.cmbEmpName.ListCount > 0 Then
    
        Me.cmbEmpName.ListIndex = Me.cmbEmpName.ListCount - 1
        
        curRecNo = Me.cmbEmpName.ListCount
        Me.lblCurrentRecNo.Caption = "Record No.: " & curRecNo
        
    End If
        
End Sub

Private Sub btnNext_Click()
    
    If Me.cmbEmpName.ListCount > 0 Then
    
        If Me.cmbEmpName.ListIndex = Me.cmbEmpName.ListCount - 1 Then
        
            Me.cmbEmpName.ListIndex = 0
            
        Else
        
            Me.cmbEmpName.ListIndex = Me.cmbEmpName.ListIndex + 1
            
        End If
        
        curRecNo = Me.cmbEmpName.ListIndex + 1
        Me.lblCurrentRecNo.Caption = "Record No.: " & curRecNo
    
    End If
    
End Sub

Private Sub btnPrevious_Click()
    
    If Me.cmbEmpName.ListCount > 0 Then
    
        If Me.cmbEmpName.ListIndex = 0 Then
        
            Me.cmbEmpName.ListIndex = Me.cmbEmpName.ListCount - 1
            
        Else
        
            Me.cmbEmpName.ListIndex = Me.cmbEmpName.ListIndex - 1
        
        End If
    
        curRecNo = Me.cmbEmpName.ListIndex + 1
        Me.lblCurrentRecNo.Caption = "Record No.: " & curRecNo
        
    End If
    
End Sub

Private Sub btnSearch_Click()
    
    Dim searchStr As String
    Dim idx As Integer
    
    searchStr = Me.tbFind.Text
    
    Me.lstFind.Clear
    
    For idx = 0 To Me.cmbEmpName.ListCount - 1
    
        If VBA.InStr(1, VBA.LCase(Me.cmbEmpName.List(idx)), VBA.LCase(searchStr)) Then
        
            Me.lstFind.AddItem (Me.cmbEmpName.List(idx))
            
        End If
    
    Next idx
    
    If Me.lstFind.ListCount > 0 Then
    
        Me.lstFind.ListIndex = 0
    
    Else
    
        MsgBox ("This record does not exist in database")
        
    End If
    
End Sub

Private Sub cmbEmpName_Click()
    
    Dim Ws As Worksheet
    Dim CRow As Integer, TRow As Integer
    
    Dim empNameID() As String
    Dim empName As String, empID As String
    
    Set Ws = Sheets("Employee Records")
    
    empNameID = VBA.Split(Me.cmbEmpName.Text, " - ")
    empName = empNameID(0)
    empID = empNameID(1)
    
    Me.tbEmpID.Text = empID
    Me.tbEmpName.Text = empName
    
    TRow = Ws.Range("A" & Rows.Count).End(xlUp).Row
    
    For CRow = 2 To TRow
    
        If Ws.Range("A" & CRow).Value = empID Then
            
            Me.tbDesignation.Text = Ws.Range("C" & CRow).Value
            Me.tbGender.Text = Ws.Range("D" & CRow).Value
            Me.tbDOJ.Text = Ws.Range("E" & CRow).Value
            Me.tbAddress.Text = Ws.Range("F" & CRow).Value
            
            '********* image ***********
            
            If VBA.Dir(ThisWorkbook.Path & "\Employee Images\" & empID & ".jpg") <> "" Then
                Me.imgEmpImage.Picture = LoadPicture(ThisWorkbook.Path & "\Employee Images\" & empID & ".jpg")
            Else
                Me.imgEmpImage.Picture = LoadPicture(ThisWorkbook.Path & "\Employee Images\" & "No Image.jpg")
            End If
            
            '***************************
            
            GoTo EX1
            
        End If
    
    Next CRow
    
EX1:
    
    curRecNo = Me.cmbEmpName.ListIndex + 1
    Me.lblCurrentRecNo.Caption = "Record No.: " & curRecNo
    
End Sub



Private Sub CommandButton1_Click()
    UF_InsertAllPictures.Show
End Sub

Private Sub lstFind_Click()
    Me.cmbEmpName.Text = Me.lstFind.Text
End Sub

Private Sub UserForm_Initialize()
        
    Dim CRow As Integer, TRow As Integer
    Dim Ws As Worksheet
    
    Set Ws = Sheets("Employee Records")
    
    TRow = Ws.Range("B" & Rows.Count).End(xlUp).Row
    
    Me.cmbEmpName.Clear
    
    For CRow = 2 To TRow
    
        Me.cmbEmpName.AddItem Ws.Range("B" & CRow).Value & " - " & Ws.Range("A" & CRow).Value
    
    Next CRow
    
    If Me.cmbEmpName.ListCount > 0 Then
        Me.cmbEmpName.ListIndex = 0
        
        curRecNo = Me.cmbEmpName.ListIndex + 1
        Me.lblCurrentRecNo.Caption = "Record No. : " & Me.cmbEmpName.ListIndex + 1
        
    End If
    
    
    
End Sub


YouTube Video Links -














Comments

Popular posts from this blog

Excel VBA To Send Data From Excel Sheet To The Google Sheet

Excel VBA To Insert Multiple Images From A Folder To Worksheet