Hi Shell,
I have tested the way you instructed.
Yes, but you did not include the results that were printed to the immediate
window for this statement:
Debug.Print "EncourageDate is a" & TypeName(varTest)
Also, this line of code should be inserted *prior to* the line of code that
is bombing out. You had it placed after the line of code that is bombing, so
you probably never saw the result printed.
A few more questions for you:
1.) Has this code ever worked, or are you trying to get it to work for the
first time
2.) What is the data type for the Month field? Please give some examples of
data values that are found in this field. Note: It must be a date/time,
numeric expression, string expression, or any combination, that can represent
a date.
3.) This prompt is expecting what type of entry from a user:
MsgBox "Please input Month, it should not be empty", vbOKOnly, "Input
Month"
A number (1 through 12), a string (January, February, etc.)?
I think you are definately getting bitten by a reserved word issue, which
Van pointed out earlier. Try renaming the Month field in your table to
anything else that is not considered a reserved word in Access or JET. Also,
rename the textbox to something like txtMonth in case it is named Month. Use
this list as a handy reference for names that you should avoid using:
Problem names and reserved words in Access
http://allenbrowne.com/AppIssueBadWord.html
Try the following modifications, and then copy the results from the
Immediate window and paste them into a reply:
Option Compare Database
Option Explicit
Private Sub Payment_Calculation_Click()
On Error GoTo Err_Payment_Calculation_Click
Dim ss As Single
Dim varTest As Variant
Dim STemp As String
Dim i As Integer
Dim Rs As ADODB.Recordset
Set Rs = New ADODB.Recordset
If IsNull(Me!EmployeeID) = True Then
MsgBox "Please input EmployeeID, it should not be empty.", _
vbCritical + vbOKOnly, "Input Employee ID."
Me!EmployeeID.SetFocus
ElseIf IsNull(Me!MonthX) = True Then
MsgBox "Please input Month, it should not be empty.", _
vbCritical + vbOKOnly, "Input Month."
Me!Month.SetFocus
Else
STemp = "Select * From [Employee Encouragement]"
Rs.Open STemp, CurrentProject.Connection, _
adOpenKeyset, adLockOptimistic
Rs.MoveFirst
Me!Bonus = 0
For i = 1 To Rs.RecordCount
varTest = Rs("EncourageDate")
Debug.Print "EncourageDate is a " & _
TypeName(varTest) & " = to: " & varTest
Debug.Print "Year = " & Year(Rs("EncourageDate"))
Debug.Print "Month = " & Month(Rs("EncourageDate"))
Debug.Print
If Rs("EmployeeID") = Me!EmployeeID And _
Rs("IntoSalary").Value = True Then
If (Year(Rs("EncourageDate")) = Year(Now())) And _
(Month(Rs("EncourageDate")) = Me![MonthX]) Then
varTest = Rs("EncourageDate")
Me!Bonus = Me!Bonus + Rs("EncouragePayment")
End If
Rs.MoveNext
Else
Rs.MoveNext
End If
Next i
Rs.Close
Stop
Notes:
I'm only showing the first half of the procedure. I added a Stop statement,
which you will need to remove once you get this much working properly. For my
testing, I renamed the Month field to MonthX and the text box to txtMonth.
Just a comment....you have a table or query named "Employee Punishment"?
Ouch! That doesn't sound like a great way of motivating employees.
Tom Wickerath
Microsoft Access MVP
http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________
Shell said:
EncourageDate is a date field. I have tested the way you instructed. The
result is the same. I attached the complete code for your reference:
Private Sub Payment_Calculation_Click()
On Error GoTo Err_Payment_Calculation_Click
Dim ss As Single
Dim varTest As Variant
Dim STemp As String
Dim i As Integer
Dim Rs As ADODB.Recordset
Set Rs = New ADODB.Recordset
If IsNull(Me!EmployeeID) = True Then
MsgBox "Please input EmployeeID,it should not be empty", vbOKOnly,
"Input EmployeeID"
Me!EmployeeID.SetFocus
ElseIf IsNull(Me!Month) = True Then
MsgBox "Please input Month,it should not be empty", vbOKOnly, "Input
Month"
Me!Month.SetFocus
Else
STemp = "Select * From [Employee Encouragement]"
Rs.Open STemp, CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
Rs.MoveFirst
Me!Bonus = 0
For i = 1 To Rs.RecordCount
If Rs("EmployeeID") = Me!EmployeeID And Rs("IntoSalary").Value =
True Then
If (Year(Rs("EncourageDate")) = Year(Now())) And
(Month(Rs("EncourageDate")) = Me![Month]) Then
varTest = Rs("EncourageDate")
Debug.Print "EncourageDate is a" & TypeName(varTest)
Me!Bonus = Me!Bonus + Rs("EncouragePayment")
End If
Rs.MoveNext
Else
Rs.MoveNext
End If
Next i
Rs.Close
STemp = "Select * From [Employee Punishment]"
Rs.Open STemp, CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
Rs.MoveFirst
For i = 1 To Rs.RecordCount
If Rs("EmployeeID") = Me!EmployeeID And Rs("IntoSalary").Value =
True Then
If Year(Rs("PunishDate")) = Year(Now()) And
Month(Rs("PunishDate")) = Me![Month] Then
Me!PunishTotal = Me!PunishTotal + Rs("PunishPayment")
End If
Rs.MoveNext
Else
Rs.MoveNext
End If
Next i
Rs.Close
Me!TotalPayment = Me!Bonus + Me!BasicSalary + Me!UnstableSalary +
Me!ContractAllowance + Me!FoodAllowance + Me!HouseAllowance +
Me!TemperaryAllowance + Me!PositionSalary + Me!ServiceSalary + Me!AuditSalary
Me!TotalDeduct = Me!PunishTotal + Me!WaterElectrialCost +
Me!LeaveDeduct + Me!DutyDeduct + Me!Compo + Me!HouseAccumulation +
Me!MedicalInsurance + Me!SocialInsurance + Me!UnemployeementInsurance +
Me!PregnantInsurance
Me!SalaryTotal = Me!TotalPayment - Me!TotalDeduct
Me!Tax = IncomeTax(CSng("& Me!SalaryTotal &"))
Me!ActualSalary = Me!SalaryTotal - Me!Tax
MsgBox "All the salary result has been calculated successfully",
vbOKOnly, "Calculate Complete"
End If
Set Rs = Nothing
Exit_Payment_Calculation_Click:
Exit Sub
Err_Payment_Calculation_Click:
MsgBox Err.Description
Resume Exit_Payment_Calculation_Click
End Sub
Would you please give me more instruction?
Thanks
Shell