0

I'm having an issue with figuring out how to update my forms textbox with the value I ended with in the procedure. If a messagebox is used the output is fine. However, I want the user to be able to click the button and the textbox on that form be updated with the answer.

Private Sub btnTotalGuests_Click()

    Call CalculateTotalGuestsForEachService

End Sub

Here is the procedure

Public Sub CalculateTotalGuestsForEachService()

    'Declare variables
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim intTotalParty As Integer
    Dim intID As Integer
    
    'Set the current database
    Set db = CurrentDb
    
    'Set the recordset
    intID = InputBox("Enter the Service ID (1-6)", "Service ID")
    Set rst = db.OpenRecordset("Select Orders.* From Orders Where ServiceID =" & intID)
    
    'Cycle through the records
    Do While Not rst.EOF
        If Not IsNull(rst!NoInParty) Then
            intTotalParty = intTotalParty + rst!NoInParty
        End If
    rst.MoveNext
    Loop
    
    'Display total amount
    txtTotalGuests.Text = intTotalParty 'Wondering how to display this in the textbox on the form.

    
    'Close the recordset
    rst.Close
    Set rst = Nothing
    Set db = Nothing
    
End Sub
2
  • The form for the program is a single button to launch the procedure and a textbox to get the answer back. Commented Nov 18, 2020 at 5:47
  • Saving calculated data is usually not necessary and often a bad idea, especially aggregate data. If it can be calculated for saving, it can be calculated when needed. Commented Dec 2, 2020 at 6:18

1 Answer 1

1

Don't use Text property. You want Value property and since Value is default, don't even need to explicitly reference. Presume CalculateTotalGuestsForEachService is in the form module so can use Me.txtTotalGuests.

Consider domain aggregate function instead of looping recordset.

Dim intID As Integer
intID = InputBox("Enter the Service ID (1-6)", "Service ID")
Me.txtTotalGuests = DSum("NoInParty", "Order", "ServiceID =" & intID)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.