Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 08-11-2018, 04:28 AM   #1
grenee
Newly Registered User
 
Join Date: Mar 2012
Posts: 188
Thanks: 31
Thanked 1 Time in 1 Post
grenee is on a distinguished road
Setting a form as a variable

Good Day All,

I want to create a variable of Form type. Then I want to access the members. This code is my effort, But it is not working. Would appreciate some advise

Code:
   
    Dim PoForm As Form
    
    Set PoForm = Forms![Purchase Orders Payments]
    
    PoForm.Currency

grenee is offline   Reply With Quote
Old 08-11-2018, 05:47 AM   #2
The_Doc_Man
Happy Retired Curmudgeon
 
Join Date: Feb 2001
Location: Suburban New Orleans, LA, USA
Posts: 11,989
Thanks: 56
Thanked 1,093 Times in 999 Posts
The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold
Re: Setting a form as a variable

First, since "Currency" is a data type name, it is not a good choice.

Second, you are using "property" syntax, not "member" syntax. With Currency as a reserved word, it would look to Access like you were searching the form for that name as a property. Which will fail since there is no form property called Currency.

Third, be sure that the form is open before trying this since if the form is not open, the controls are only virtual.

Try

Code:
Set PoForm = Forms![Purchase Orders Payments]
PoForm![Currency] = ...
(or)
something = PoForm![Currency]
But it would be a really good thing to name-change any controls that happen to match ANY Access keywords. I'm always unhappy to realize that Access took up all of the really good keywords like Date, Time, Height, Width, etc... but then, having a more specific name like MyHeight or StartDate helps in the long run because of mnemonic value.
__________________
I'm a certified grandpa (3 times now) and proud of it.
Retired over one year and survived being home all day with the wife. She must really love me.
If I have helped you, please either click the thanks or click the scales.
The_Doc_Man is offline   Reply With Quote
Old 08-12-2018, 10:57 AM   #3
grenee
Newly Registered User
 
Join Date: Mar 2012
Posts: 188
Thanks: 31
Thanked 1 Time in 1 Post
grenee is on a distinguished road
Re: Setting a form as a variable

Thanks Mr_Doc_Man for your assistance and guidance.

I applied your council but I am still getting a little error message so maybe you can assist further:
Here is my new code:

Code:
Dim PoForm As Form
    Dim a As String
    
    Set PoForm = Forms![Purchase Orders Payments]
      a = PoForm![PayAmt]
    MsgBox a
This is the error message:

There is an invalid use of the . (dot) or ! Operator or invalid parenthesis

grenee is offline   Reply With Quote
Old 08-12-2018, 11:06 AM   #4
ridders
Part time moderator
 
ridders's Avatar
 
Join Date: Jan 2017
Location: Somerset, UK
Posts: 5,805
Thanks: 79
Thanked 1,428 Times in 1,333 Posts
ridders is just really nice ridders is just really nice ridders is just really nice ridders is just really nice
Re: Setting a form as a variable

You haven't said where the code is being used

Try a = PoForm.PayAmt
__________________
If this answer has helped, please click the Thanks button and/or click the 'reputation scales' symbol on the left.

New example databases:

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


Colin
Access 2010 32-bit, Access 2016 32-bit & 64-bit, SQL Server Express 2014, Windows 10,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

-----------------------------------------------------------------------------------
nil illegitimi carborundum est
ridders is offline   Reply With Quote
Old 08-12-2018, 11:07 AM   #5
June7
Newly Registered User
 
Join Date: Mar 2014
Posts: 498
Thanks: 0
Thanked 113 Times in 113 Posts
June7 will become famous soon enough
Re: Setting a form as a variable

Why do you want to do this? I have never found the need to declare and set a form object variable.

However, code works for me using ! (bang) or . (dot).

Is the form actually being used as a subform? If so, this code won't work.
__________________
To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression. Attachment Manager is below Advanced editor window, click Go Advanced below Quick Reply window.
June7 is offline   Reply With Quote
Old 08-12-2018, 11:58 AM   #6
The_Doc_Man
Happy Retired Curmudgeon
 
Join Date: Feb 2001
Location: Suburban New Orleans, LA, USA
Posts: 11,989
Thanks: 56
Thanked 1,093 Times in 999 Posts
The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold
Re: Setting a form as a variable

Galaxiom had a really good post about the virtues of dot (.) vs bang (!) and when each was appropriate. Where there is no ambiguity, the dot and bang work equally well for a control, though bang doesn't work for everything.

However, June7, the syntax in question should work. It might not look orthodox, but as long as the named form is open, there is no ambiguity in the path from the forms collection through the named form to a control residing on that form. Were you perhaps thinking of another way to reference the form?

Grenee, your Forms![form name] syntax is not wrong, but you COULD also have used an alternative syntax - Forms("form name") - as a way to specify the form as a named member of the Forms collection. The "SET" syntax will accept a form reference that way, too.

Code:
    Dim PoForm As Form
    ...
    Set PoForm = Forms("Purchase Orders Payments")
    MsgBox PoForm![PayAmt]

    ...    (or)

    MsgBox Forms("Purchase Orders Payments")![PayAmt]

    ...
My last example should work without the DIM statement or any extra variable.

As to dot/bang issues, see this thread and the links within it.

https://access-programmers.co.uk/for...light=Dot+Bang
__________________
I'm a certified grandpa (3 times now) and proud of it.
Retired over one year and survived being home all day with the wife. She must really love me.
If I have helped you, please either click the thanks or click the scales.
The_Doc_Man is offline   Reply With Quote
Old 08-12-2018, 12:21 PM   #7
June7
Newly Registered User
 
Join Date: Mar 2014
Posts: 498
Thanks: 0
Thanked 113 Times in 113 Posts
June7 will become famous soon enough
Re: Setting a form as a variable

I agree it should work (it did for me, as stated) if the form is open as a FORM and not a SUBFORM.

__________________
To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression. Attachment Manager is below Advanced editor window, click Go Advanced below Quick Reply window.
June7 is offline   Reply With Quote
Old 08-12-2018, 01:23 PM   #8
grenee
Newly Registered User
 
Join Date: Mar 2012
Posts: 188
Thanks: 31
Thanked 1 Time in 1 Post
grenee is on a distinguished road
Re: Setting a form as a variable

I tried this recommendation:

Code:
   a = PoForm.PayAmt
but it throws the same error message:
grenee is offline   Reply With Quote
Old 08-12-2018, 01:32 PM   #9
grenee
Newly Registered User
 
Join Date: Mar 2012
Posts: 188
Thanks: 31
Thanked 1 Time in 1 Post
grenee is on a distinguished road
Re: Setting a form as a variable

Quote:
Originally Posted by June7 View Post
Why do you want to do this? I have never found the need to declare and set a form object variable.

However, code works for me using ! (bang) or . (dot).

Is the form actually being used as a subform? If so, this code won't work.
The form is not being used as a subform.

My ultimate objective is to pass some of the form's text fields as byref arguments to a function where the text fields can be tested for emptiness etc. The function would return a Boolean result.

I know that the test can be done directly on the opened form but I want a generic function that can be used on several forms when the need arises.

So at the moment I making sure that the process for referencing the form as variable works before I go on to the next step. Obviously if it can't work here it can't be passed as a parameter to a function.
grenee is offline   Reply With Quote
Old 08-12-2018, 01:55 PM   #10
June7
Newly Registered User
 
Join Date: Mar 2014
Posts: 498
Thanks: 0
Thanked 113 Times in 113 Posts
June7 will become famous soon enough
Re: Setting a form as a variable

Since I cannot replicate the issue, perhaps you should provide db for analysis. Follow instructions at bottom of my post.
__________________
To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression. Attachment Manager is below Advanced editor window, click Go Advanced below Quick Reply window.
June7 is offline   Reply With Quote
Old 08-12-2018, 02:11 PM   #11
grenee
Newly Registered User
 
Join Date: Mar 2012
Posts: 188
Thanks: 31
Thanked 1 Time in 1 Post
grenee is on a distinguished road
Re: Setting a form as a variable

Quote:
Originally Posted by The_Doc_Man View Post
Galaxiom had a really good post about the virtues of dot (.) vs bang (!) and when each was appropriate. Where there is no ambiguity, the dot and bang work equally well for a control, though bang doesn't work for everything.

However, June7, the syntax in question should work. It might not look orthodox, but as long as the named form is open, there is no ambiguity in the path from the forms collection through the named form to a control residing on that form. Were you perhaps thinking of another way to reference the form?

Grenee, your Forms![form name] syntax is not wrong, but you COULD also have used an alternative syntax - Forms("form name") - as a way to specify the form as a named member of the Forms collection. The "SET" syntax will accept a form reference that way, too.

Code:
    Dim PoForm As Form
    ...
    Set PoForm = Forms("Purchase Orders Payments")
    MsgBox PoForm![PayAmt]

    ...    (or)

    MsgBox Forms("Purchase Orders Payments")![PayAmt]

    ...
My last example should work without the DIM statement or any extra variable.

As to dot/bang issues, see this thread and the links within it.

https://access-programmers.co.uk/for...light=Dot+Bang
I tried both ways by there is still a problem.

Here is another observation that might help you to help me.

When I use
Code:
me.PayAmt
From the time I enter the . (dot) after me, I get a drop down list that displays all the member which I put on the form along with the access members. However when I enter the . (dot) after PoForm I get only the Access members. Now that throw a red flag for me because after both dots should have the same response since I am referencing the same form.
grenee is offline   Reply With Quote
Old 08-12-2018, 02:37 PM   #12
The_Doc_Man
Happy Retired Curmudgeon
 
Join Date: Feb 2001
Location: Suburban New Orleans, LA, USA
Posts: 11,989
Thanks: 56
Thanked 1,093 Times in 999 Posts
The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold
Re: Setting a form as a variable

Regarding ME vs the form variable, no. They should NOT be the same.

Using "Me." triggers a drop-down list of the elements currently defined in the form being considered, and I will infer that you are dealing with code in the form's Class module - because that drop-down based on "Me." can't occur in a general module. Further, to enter code, you are in design mode.

Using the form object variable, you might have provided a code line before that to define which form object you wanted to use, but at design time that form object variable is not populated. The code hasn't executed yet, so you cannot expect to see the members YOU have added (to the form). You can ONLY expect to see elements that are part of the basic object definition. Therefore, what you describe is exactly what I would have expected to see.
__________________
I'm a certified grandpa (3 times now) and proud of it.
Retired over one year and survived being home all day with the wife. She must really love me.
If I have helped you, please either click the thanks or click the scales.
The_Doc_Man is offline   Reply With Quote
Old 08-12-2018, 02:50 PM   #13
Uncle Gizmo
Nifty Access Guy
 
Uncle Gizmo's Avatar
 
Join Date: Jul 2003
Location: Newbury Berks UK
Posts: 9,295
Thanks: 340
Thanked 748 Times in 714 Posts
Uncle Gizmo is a jewel in the rough Uncle Gizmo is a jewel in the rough Uncle Gizmo is a jewel in the rough
Send a message via Skype™ to Uncle Gizmo
Re: Setting a form as a variable

Quote:
Originally Posted by grenee View Post
My ultimate objective is to pass some of the form's text fields as byref arguments to a function where the text fields can be tested for emptiness etc.
"Some of the Text fields"

What identifies the ones you want to inspect?

What feature?

If not a feature, then how do you mark them?

Something in the tag property?
A coding of the control name?
Possibly you select then by there contents, date, numeric, text. ..

Once you specify how you identify the controls, then it would be quite easy to write a routine that would inspect all of the controls in the forms controls collection, select the particular controls by the method you use to identify them, then test them, change them, report on them, what ever you require...

See. My blog here:-

http://www.niftyaccess.com/loop-thro...t-of-controls/

Which demos the method I'm suggesting...


Sent from my SM-G925F using Tapatalk
__________________
Get $20 worth of "Nifty Code" and "Nifty Sample Databases" for free - See what's on offer on this page here:-
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Uncle Gizmo is offline   Reply With Quote
Old 08-12-2018, 03:02 PM   #14
MarkK
Super Moderator
 
MarkK's Avatar
 
Join Date: Mar 2004
Location: Vancouver BC
Posts: 7,674
Thanks: 10
Thanked 1,266 Times in 1,205 Posts
MarkK is a name known to all MarkK is a name known to all MarkK is a name known to all MarkK is a name known to all MarkK is a name known to all MarkK is a name known to all
All forms that you create inherit from Access.Form, so you can run code like this...
Code:
Private Sub Command7_Click()
    Debug.Print "The current form is type:", TypeName(Me)
    Debug.Print "Is Me a Form object? ", TypeOf Me Is Form
    Debug.Print "Is Me a Form_Form3 object? ", TypeOf Me Is Form_Form3
End Sub
... which prints this to the immediate pane ...
Code:
The current form is type:   Form_Form3
Is Me a Form object?        True
Is Me a Form_Form3 object?  True
... showing that the object is a Form and Form_Form3 at the same time.

If you want to see intellisense showing the custom methods and properties of your form, then declare a variable of that type by changing...
Code:
    Dim PoForm As Form
... to ...
Code:
    Dim PoForm As Form_fMyPoForm
... where the purple part is the name of your form.
hth
Mark
__________________
formerly known as lagbolt | Windows 10 | Access 2010 | Visual Studio 2013 | "Institutions have a vested interest in perpetuating the problems to which they are the solution." - Clay Shirky
MarkK is offline   Reply With Quote
Old 08-12-2018, 05:59 PM   #15
grenee
Newly Registered User
 
Join Date: Mar 2012
Posts: 188
Thanks: 31
Thanked 1 Time in 1 Post
grenee is on a distinguished road
Re: Setting a form as a variable

Quote:
Originally Posted by Uncle Gizmo View Post
"Some of the Text fields"

What identifies the ones you want to inspect?

What feature?

If not a feature, then how do you mark them?

Something in the tag property?
A coding of the control name?
Possibly you select then by there contents, date, numeric, text. ..

Once you specify how you identify the controls, then it would be quite easy to write a routine that would inspect all of the controls in the forms controls collection, select the particular controls by the method you use to identify them, then test them, change them, report on them, what ever you require...

See. My blog here:-

http://www.niftyaccess.com/loop-thro...t-of-controls/

Which demos the method I'm suggesting...


Sent from my SM-G925F using Tapatalk
I followed the video on this link and it provided me with exactly what I needed; that is to pass a text box to a function. Actually the solution example is in not in the text box video but rather in the check box video. Anyways it is working fine.

Thank to you and all the others who offered me assistance today. I would actually study some of the other solution too to see if there is more to pickup

grenee is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting A Form Variable detrie Modules & VBA 1 02-11-2013 03:28 PM
Setting the URL for a WebBrowser from a Form Variable Tezcatlipoca General 8 01-22-2009 05:39 AM
need help setting a variable Jim W General 6 08-23-2004 09:11 AM
Setting a Form Variable FROM a string Randomblink Modules & VBA 1 08-25-2003 01:33 AM
[SOLVED] Set statement (setting a form name as a variable) HAL2100again Modules & VBA 1 03-23-2001 02:30 AM




All times are GMT -8. The time now is 04:57 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Sponsored Links

How to advertise

Media Kit


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World