Problem querying combobox during Form_Open

  • Thread starter Thread starter Max Yaffe
  • Start date Start date
M

Max Yaffe

Dear Group,

This was posted originally on m.p.a.modulesdaovba but no one has
replied. So, if you don't mind, I'm reposting it here.

I have an form (formPartInfo)with an empty record source. On it is a
combo box (cboPart) with a query (qryPartList) for a row source. I
initialize this form during the Form_Open event using code that looks
like:

Public Sub SetPart(myPart As String)

' Valid Part Number Test
Dim FindPart As String
FindPart = "Part = '" & myPart & "'"
Me.cboPart.Recordset.FindFirst FindPart
If (Me.cboPart.Recordset.NoMatch = True) Then
' Inform user & take corrective action
MsgBox ("Part " + myPart + " is not found)
Me.cboPart = Me.cboPart.ItemData(0)
myPart = Me.cboPart.Value
End If

'Continue on setting record source for form & subforms here
Me.RecordSource = PartQuery(myPart)
Me.sfVendor.Form.RecordSource = VendorQuery(myPart)
' and so forth.

The problem is that sometimes when I do the Valid Part Number Test,
Me.cboPart.Recordset is uninitialized and the test fails with an error
"Run-time error 91. Object or With Variable not set".
Then when I look at "Me.cboPart.Recordset" in the watch window, it is
set to "Nothing". However, if I then look at Me.cboPart in the watch
window, it is set correctly, after which, Me.cboPart.Recordset is set
correctly and the code continues to run!

I've tried this code running from Form_Open and from Form_Load with
the same results.

I guess the question is, when is MyForm.cboMyCombo.Recordset valid
after a form has started to open?

Thanks for your help.
Max
 
Answered, tentatively, in .modulesdaovba.


Dear Group,

This was posted originally on m.p.a.modulesdaovba but no one has
replied. So, if you don't mind, I'm reposting it here.

I have an form (formPartInfo)with an empty record source. On it is a
combo box (cboPart) with a query (qryPartList) for a row source. I
initialize this form during the Form_Open event using code that looks
like:

Public Sub SetPart(myPart As String)

' Valid Part Number Test
Dim FindPart As String
FindPart = "Part = '" & myPart & "'"
Me.cboPart.Recordset.FindFirst FindPart
If (Me.cboPart.Recordset.NoMatch = True) Then
' Inform user & take corrective action
MsgBox ("Part " + myPart + " is not found)
Me.cboPart = Me.cboPart.ItemData(0)
myPart = Me.cboPart.Value
End If

'Continue on setting record source for form & subforms here
Me.RecordSource = PartQuery(myPart)
Me.sfVendor.Form.RecordSource = VendorQuery(myPart)
' and so forth.

The problem is that sometimes when I do the Valid Part Number Test,
Me.cboPart.Recordset is uninitialized and the test fails with an error
"Run-time error 91. Object or With Variable not set".
Then when I look at "Me.cboPart.Recordset" in the watch window, it is
set to "Nothing". However, if I then look at Me.cboPart in the watch
window, it is set correctly, after which, Me.cboPart.Recordset is set
correctly and the code continues to run!

I've tried this code running from Form_Open and from Form_Load with
the same results.

I guess the question is, when is MyForm.cboMyCombo.Recordset valid
after a form has started to open?

Thanks for your help.
Max
 
Back
Top