You are on page 1of 2

Dim strSQL As String

Dim LoanAmt As Double


Dim payDate As Date
' Dim empCode As String
Dim loanStartDate As Date
Dim LoanPaidOffDate As Date ' Changed the data type to Variant

' Get the selected pay date, staff name, and loan paid off date from the form
payDate = Me.PaidDate.Value
EmpCode = Me.EmpCode.Value

' Escape single quotes in the empCode value


EmpCode = Replace(EmpCode, "'", "''")

' Retrieve the loan paid off date from the tblLoan2 table
strSQL = "SELECT LoanPaidOffDate FROM tblLoan2 WHERE EmpCode = '" & EmpCode & "'"
loanStartDate = DLookup("LoanStartDate", "tblLoan2", "EmpCode = '" & EmpCode & "'")
LoanPaidOffDate = DLookup("LoanPaidOffDate", "tblLoan2", "EmpCode = '" & EmpCode & "'")

' Check if the loan start date is earlier than or equal to the pay date and the pay date is earlier
than or equal to the loan paid off date
If Not IsNull(loanStartDate) And Not IsNull(LoanPaidOffDate) And payDate >= loanStartDate And
payDate <= LoanPaidOffDate Then
' Retrieve the loan amount from the tblLoan2 table
strSQL = "SELECT LoanAmt FROM tblLoan2 WHERE EmpCode = '" & EmpCode & "'"
LoanAmt = CDbl(DLookup("LoanAmt", "tblLoan2", "EmpCode = '" & EmpCode & "'"))
Else
' Set the loan amount to zero if the loan is not within the valid period or no loan record is found
LoanAmt = 0
End If

' Set the PLoan field in the Payroll 2 form to the calculated loan amount
Forms("Form2").PLoan.Value = LoanAmt
Private Sub UpdateLoanAmount()
' Existing code for retrieving loan information

' Determine the loan description


Dim loanDescription As String
Dim currentLoan As Integer
Dim totalLoans As Integer

' Retrieve the total number of loans for the employee


strSQL = "SELECT COUNT(*) AS TotalLoans FROM tblLoan2 WHERE EmpCode = '" & empCode & "'"
totalLoans = DCount("*", "tblLoan2", "EmpCode = '" & empCode & "'")

' Determine the current loan number


strSQL = "SELECT COUNT(*) AS CurrentLoan FROM tblLoan2 WHERE EmpCode = '" & empCode & "'
AND LoanStartDate <= #" & Format(payDate, "yyyy-mm-dd") & "#"
currentLoan = DCount("*", "tblLoan2", "EmpCode = '" & empCode & "' AND LoanStartDate <= #" &
Format(payDate, "yyyy-mm-dd") & "#")

' Generate the loan description in the desired format


loanDescription = Format(currentLoan, "00") & "/" & Format(totalLoans, "00")

' Set the loan description field in the Payroll 2 form


Forms("Payroll 2").LoanDes.Value = loanDescription
End Sub

You might also like