ChrisLeicester
Member
- Local time
- Today, 23:25
- Joined
- Feb 14, 2025
- Messages
- 38
Hi All
I am trying to use more VBA than macros, and have the following code which is coming up with a runtime error saying I must save the record before it can be requery.
The yellow line is coming up at the Docmd.save before the requery line.
The strange thing is, it was working before i added another button on the form.
The idea is that while taking payment, the user enters a value in a text box and presses one of three buttons, Cash, Card or Coupon, The code then makes a 'many' record in a payment subform which has a sum textbox at the bottom. the code should then refresh the sum and recalculate the outstanding balance. for example, a bill may be £20, the customer may pay £5 with a coupon so the balance textbox then changes to £15 etc.
Thanks
I am trying to use more VBA than macros, and have the following code which is coming up with a runtime error saying I must save the record before it can be requery.
The yellow line is coming up at the Docmd.save before the requery line.
Code:
Public Function CashPaymentMade()
'Set new sub payment record
Dim MoneyEntered As Currency
Dim Valueentered As Integer
Dim SalesValue As Currency
Dim SalesSoFar As Currency
Valueentered = Nz([Forms]![SalesPaymentMainFm]![TillKeypadFM]![NumberEntered], 0)
MoneyEntered = Val(Valueentered) / 100
SalesValue = [Forms]![SalesPaymentMainFm]![AmountToPay]
SalesSoFar = Nz([Forms]![SalesPaymentMainFm]![SalesPayMadeSubFM]![SubtotalPaid], 0)
[Forms]![SalesPaymentMainFm]![SalesPayMadeSubFM].SetFocus
DoCmd.GoToRecord , , acNewRec
'Set payment values
[Forms]![SalesPaymentMainFm]![SalesPayMadeSubFM]![WhichPaymentMain] = [Forms]![SalesPaymentMainFm]![SalesPaymentMainID]
[Forms]![SalesPaymentMainFm]![SalesPayMadeSubFM]![SubPaymentAmount] = MoneyEntered 'The value entered
[Forms]![SalesPaymentMainFm]![SalesPayMadeSubFM]![WhatPaymentType] = 1 'Sets the payment type as cash
[Forms]![SalesPaymentMainFm]![SalesPayMadeSubFM].SetFocus
DoCmd.Save 'saves the record
[Forms]![SalesPaymentMainFm]![SalesPayMadeSubFM].Requery 'Requeries the payment list
[Forms]![SalesPaymentMainFm]![TillKeypadFM]![NumberEntered] = Null 'clears the number entered field for next entry
DoCmd.Save
[Forms]![SalesPaymentMainFm]![SalesPayMadeSubFM].SetFocus
DoCmd.Save
[Forms]![SalesPaymentMainFm]![SalesPayMadeSubFM]![SubPaymentAmount].Requery
'set the Balance to pay display
[Forms]![SalesPaymentMainFm]![Balance] = [Forms]![SalesPaymentMainFm]![AmountToPay] - [Forms]![SalesPaymentMainFm]![SalesPayMadeSubFM]![SubtotalPaid
End Function
The strange thing is, it was working before i added another button on the form.
The idea is that while taking payment, the user enters a value in a text box and presses one of three buttons, Cash, Card or Coupon, The code then makes a 'many' record in a payment subform which has a sum textbox at the bottom. the code should then refresh the sum and recalculate the outstanding balance. for example, a bill may be £20, the customer may pay £5 with a coupon so the balance textbox then changes to £15 etc.
Thanks