简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM