Setting the Product ID


When the user selects a product, we need to have a variable into which to place the selected product ID, so we'll declare a variable in the form declarations using the code in Listing 28.3.

Listing 28.3 - RECVNG.FRM - The declarations section for our form, declaring a variable which we can use for holding the selected product ID.

Option Explicit
'We always return the same error number
Private Const ERROR_NUMBER = vbObjectError + 0
'The currently selected product ID
Dim iiProductID As Long

We'll set the product ID into this variable whenever the user selects a product from the list by navigating in the Remote Data Control to the currently selected row, and then getting the Prd_ID column from that row, as in Listing 28.4.

Listing 28.4 - RECVNG.FRM - When the user selects a product, we need to grab the product ID from the Remote Data Control.

Private Sub dbcProduct_Click(Area As Integer)
    Dim varCurRecord As Variant
    'What is the currently selected item?
    varCurRecord = dbcProduct.SelectedItem
    'Move to the selected record in the result set
    MSRDC1.Resultset.Move 0, varCurRecord
    'Grab the product ID
    iiProductID = MSRDC1.Resultset!Prd_ID.Value
End Sub

The code that we are using to grab the selected product ID is dependent on the Remote Data Control being used for the ListField, DataField, and the BoundColumn. If we use the Remote Data Control for just the ListField, the dbcProduct_Click method will error out with an invalid bookmark error when you first click it to select a product. This is due to the fact that the control will start out with the selected item of 0, which the Remote Data Control will interpret as an invalid row. It will be after you have selected a row that the control will have a valid row number, but you will not be able to get to that point.

We could easily have waited until the user has clicked the Received button when calling the Transaction Server object to determine the selected item, but that would entail additional code in that specific method which we've been able to isolate by using the method that we have. This is not to say that this approach is better, because it's not, but it does isolate the functionality, allowing us to focus on the core functionality that we are implementing in each method.