Reference Form Properties

A

alex

Reference Form Properties

Hello,
Using Access ’03…

I have some code in a global module that imports a form into the mdb.

Once the form is imported, I’d like to access its caption property. I
cannot, however, seem to figure out how to reference the form.

From the global module, I’ve tried:

Public Function myFunc()
Msgbox Forms!myForm.caption
End Function

I get an error, however, saying that Access cannot find the form. I
know that I can access the properties in the class module, but I need
the code to recognize the form from the global module.

Any help would be appreciated.

Thanks,
alex
 
J

Jeff Boyce

Alex

Would you provide the code that "imports a form"? It may be that the name
that code uses doesn't match the name you're giving it in the function.

(in fact, if you are literally using "Forms!myForm.Caption", your import
function had better be naming it "myForm"!)

Good luck

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

Reference Form Properties

Hello,
Using Access ’03…

I have some code in a global module that imports a form into the mdb.

Once the form is imported, I’d like to access its caption property. I
cannot, however, seem to figure out how to reference the form.

From the global module, I’ve tried:

Public Function myFunc()
Msgbox Forms!myForm.caption
End Function

I get an error, however, saying that Access cannot find the form. I
know that I can access the properties in the class module, but I need
the code to recognize the form from the global module.

Any help would be appreciated.

Thanks,
alex
 
A

alex

Alex

Would you provide the code that "imports a form"?  It may be that the name
that code uses doesn't match the name you're giving it in the function.

(in fact, if you are literally using "Forms!myForm.Caption", your import
function had better be naming it "myForm"!)

Good luck

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.


Reference Form Properties

Hello,
Using Access 03

I have some code in a global module that imports a form into the mdb.

Once the form is imported, I d like to access its caption property.  I
cannot, however, seem to figure out how to reference the form.

From the global module, I ve tried:

Public Function myFunc()
Msgbox Forms!myForm.caption
End Function

I get an error, however, saying that Access cannot find the form.  I
know that I can access the properties in the class module, but I need
the code to recognize the form from the global module.

Any help would be appreciated.

Thanks,
alex

Thanks Jeff for responding...
I know the names are the same; I get the same error (form not found)
even after I import the form (which works fine) and then run the
function that attempts to pull the caption in the immediate window.

Code:
''''''''''''''''''''''''
Dim strDatabaseType As String
strDatabaseType = "Microsoft Access"
Dim strFormName As String
strFormName = "myForm"
Dim strDatabaseName As String
strDatabaseName = "T:\MY.mdb"

DoCmd.TransferDatabase acImport, strDatabaseType, _
strDatabaseName, acForm, strFormName, strFormName
'''''''''''''''''''''''''''

I can see the form in the database window (it's there)...but this will
not work for some reason:
Public Function myFunc()
Msgbox Forms!myForm.caption
End Function
 
D

Douglas J. Steele

Is the form open when myFunc runs?

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)

Alex

Would you provide the code that "imports a form"? It may be that the name
that code uses doesn't match the name you're giving it in the function.

(in fact, if you are literally using "Forms!myForm.Caption", your import
function had better be naming it "myForm"!)

Good luck

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.


Reference Form Properties

Hello,
Using Access 03

I have some code in a global module that imports a form into the mdb.

Once the form is imported, I d like to access its caption property. I
cannot, however, seem to figure out how to reference the form.

From the global module, I ve tried:

Public Function myFunc()
Msgbox Forms!myForm.caption
End Function

I get an error, however, saying that Access cannot find the form. I
know that I can access the properties in the class module, but I need
the code to recognize the form from the global module.

Any help would be appreciated.

Thanks,
alex

Thanks Jeff for responding...
I know the names are the same; I get the same error (form not found)
even after I import the form (which works fine) and then run the
function that attempts to pull the caption in the immediate window.

Code:
''''''''''''''''''''''''
Dim strDatabaseType As String
strDatabaseType = "Microsoft Access"
Dim strFormName As String
strFormName = "myForm"
Dim strDatabaseName As String
strDatabaseName = "T:\MY.mdb"

DoCmd.TransferDatabase acImport, strDatabaseType, _
strDatabaseName, acForm, strFormName, strFormName
'''''''''''''''''''''''''''

I can see the form in the database window (it's there)...but this will
not work for some reason:
Public Function myFunc()
Msgbox Forms!myForm.caption
End Function
 
A

alex

Is the form open when myFunc runs?

--
Doug Steele, Microsoft Access MVPhttp://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)



















Thanks Jeff for responding...
I know the names are the same; I get the same error (form not found)
even after I import the form (which works fine) and then run the
function that attempts to pull the caption in the immediate window.

Code:
''''''''''''''''''''''''
Dim strDatabaseType As String
    strDatabaseType = "Microsoft Access"
Dim strFormName As String
    strFormName = "myForm"
Dim strDatabaseName As String
    strDatabaseName = "T:\MY.mdb"

DoCmd.TransferDatabase acImport, strDatabaseType, _
strDatabaseName, acForm, strFormName, strFormName
'''''''''''''''''''''''''''

I can see the form in the database window (it's there)...but this will
not work for some reason:
Public Function myFunc()
        Msgbox Forms!myForm.caption
End Function- Hide quoted text -

- Show quoted text -

No Doug; it is not...

Taking your comment, I tested the code with the form open and it
worked (I should have thought of that)!

Is there any way for the code to work with the form closed?
 
D

Douglas J. Steele

You cannot refer to properties of forms unless they're open.

You could check whether or not it's already open, and open it hidden (then
reclose it) if not.

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)

Is the form open when myFunc runs?

--
Doug Steele, Microsoft Access MVPhttp://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)




















Thanks Jeff for responding...
I know the names are the same; I get the same error (form not found)
even after I import the form (which works fine) and then run the
function that attempts to pull the caption in the immediate window.

Code:
''''''''''''''''''''''''
Dim strDatabaseType As String
strDatabaseType = "Microsoft Access"
Dim strFormName As String
strFormName = "myForm"
Dim strDatabaseName As String
strDatabaseName = "T:\MY.mdb"

DoCmd.TransferDatabase acImport, strDatabaseType, _
strDatabaseName, acForm, strFormName, strFormName
'''''''''''''''''''''''''''

I can see the form in the database window (it's there)...but this will
not work for some reason:
Public Function myFunc()
Msgbox Forms!myForm.caption
End Function- Hide quoted text -

- Show quoted text -

No Doug; it is not...

Taking your comment, I tested the code with the form open and it
worked (I should have thought of that)!

Is there any way for the code to work with the form closed?
 
A

alex

You cannot refer to properties of forms unless they're open.

You could check whether or not it's already open, and open it hidden (then
reclose it) if not.

--
Doug Steele, Microsoft Access MVPhttp://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)










No Doug; it is not...

Taking your comment, I tested the code with the form open and it
worked (I should have thought of that)!

Is there any way for the code to work with the form closed?- Hide quoted text -

- Show quoted text -

Ok, thanks for the help. I appreciate it!
alex
 
A

alex

An alternative might be, if you have control of the exporting database, to
create a custom property for myForm which you could read in the importing
database even with the form closed.  Try something like the following
(untested):

'Routine to create the property in the exporting database:

Dim db As DAO.Database
Dim frm As DAO.Document
Dim prp As DAO.Property

Set db = CurrentDb
Set frm = db.Containers("Forms").Documents("myForm")
Set prp = frm.CreateProperty("CustomCaption", dbText, "The Caption")
frm.Properties.Append prp
frm.Properties.Refresh

Set prp = Nothing
Set frm = Nothing
Set db = Nothing

'Routine to retreive in the importing database:

Dim db As DAO.Database
Set db = CurrentDb
MsgBox
db.Containers("Forms").Documents("myForm").Properties("CustomCaption").Valu­e
Set db = Nothing

May be easier to adopt Douglas's suggestion though!

Jon










No Doug; it is not...

Taking your comment, I tested the code with the form open and it
worked (I should have thought of that)!

Is there any way for the code to work with the form closed?- Hide quoted text -

- Show quoted text -

Thanks Jon; I'll take a look at it.
alex
 
D

David W. Fenton

An alternative might be, if you have control of the exporting
database, to create a custom property for myForm which you could
read in the importing database even with the form closed. Try
something like the following (untested):

You use the documents collection, bit I think the same information
is accessible much more easily via CurrentProject.AllForms. I do
know there's a Properties collection of the form objects returned by
that, and I'd assume custom properties would show up there, but
maybe not.
 
D

David W. Fenton

I had a look at this and yes the
Application.CurrentProject.AllForms collection does include
un-opened Forms. However, an
Application.CurrentProject.AllForms(Item).Properties collection
seems to have a count of 0 i.e. there are no properties created
as standard. You can use the Add method of this collection to
append a custom property to a form however, if you export or
import the form, the property does not move with it. So these
properties are not Forms(Item) properties or Documents(Item)
properties but seem to belong to another collection that resides
with the CurrentProject rather than the AccessObject itself so as
far as I can tell it wouldn't work for the OPs scenario.

That's very, very interesting. I would have assumed that a custom
form property would have travelled along with it when imported.

What does the custom property look like in the text version that you
can see with Application.SaveAsText?
 
D

David W. Fenton

CurrentDb.Containers("Forms").Documents("myForm").Properties
contains some default properties and is a completely different
collection to
Application.CurrentProject.AllForms("myForm").Properties which
contains no default properties (same with AllMacros, AllModules,
AllReports).

The are different collections, but they should be returning the same
list of objects.
You can add 'Custom' properties to both collections.

But you're not adding the properties to the collections, but to the
objects in the collections. Certainly, as we've seen, the AllForms
collection doesn't seem to provide access to the same properties of
a form object as the path through the documents collection, but that
doesn't mean that the objects being accessed are different.
If you export/import "myForm", custom properties added to
Documents("myForm").Properties are included, whereas those added
to AllForms("myForm").Properties are not.

If you SaveAsText no custom properties from either collection are
included.

Really? Hmm. That's very interesting.

It would seem, then, that the object returned by each of the
collections is *not* the same, as you said. By going through the
documents collection, you're returning a document object, one that
happens to contain a form, while through the AllForms collection
seems to return, well, it's not clear to me what it returns. It's
..Type property says it's a form, but it doesn't actually return a
usable form object (as in what you get in the forms collection),
since the form isn't open.

As an analogy, consider as subform control on a form and the form it
contains. It seems to me, that these two collections return an
object that is container for the form (like the subform control),
not the actual form itself. Thus, adding a property to the objects
returned via these collections would be like adding a property to a
subform control -- it wouldn't change the properties of the form
embedded in that control.

Now, what is surprising to me is that the container returned by
these two collections is apparently not the same container, nor even
of the same type. I suspect it's some ADO/DAO issue, but that's just
speculation.
 
D

David W. Fenton

Actually by Collections I meant the respective Properties
collections.

But what matters is what type of *object* that is the properties
collection *of*. Certainly the properties of a subform control are
distinct from the properties of the form embedded in it, and my
guess is that this is why the two methods produce different
properties collections, because they are returning different
objects.
That's a pretty good analogy but it's odd that one container's
properties are exported with the Forms but the other's not.

I think this points to the fact that the AllForms path to form
properties is not really Jet-aware, and that whatever is being
altered there when you add a property is not really part of what
Access considers to be part of the form's definition.

This is one of the reasons I've continued to use the documents
collection, even though it's much harder to set up (though I usually
just cut and paste...) than the AllForms collection.
 

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

Similar Threads

Immediate Window? 2
Deleted Code Module! 1
Refresh Table 3
Set object variable in class module 5
Unload RecordSource from Form 5
Import Form 9
Loop through table def 3
Hiding Access 3

Top