How to check if subform exists in ms access

I have a main form MYMAIN with two subordinates MYSUBONE and MYSUBTWO in it.

I have "on current" events in each subform that update the textbox in the other subform.

My problem occurs when loading forms. The "on current" event is fired when the "MYSUBONE" subform is loaded (before "MYSUBTWO" is loaded) and tries to update a text field in MYSUBTWO that has not yet been loaded. Thus, the error occurs in the event procedure.

How do I check my "on current" procedure (in VBA?) For MYSUBONE to check if the MYSUBTWO subformation has not been loaded yet.

on volt-mysubone if mysubtwo doesn't load then update mysubtwo.textbox = ... end if

I tried the Loaded function in the Northwind sample database but it doesn't seem to work. how to check if a subform is still loaded?

Or can I just ignore the error and use something like "if error, exit function"?

+1


a source to share


4 answers


One possible solution to this problem would be to simply make sure you know what order the subforms are loaded in. You can accomplish this by unbinding the subform elements and then manually loading them. Here's how to do it:

  • Add your child elements to the parent form as usual.
  • Make sure you have link fields, etc. to customize them however you want.
  • On the Subform Properties tab> Data tab, delete the value in the Source Object field.


Add VBA to manually bind controls when the parent form opens:

Private Sub Form_Open(Cancel As Integer)
    Me.sfB.SourceObject = "FormB"
    Me.sfA.SourceObject = "FormA"
End Sub

      

+4


a source


Subforms actually open before the main form is opened (including all their OnCurrent events). I don't think you can guarantee which order they are loaded with.



I would include the issue and make an update from the main form. If you really need to do the update from Subform, move the update to a separate function in MySubOne

. Then, in the main OnCurrent form, call the function in MySubOne

. This ensures that both MySubOne

and are MySubTwo

already loaded.

0


a source


Not sure if it's still useful, but for what it's worth ... I am showing two grids with parent-child relationships. However, the second grid displays at once all child records associated with all parent records in the first grid. With one record selected in the first grid, I want to find the first matching child in the second grid. I ran into the same issue as the second grid (subform's) crashing when the main form initially opens as the second grid is not showing yet. My workaround was to bypass error 2455 and ignore it, so it makes it possible to continue until the second grid is displayed.

Private Sub Form_Current()
Dim rsResults As Recordset
Dim stFilter As String

On Error GoTo ErrHdlr

'Locate first results record associated to current requisition
Set rsResults = Me.Parent.sfResults.Form.Recordset
stFilter = obRecord.GetFilter(Me.Recordset, vrPKFields, vrPKTypes)
rsResults.FindFirst (stFilter)

Exit Sub

ErrHdlr:
If Err.Number = 2455 Then
   Exit Sub 'When this sub form is first displayed, Current event
               'tries to reference another subform on the same parent.
               'However, such second subform is not yet open and
               'reference fails. This is the error number raised,
               'so it is ignored on purpose. After second subform
               'is displayed, code works correctly.
               'It had to be handled this way because there no way
               'to know in advance whether subform is already open.
Else
   msgbox Err.number & " " & err.description
endif
exit sub

      

0


a source


"is there a way to check if the number of subformat records found is 0"

if forms!myMainForm!mySubForm.form.RecordsetClone.RecordCount = 0 then....

      

-1


a source







All Articles