HOW TO GET DATA FORM FORMS FIELDS

A

Antinsh

Hi!

How can i get a hold of all the fields in Access form...basicly all the
controls. I'm developing in Visaul Basic .Net framework 2.0.
So far i only know how to find the right form
(appAccess.CurrentProject.AllForms(x)) but i cant get information from its
fields.
 
P

Pieter Wijnen

In VBA syntax:

Dim C As Access.Control
Dim Frm As Access.Form
Set Frm = appAccess.CurrentProject.AllForms(x))
For Each C In Frm.Controls 'Alt: For i = 0 To Frm.Controls -1
Debug.Print C.Name, C.ControlType
Next

HtH

Pieter
 
A

Antinsh

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?
 
P

Pieter Wijnen

Sorry about that , you need to open it as well

Const acviewdesign =1
Const acForm =2
Dim C As Access.Control
Dim Frm As Access.Form


AppAccess.DoCmd.OpenForm appAccess.CurrentProject.AllForms(x).Name,
acviewdesign

Set Frm = appAccess.CurrentProject.AllForms(x))

For Each C In Frm.Controls 'Alt: For i = 0 To Frm.Controls -1

Debug.Print C.Name, C.ControlType
Next
AppAccess.DoCmd.Close acForm, Frm.Name


HtH

Pieter
 
D

Dirk Goldgar

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
 
Top