Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

same or different...another scoping issue?

Status
Not open for further replies.

SilverBean

Programmer
May 23, 2001
44
US
I've got a good amount of VB knowledge mainly started with VB 4, alot of work with VB 5, a
little with 6.

As I work with Access 2002, I see alot that is the same from VB and alot
that is different; hence the name VBA.

I checked out some previous posts on scoping and I came up with Global which
I don't recall playing with before. I wasn't able to check if the impact on my observations below.

Anyway what I'm trying to do is to get information from a Form down to a
child on that form in order for the child to add or edit its particular
source (another table in this instance). By the way I do have Automatic
Variable declaration turned off and I'm aware of that effect on the code I
write.

So in the parent form I have in the General Declarations:

Option Compare Database
Public VariableForChild as String

and when needed I put something in it that I want the child to have. I do
this in the PARENT for example on a button click.

Private Sub SomeButton_Click()
VariableForChild = 2
Sub End

Now when the child needs to put that information in the table I do a

Private Sub Form_BeforeUpdate(Cancel as Integer)
[FID] = Form_Parent.VariableForChild
End Sub

Ok, so it does work HOWEVER, it does not work all time. Specifically I've
traced it down to the initial time that this portion is executed. It seems
my Form_Parent.VariableForChild never gets intializied its Null and the
Update barfs on that. However, I've traced through displayed status Message
Boxes and after the first time subsequent "adds" let's call it are fine the
right variable gets put in FID. I have tried [FID].Value and that was no
help.

Its almost like in the Click event the VariableForChild and
Form_Parent.VariableForChild are different, however if I remove the Public
declaration the click events barfs because VariableForChild
undefined.....very interesting. In the button click event if I use
Form_Parent.VariableForChild = 2 that works fine all the time. That seems a
little redundant overkill - I'd like to have a reason as to what the correct
syntax is here. Me.VariableForChild = 2 is very attractive I could live with
that but why? Obviously VB something knows its the same variable, object
whatever?

Questions are: Once again what I'm trying to do is get some data from a Form
down to its child so that the child can put it into a table. Its really not
possible for a link(on this data) - not sure if that would even help.

Q1. Whats wrong with the above? If my VB memory serves everything above is
fine and it does work.
Q2. Any suggestions on a VBA-safe way to do this?
 
Q1. I do not know if that is possible. If I use Globals I declare them in a module and and then I can reference them from any place in my application by there name (no need to add the name of the form). On opening a form I always set the default value of a variable so I always know what the starting value is. So I have not seen the problem (yet).

Q2. The most simple solution is to create a textbox that is not visible if the form is open. Place the value you need in the subform and reference the textbox from the subform (i.e. Forms!<form_name>.form.<textbox>

W.
 
Hi SilverBean,

I don't see anything wrong in what you are doing in theory, so a couple of quick questions ..

Exactly how are you referencing the parent form from the subform?

What do mean when you say you have a problem with a Null? Your (global) variable will never be null because you have declared it typed (as a string).

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
I lied thats not exactly what I was doing. However I did uncover some very
interesting behavior. I think I understand the "VB" part ok but its the "A"
that has me. I have some suggestions but I'd prefer not to use that kind of language here.

The trick is do this:

in a Parent Form call it Parent A

Option Compare Database
Public VariableForChild as String


Private Sub SomeButton_Click()
VariableForChild = 2
Sub End

So the variable gets set on the parent form and used on the child form as in:

Then when the child needs the value it should do this

Private Sub Form_BeforeUpdate(Cancel as Integer)
[FID] = Form_Parent.VariableForChild
End Sub

This does work fine, although I think the Public declaration in the
form is not touted in VBA. In fact its taboo from sources I've seen.

Now my original complaint involed the above scenario as a child on another parent form. Say Parent B
Now in this instance the code will not work, initially - oddly enough it will start
working after a few clicks and changing of focus among the parents and the
child involved. Very strange.

My take on it is that there are some VB gremilins running around in VBA,
such that this almost works but it can't under VBA. My guess something with
the first form setting up memory space, allocating, whatever deep dark
internal working. I'm sure Uncle Bill would just have fits about this one,
but it finally got nice here in the Northeast I'm not in the mood to ruin
his weekend.

Mostly Fiep gave me the answer in part B there had to be a away to do this
it was just how. I hate to have a global variable everytime I want to send
information to a child. Thanks Tony for agreeing with me that was a sanity
check it looked ok in VB.

PS as for the NULL, I don't know terminology thing a NULL string? Its
strange in this(and only this) more complex parent, parent(previous), always
child arrangment I just documented Form_Parent.VariableForChild is never set
to 2. I tried all types, integers, strings, double, booleans just won't
work. So either it was "", 0, or whatever just not initialized to anything.
Then all of a sudden it would get initialized and start working fine. All
along VariableForChild would BE set to 2. Oddly enough if I change the name
in the public declaration (i.e, notVariableForChild) VB was johnny on the
spot telling me undeclared at the button click (i.e, "VariableForChild =
2").
 
Hi!

I don't use the Form_FormName syntax notation, and don't know if that's got anything to do with it, but usually one would refer to controls, methods and properties etc on another form like this (How to Refer to a Control on a Subform or Subreport, although there are many other "allowed" versions). Think that's why TonyJollans asked for the exact referencing.

A variable declared as public within a forms module, is (should be) accessible as any other property of the form, just using appropriate qualifying.

Parent forms controls, methods and properties from subform:
[tt]Me.Parent!txtControl.Value
Me.Parent.Requery
Me.Parent.MyPublic[/tt]

Subforms controls methods and properties from main form:
[tt]Me!SubFormControlName.Form!txtControl.Value
Me!SubFormControlName.Form.Requery
Me!SubFormControlName.Form.MyPublic[/tt]

Refer to controls methods and properties on another open form:
[tt]Forms!FormName!txtControl.Value
Forms!FormName.Requery
Forms!FormName.MyPublic[/tt]

Refer to controls methods and properties on another open forms subform:
[tt]Forms!FormName!SubFormControlName.Form!txtControl.Value
Forms!FormName!SubFormControlName.Form.Requery
Forms!FormName!SubFormControlName.Form.MyPublic[/tt]

For all these, a more dynamic way of addressing is also available, using the last version, substituting the name of the form and subform with variables:

[tt]dim sSubForm as String
sim sForm as String
sSubForm = "SubFormControlName"
sForm = "FormName"
Forms(sForm)(sSubForm).Form.MyPublic[/tt]

Using these method's I've succesfully referenced controls, methods and properties on open forms.

There are things that might create a confusion when dealing with main/sub form constructs (well, if you're experienced in VB, then also the difference between VB forms and the Access Forms collection, methods and events is supposed to be significant).

The subform isn't recognized as an open form, but as a control on the main form. Because of that, we need to reference it thru the main form on which it resides. The subform control (the container of the subform) has two "challenging" properties, the "source object" which is the name of the subform as viewed in the database window, and then "name" property, by which we reference it (at least in the method I've shown), these might differ.

Did some testing using the Form_FormName notation, and wasn't any more lucky than you.

One thing that also might create some confusion, is the sequence of events. The subforms on open and on load events occur before the main forms - so "initializing" in the wrong sequence, might give Null values/errors in assigning (Null problem?).

Roy-Vidar
 
RoyVidar I thought I'd follow up here, since there appears to be some interest.

I went more with fiep suggestion on this one that is on my subForm I just do a

FID.Value = Form_frmParentA.FID.Value
I reference a control from the subform.

I think. I mean thats what I did - just unable to remember the exact line of code and its not in front of me right now.

As for the Form_frmXXX syntax I don't know just seems to have come along although it is not documented seems to work fine, but I do have an appreciation for what you said above(i.e, I name all my forms beginning with frmXXXX).

As for the my suspision of a bug once I'm to the point of posting I'm seeing some pretty odd behavior which I don't understand so I stand by what I cited above. Now the question can I explain it correctly so all youse can understand it and reproduce thats another whole thing entirely.

For example in the above I think I put a silly line in SomeButtonClick just so I can have a break point after VariableForChild = 2. I break on that silly line and watch VariableForChild and Form_sfrmParentA.VariableForChild and they aren't the same value. Until after several button clicks. I understand the initalizing probably does not occur when I think it does, but if the forms visible and handling button clicks thats not initializing.

This is followup this issue is well past me right now.
 
SilverBean said:
I think I understand the "VB" part ok but its the "A" that has me.
I don't think you have a good understanding of the relationship between VB and VBA. VB is built on top of the very same VBA engine that Access is built on top of. Of course you're going to see a lot of similarities, they are the same language.

That being said, the Access wrapper around the underlying VBA, and the VB wrapper around the underlying VBA are different. So, the differences that you see, are not differences in the VBA, but differences between the Access wrapper and the VB wrapper.

Terminology wise, Null, NullString, and EmptyString are the three completely different things. NULL is an underfined value, NullString is a string that has not been allocated, and EmptyString is string which has no characters.

To the basic of your problem. A form is an object. When you in the declarations sections of that form, create a Public variable, you are defining a public property of that object (Form) that can be referenced anywhere, provide a full object reference is provided, and that both the object and the property have been properly initialized. I highly recommend that all Public Properties of all objects including Forms, be initialized in the Object's contructor, which for Forms, would be the the Form_Load event.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top