In
Antinsh said:
thanx, but there is still a problem
it was going all well but then this horrow came up:
System.InvalidCastException: Unable to cast COM object of type
'System.__ComObject' to interface type
'Microsoft.Office.Interop.Access.Form'. This operation failed because
the QueryInterface call on the COM component for the interface with
IID '{3F4A878E-C395-11D3-8D1F-0050048383FB}' failed due to the
following error: No such interface supported (Exception from HRESULT:
0x80004002 (E_NOINTERFACE)).
any suggestions?
CurrentProject.AllForms doesn't return a Form object; it returns an
AccessObject object. Its only real purpose is to allow you to list or
iterate through all the forms in the application, whether open or
closed, and see a few properties (such as the IsLoaded property).
You need to use the Application.Forms collection, which holds only
*open* forms, because the form must be open before you can access its
Controls collection. If you're interested in a form that is not open,
you must open it first -- usually in design view, to avoid actually
querying the data or running code associated with the form.
You might revise the above code like this:
Dim C As Access.Control
Dim Frm As Access.Form
Dim blnWasOpen As Boolean
blnWasOpen = appAccess.CurrentProject.AllForms(x).IsLoaded
If blnWasOpen = False Then
' Open the form hidden in design view.
appAccess.DoCmd.OpenForm x, 1, , , , 1
End If
Set Frm = appAccess.Forms(x)
For Each C In Frm.Controls
Debug.Print C.Name, C.ControlType
Next
Set Frm = Nothing
' Close the form if we opened it.
If blnWasOpen = False Then
appAccess.DoCmd.Close 2, x
End If