Swifty509
Swifty509

Reputation: 25

Access VBA Update Form Textbox With Sub Parameter

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

Upvotes: 0

Views: 207

Answers (1)

June7
June7

Reputation: 21370

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)

Upvotes: 1

Related Questions