I have a form with two text boxes(XNAC & XNAC1) and 1 comman button(Save). When the save button is clicked, i want to run a query to bring back the sum of amount where XNAC(from my table) matches either XNAC or XNAC1. the code runs but the total(sum) that it brings back is incorrect. what am i doing wrong? Please help.
Private Sub Save_Click()
Dim db As Database
Set db = CurrentDb
Dim rstemp As Recordset
Dim tblpool As Recordset
Dim daysOpen As Integer
Dim rstemp60 As Recordset
Dim rstemp6190 As Recordset
Dim rstemp120 As Recordset
Dim rstemp180 As Recordset
Dim ctl As Control
Set tblpool = db.OpenRecordset("Report_History_Table")
tblpool.AddNew
tblpool![XNAC] = XNAC.Value
tblpool![XNAC2] = XNAC2.Value
'tblpool![DateModified] = Date
If IsNull(Me.XNAC2) Then
'Get 60plus aging
Set rstemp = db.OpenRecordset("Select sum(invamt) AS [Sixtyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>1")
With rstemp
.MoveFirst
tblpool![SixtyPlus_Acceptdt] = !Sixtyplus
End With
'DoCmd.RunMacro "Update_accept_date_aging"
Set rstemp1 = db.OpenRecordset("Select sum(invamt) AS [Sixtyoneninety] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))=2")
With rstemp1
.MoveFirst
tblpool![Sixtyoneninety_Acceptdt] = !Sixtyoneninety
End With
'Get 120plus aging
Set rstemp2 = db.OpenRecordset("Select sum(invamt) AS [onetwentyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>3")
With rstemp2
.MoveFirst
tblpool![OnetwentyPlus_Acceptdt] = !onetwentyPlus
End With
'Get 180plus aging
Set rstemp3 = db.OpenRecordset("Select sum(invamt) AS [oneeightyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>5")
With rstemp3
.MoveFirst
tblpool![OneeightyPlus_Acceptdt] = !OneeightyPlus
End With
Else
'Get aging with XNAC and XNAC2
Set rstemp = db.OpenRecordset("Select sum(invamt) AS [Sixtyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' or XNAC Like '" & [Forms]![Request lookup and update]![XNAC2] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>1")
With rstemp
.MoveFirst
tblpool![SixtyPlus_Acceptdt] = !Sixtyplus
End With
'DoCmd.RunMacro "Update_accept_date_aging"
Set rstemp1 = db.OpenRecordset("Select sum(invamt) AS [Sixtyoneninety] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' or XNAC Like '" & [Forms]![Request lookup and update]![XNAC2] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))=2")
With rstemp1
.MoveFirst
tblpool![Sixtyoneninety_Acceptdt] = !Sixtyoneninety
End With
'Get 120plus aging
Set rstemp2 = db.OpenRecordset("Select sum(invamt) AS [onetwentyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' or XNAC Like '" & [Forms]![Request lookup and update]![XNAC2] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>3")
With rstemp2
.MoveFirst
tblpool![OnetwentyPlus_Acceptdt] = !onetwentyPlus
End With
'Get 180plus aging
Set rstemp3 = db.OpenRecordset("Select sum(invamt) AS [oneeightyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' or XNAC Like '" & [Forms]![Request lookup and update]![XNAC2] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>5")
With rstemp3
.MoveFirst
tblpool![OneeightyPlus_Acceptdt] = !OneeightyPlus
End With
End If
tblpool.Update
MsgBox " Saved"
Private Sub Save_Click()
Dim db As Database
Set db = CurrentDb
Dim rstemp As Recordset
Dim tblpool As Recordset
Dim daysOpen As Integer
Dim rstemp60 As Recordset
Dim rstemp6190 As Recordset
Dim rstemp120 As Recordset
Dim rstemp180 As Recordset
Dim ctl As Control
Set tblpool = db.OpenRecordset("Report_History_Table")
tblpool.AddNew
tblpool![XNAC] = XNAC.Value
tblpool![XNAC2] = XNAC2.Value
'tblpool![DateModified] = Date
If IsNull(Me.XNAC2) Then
'Get 60plus aging
Set rstemp = db.OpenRecordset("Select sum(invamt) AS [Sixtyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>1")
With rstemp
.MoveFirst
tblpool![SixtyPlus_Acceptdt] = !Sixtyplus
End With
'DoCmd.RunMacro "Update_accept_date_aging"
Set rstemp1 = db.OpenRecordset("Select sum(invamt) AS [Sixtyoneninety] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))=2")
With rstemp1
.MoveFirst
tblpool![Sixtyoneninety_Acceptdt] = !Sixtyoneninety
End With
'Get 120plus aging
Set rstemp2 = db.OpenRecordset("Select sum(invamt) AS [onetwentyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>3")
With rstemp2
.MoveFirst
tblpool![OnetwentyPlus_Acceptdt] = !onetwentyPlus
End With
'Get 180plus aging
Set rstemp3 = db.OpenRecordset("Select sum(invamt) AS [oneeightyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>5")
With rstemp3
.MoveFirst
tblpool![OneeightyPlus_Acceptdt] = !OneeightyPlus
End With
Else
'Get aging with XNAC and XNAC2
Set rstemp = db.OpenRecordset("Select sum(invamt) AS [Sixtyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' or XNAC Like '" & [Forms]![Request lookup and update]![XNAC2] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>1")
With rstemp
.MoveFirst
tblpool![SixtyPlus_Acceptdt] = !Sixtyplus
End With
'DoCmd.RunMacro "Update_accept_date_aging"
Set rstemp1 = db.OpenRecordset("Select sum(invamt) AS [Sixtyoneninety] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' or XNAC Like '" & [Forms]![Request lookup and update]![XNAC2] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))=2")
With rstemp1
.MoveFirst
tblpool![Sixtyoneninety_Acceptdt] = !Sixtyoneninety
End With
'Get 120plus aging
Set rstemp2 = db.OpenRecordset("Select sum(invamt) AS [onetwentyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' or XNAC Like '" & [Forms]![Request lookup and update]![XNAC2] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>3")
With rstemp2
.MoveFirst
tblpool![OnetwentyPlus_Acceptdt] = !onetwentyPlus
End With
'Get 180plus aging
Set rstemp3 = db.OpenRecordset("Select sum(invamt) AS [oneeightyplus] from Debits where XNAC Like '" & [Forms]![Request lookup and update]![XNAC] & "*' or XNAC Like '" & [Forms]![Request lookup and update]![XNAC2] & "*' and INVAMT > 0 and (DateDiff('M',InvDate,Now()))>5")
With rstemp3
.MoveFirst
tblpool![OneeightyPlus_Acceptdt] = !OneeightyPlus
End With
End If
tblpool.Update
MsgBox " Saved"