Question

I have a subform embedded in my main form as a datasheet. The subform's data gets populated after I select some filtering criteria and click "search" button. Everything works fine except one control I have in this subform. This is an unbound control and I will set its control source in the subform's onLoad event with values input from other controls of my subform. When I first load the main form and before select anything or click "search" button, the subform is displayed with an empty row of record. However, on the column of my unbound control, it shows as "#ERROR" while others are all blank. I know this will work fine after I do the search and my unbound control data gets populated, but is there anyway I can avoid showing the ugly "#ERROR" in my subform?

Part of my code is here.

Private Sub Form_Load()

Me.text_root_cause_field.ControlSource = "=getLatestRemark([plant], [shipment_no])"

End Sub

Public Function getLatestRemark(plant_code As String, shipment_no As String) As String

Dim db As Database
Dim srRS As Recordset

Set db = CurrentDb
Set srRS = db.OpenRecordset("SELECT TOP 1 root_cause FROM Shipment_Remark" _
                            & " WHERE plant = '" & plant_code & "'" _
                            & " AND shipment_no = '" & shipment_no & "'" _
                            & " ORDER BY update_time DESC", dbOpenDynaset)

If Not srRS Is Nothing Then
    If Not srRS.EOF Then
        getLatestRemark = srRS!root_cause
        srRS.Close
        Set srRS = Nothing
    End If
Else
    getLatestRemark = ""
End If

db.Close
Set db = Nothing

End Function

These are codes for my subform. text_root_cause_field is the unbound field I am talking about. Its control source depends on two other bound fields - [plant] and [shipment_no]. The field [plant] and [shipment_no], however, are only bound at run time after the user clicks search button in the main form and the subform's record source is set.

Thanks for the help.

Was it helpful?

Solution

Thanks @Wayne G. Dunny for his helpful discussion with me. This helps me figure out the problem of my current implementation and a possible solution to it.

Here is the reason for the #ERROR shown in my unbound text box, text_root_cause_field. This text box is bound to a function call in my subform's Load Event with two other fields from my subform's record source as parameters. This will initially give me a value in text_root_cause_field text box because the record source of my subform has been set in the form property to be one of my queries, Shipment_cycle_time.

This works fine until the program hits my main form's Open Event. In the Open Event, I set the subform's record source to be NULL because I do not want to display any data on the form's startup. As the main form's Open Event happens after the subform's Load Event, I do not have any field present in the subform any more. So the function which my text_root_cause_field is bound to is retrieving values from two non-existent fields. This gives the #ERROR.

The solution is pretty simple after the cause is figured out. I can include the SQL output of my function to the SQL of my subform's record source. So the subform's record source becomes like

SELECT s.*, (SELECT TOP 1 root_cause FROM Shipment_Remark as r 
                            WHERE r.plant = s.plant
                            AND r.shipment_no = s.shipment_no 
                            ORDER BY r.update_time DESC) as root_cause 
          FROM shipment_cycle_time AS s WHERE TRUE

In addition to all the fields in shipment_cycle_time query, the other field root_cause is selected from a different table based on the two shipment_cycle_time fields. This is exactly the same as what returns from the function. Instead of using a function, I set the controlsource of the text box text_root_cause_field to root_cause field.

Problem solved!

OTHER TIPS

Since you are setting the value in the Form_Load event, why not just check if there are zero records, and if so, then don't set the value - let it be blank.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top