Another Save Error

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.

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
 
I believe you are using the wrong command

Try DoCmd.RunCommand acCmdSaveRecord

If that code is in one of those forms, you can use Me and likely use the Parent keyword as well.
You only needed all that for macroes. :(
 
You might consider adding error handling in this function so that if there is an error, it returns False or returns the err.description to the user

Code:
Function FunctionName(input params) as boolean()
On Error GoTo Errhandler


functionName=True

Errhandler:
functionName=False
'functionName=err.description
End Function

In a function I think error handling is particularly important, well, it's important everywhere, but in a Function that you are "using" somewhere else, an unhandled error might cause a special type of chaos.
 

Users who are viewing this thread

Back
Top Bottom