ShabanaHafiz
Programmer
I have a userform in Excel Workbook in which user can add new employee records and browse existing records. Data is stored in EmployeesData worksheet in the same workbook. This sheet is hidden and the workbook is protected.
To browse existing records, user can click cmdFirst, cmdPrevious, cmdNext and cmdLast. There is a textbox, txtRowNumber between First,Previous and Next,Last buttons. Number in text box shows the row number of displayed record in EmployeesData worksheet.
The GetData routine is located in the module associated with the user form. GetData copies the data from the EmployeesData worksheet to the user form. The RowNumber text box contains the number of the row that should be displayed on the form, so the real trick is to convert the value in the RowNumber text box into a value that can be used to extract the data from the worksheet using the Cells method. Program listing of GetData is as follows:
Code for cmdAdd_Click is as follows:
The FindLastRow function scans through the worksheet to find the first cell that doesn't have a value.
What I need to accomplish is whenever there is a change in txtEffdate, I need to search EmployeesData worksheet. If data exists for the composite primary key employeenumber and effective date, then the row number of that record to be assigned to txtRowNumber and then call GetData routine to populate controls on the userform.
First two columns in EmployeesData worksheet are EmployeeNumber and Effective Date and rows are sorted by these two columns.
To browse existing records, user can click cmdFirst, cmdPrevious, cmdNext and cmdLast. There is a textbox, txtRowNumber between First,Previous and Next,Last buttons. Number in text box shows the row number of displayed record in EmployeesData worksheet.
The GetData routine is located in the module associated with the user form. GetData copies the data from the EmployeesData worksheet to the user form. The RowNumber text box contains the number of the row that should be displayed on the form, so the real trick is to convert the value in the RowNumber text box into a value that can be used to extract the data from the worksheet using the Cells method. Program listing of GetData is as follows:
Code:
Private Sub GetData()
Dim r As Long
If IsNumeric(txtRowNumber.Text) Then
r = CLng(txtRowNumber.Text)
Else
ClearData
MsgBox "Illegal row number"
Exit Sub
End If
If r > 1 And r <= LastRow Then
txtEmployeeNumber.Text = Worksheets("EmployeesData").Cells(r, 1)
txtEffDate.Text = FormatDateTime(Worksheets("EmployeesData").Cells(r, 2), vbShortDate)
txtName.Text = Worksheets("EmployeesData").Cells(r, 3)
cboJobTitle.Text = Worksheets("EmployeesData").Cells(r, 4)
cboWorkLocation.Text = Worksheets("EmployeesData").Cells(r, 5)
txtLeaveEncash.Text = Format(Worksheets("EmployeesData").Cells(r, 15), "##,##")
txtOvertime.Text = Format(Worksheets("EmployeesData").Cells(r, 16), "##,##")
txtLoan.Text = Format(Worksheets("EmployeesData").Cells(r, 17), "##,##")
txtMonthlyTax.Text = Format(Worksheets("EmployeesData").Cells(r, 18), "##,##")
txtBasicSalary.Text = Format(Worksheets("EmployeesData").Cells(r, 6), "##,##")
txtHouseRent.Text = Format(Worksheets("EmployeesData").Cells(r, 7), "##,##")
txtConveyance.Text = Format(Worksheets("EmployeesData").Cells(r, 8), "##,##")
txtUtility.Text = Format(Worksheets("EmployeesData").Cells(r, 9), "##,##")
txtGrossSalary.Text = Format(Worksheets("EmployeesData").Cells(r, 14), "##,##")
DisableSave
cmdAdd.Enabled = True
ElseIf r = 1 Then
ClearData
Else
ClearData
MsgBox "Invalid row number"
End If
End Sub
Code for cmdAdd_Click is as follows:
Code:
Private Sub cmdAdd_Click()
EnableSave
LastRow = FindLastRow
txtRowNumber.Text = FormatNumber(LastRow, 0)
cmdAdd.Enabled = False
ClearData
txtEmployeeNumber.SetFocus
End Sub
The FindLastRow function scans through the worksheet to find the first cell that doesn't have a value.
What I need to accomplish is whenever there is a change in txtEffdate, I need to search EmployeesData worksheet. If data exists for the composite primary key employeenumber and effective date, then the row number of that record to be assigned to txtRowNumber and then call GetData routine to populate controls on the userform.
First two columns in EmployeesData worksheet are EmployeeNumber and Effective Date and rows are sorted by these two columns.