Why is my code returning an error?.

O

OssieMac

Access 2002 with all updates.

I have the following code to identify the position parameters for forms so
that I can use the parameters in the Form_Open events as per the following
example:

Me.Move Left:=4050, Top:=200, Width:=11085, Height:=8715

I open the forms and position and size them and then run the code from the
VBA Editor.

The code works perfectley when called from either of the following two subs:

Sub Call_Position_Option_1()
and from:
Sub Call_Position_Option_2()


However when called from Sub Call_Position_Option_3()
I get an error at the msgbox in
Sub Position_Params(objFormId As Object)

Error is Run-time error '438'
Object doesn't support this property or method

What am I not understanding or missing here? I thought that it should work
from all three calling subs.

I realize that I have code that works but my curiosity has got the better of
me and I would like to understand what the problem is.

If I delete the position parameters and the msgbox is:
MsgBox "Form Name: " & .Name
then it returns the form name OK so it is the position parameters that cause
it to fail.

All help is greatly appreciated.

Sub Call_Position_Option_1()

Dim objForm As Object
Dim i As Integer

'Forms.Count returns only Loaded forms
For i = 0 To Forms.Count - 1
Set objForm = Forms(i)
Call Position_Params(objForm)
Next i
End Sub

Sub Call_Position_Option_2()
Dim objForm As Object

'For Each objForm In Forms returns only loaded forms
For Each objForm In Forms
Call Position_Params(objForm)
Next objForm

End Sub

Sub Call_Position_Option_3()
Dim dbs As Object
Dim objForm As Object

Set dbs = Application.CurrentProject

'For Each objForm In dbs.AllForms _
returns all forms (Loaded and Not Loaded) _
so test for IsLoaded
For Each objForm In dbs.AllForms
If objForm.IsLoaded = True Then
Call Position_Params(objForm)
End If
Next objForm

End Sub

Sub Position_Params(objFormId As Object)
'Called sub which requires parameter

'The following line of code returns the error when _
called from Sub Call_Form_Pos_Params_3

With objFormId
MsgBox "Form Name: " & .Name & Chr(13) & _
"WindowLeft: " & .WindowLeft & Chr(13) & _
"WindowTop: " & .WindowTop & Chr(13) & _
"WindowWidth: " & .WindowWidth & Chr(13) & _
"WindowHeight: " & .WindowHeight

End With
End Sub
 
S

Stefan Hoffmann

hi,
However when called from Sub Call_Position_Option_3()
I get an error at the msgbox in
Sub Position_Params(objFormId As Object)

Error is Run-time error '438'
Object doesn't support this property or method
Yup, because a the class Object doesn't have these properties. Change at
least your sub declaration:

Pubbli Sub Position_Params(objForm As Access.Form)

Maybe you should or need to redeclare your other form references also.


mfG
--> stefan <--
 
S

Stuart McCall

OssieMac said:
Access 2002 with all updates.

I have the following code to identify the position parameters for forms so
that I can use the parameters in the Form_Open events as per the following
example:

Me.Move Left:=4050, Top:=200, Width:=11085, Height:=8715

I open the forms and position and size them and then run the code from the
VBA Editor.

The code works perfectley when called from either of the following two
subs:

Sub Call_Position_Option_1()
and from:
Sub Call_Position_Option_2()


However when called from Sub Call_Position_Option_3()
I get an error at the msgbox in
Sub Position_Params(objFormId As Object)

Error is Run-time error '438'
Object doesn't support this property or method

What am I not understanding or missing here? I thought that it should work
from all three calling subs.

I realize that I have code that works but my curiosity has got the better
of
me and I would like to understand what the problem is.

If I delete the position parameters and the msgbox is:
MsgBox "Form Name: " & .Name
then it returns the form name OK so it is the position parameters that
cause
it to fail.

All help is greatly appreciated.

Sub Call_Position_Option_1()

Dim objForm As Object
Dim i As Integer

'Forms.Count returns only Loaded forms
For i = 0 To Forms.Count - 1
Set objForm = Forms(i)
Call Position_Params(objForm)
Next i
End Sub

Sub Call_Position_Option_2()
Dim objForm As Object

'For Each objForm In Forms returns only loaded forms
For Each objForm In Forms
Call Position_Params(objForm)
Next objForm

End Sub

Sub Call_Position_Option_3()
Dim dbs As Object
Dim objForm As Object

Set dbs = Application.CurrentProject

'For Each objForm In dbs.AllForms _
returns all forms (Loaded and Not Loaded) _
so test for IsLoaded
For Each objForm In dbs.AllForms
If objForm.IsLoaded = True Then
Call Position_Params(objForm)
End If
Next objForm

End Sub

Sub Position_Params(objFormId As Object)
'Called sub which requires parameter

'The following line of code returns the error when _
called from Sub Call_Form_Pos_Params_3

With objFormId
MsgBox "Form Name: " & .Name & Chr(13) & _
"WindowLeft: " & .WindowLeft & Chr(13) & _
"WindowTop: " & .WindowTop & Chr(13) & _
"WindowWidth: " & .WindowWidth & Chr(13) & _
"WindowHeight: " & .WindowHeight

End With
End Sub

The AllForms collection contains 'AccessObject' objects, not form objects.
An AccessObject is a description of a form (in the AllForms case), not a
true form object. Search help for 'AllForms collection' to get the official
take on it.

Because you only want to process loaded forms, you might just as well use
the Forms collection, since that's precisely what it is, ie a collection of
loaded forms.
 
O

OssieMac

Hi Stefan and Stuart,

Thanks for the replies but unfortunately neither suggestion works. Not to
worry, my first 2 options work and as I said, it was mainly curiosity.
 
D

Dirk Goldgar

OssieMac said:
Hi Stefan and Stuart,

Thanks for the replies but unfortunately neither suggestion works. Not to
worry, my first 2 options work and as I said, it was mainly curiosity.


I don't think you understood what Stuart was telling you. He was, in my
opinion, right on the money. The AllForms collection is not a collection of
Form objecs; it's a collection of AccessObject objects, which do not have
the properties of a Form (such as WindowLeft, WidowTop, WindowWidth, and
WindowHeight.

There's no real point to starting with the AllForms collection, since -- as
Stuart said -- the Forms collection holds all and only the open forms, but
if you really want to start with the AllForms collection, you would have to
use it *only* as a means to determine whether a particular form is open or
not, and then go to the Forms collection for a reference to the Form object
itself:

'----- start of revised code -----
Sub Call_Position_Option_3()

Dim aoForm As AccessObject

For Each aoForm In CurrentPioject.AllForms
If aoForm.IsLoaded = True Then
Call Position_Params(Forms(aoForm.Name))
End If
Next aoForm

End Sub

'----- end of revised code -----
 
S

Stuart McCall

OssieMac said:
Hi Stefan and Stuart,

Thanks for the replies but unfortunately neither suggestion works. Not to
worry, my first 2 options work and as I said, it was mainly curiosity.

I wasn't suggesting a solution (because your 1st two options are already
working), merely pointing out why your third option didn't work. That's what
you asked for.

If you really want to satisfy your curiosity, read the help topic I
mentioned.
 
O

OssieMac

Thankyou Dirk. You have explained it well. You are right; I didn't properly
understand Stuarts answer even though I did refer to Allforms collection in
help but with your explanation I can now see that what Stuart said was the
same thing.

Anyway thanks to One and All for the help and it has now satisfied my
curiosity.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top