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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax issue subform

Status
Not open for further replies.

hargy

Technical User
Jan 22, 2002
38
0
0
GB
I need to find out how to reference a subform within a main form with VBA.

I have a Order Details subform in a main purchasing form. Buttons are available on this subform and I wish to control these using the forms On Load event rather than the subform load event. That means I can control the status of the buttons with one section of code rather than several which makes the coding longwinded and laborious.

Is there a way of referencing these in code like Me![btnClick]. Can the subform be included as part of this Me statement or since it is a separate form does it require an explicit statement?
 
[Forms]![MainForm].[SubformName].Form.ControlName is the correct way to reference a subform.

Hope that helps

Craig
 
Craig

Still having problems - this is one of the problems I find with VBA - the different syntaxes for doing the same thing

I have seen the following in the Reference guide

strString = Forms!Formname.field
strString = Me!field

I am basically confused as to when to use [] and when not to.

I have a control called btnAuthoriseLM on a subform called tblLMAuthorisation SubForm sitting on a main form called frmPurchaseRequest. I wish to change the Enabled property of this button - I can t use Me because I am coding in an external module and so need to reference this button properly.

I have tried every differnet combination of with or without [] and can't get the code to work - what is the deal with the brackets and how do they work?

How do i get to my button from the external module?

The line I've used is

[Forms]![frmPurchaseRequest].[tblLMAuthorisation Sub].btnAuthoriseLM.Enabled = True

I am really scratching my head on this one and its probably really simple.
 
[] is used when there is a space in an object name.....which you shouldn't really create by object naming rules.....

The syntax you should be using is.....

[Forms]![frmPurchaseRequest]![tblLMAuthorisation Sub].Form!btnAuthoriseLM.Enabled = True

Craig
 
Craig

I now get an error saying

Runtime error 2465

Microsoft Access cannot find the field 'tblLMAuthorisation subform' referred to in your expression

I then checked it against your original syntax and did

[Forms]![frmPurchaseRequest].[tblLMAuthorisation].Form.btnAuthoriseLM.Enabled = True

Same runtime error

This time

Microsoft Access cannot find the field '|' referred to in your expression

This is weird - I cannot believe its this difficult to reference a form in the code I am using - there isn't even the slash "|" character in that line - I've copied it from my code and still can't se it.

Is there an easier way of achieving what I need to do. eg can I create a variable such as frmCurrentForm as Form???? that can replace the extensive line of code I am using - I am still going to need to tell the variable where to look though.

Help. This is really holding me back, and I've applied just about every last ounce of logic at it - typically MS have failed to explain this concept at all well in the Helpfiles - they do it different ways in each of the places I read. It just doesn't make sense to me and this is an important part of writing external procedures and functions


 
Heres what I used to reference mine, and I do feel your pain on this subject, its hard to find anything about it.

[Forms]![Customers]![Problem Entry]![assigned to]
main sub field

I just looked up the difference between ! and .

!(bang):separates an objects name form the name of the collection of which it is a member.

.:separates each semiqualified object name in a fully qualified objet name. It signifies the next step in the hierarchy

If that makes sense to you then you're doing better than me. Just try swapping out the 2nd and 3rd ! and .'s until it works, or until you want to throw your box against the wall. Sam Greene
anyone in need of a rock induced headache? if so
 
Just to drop in here, this may help:

1) Brackets are REQUIRED around ANY object with embedded spaces.

2) Brackets WON'T HURT around ANY OBJECT.

3) Exclamation Point (BANG) separates a collection from a member of the collection, e.g. Forms!FrmCustomers

4) Exclamation Point (BANG) separates an Collection Object from a CONTROL in that object, e.g. frmCustomers!CustID

5) Period (DOT) separates an(y) object and a PROPERTY of that object, e.g. CustID.Value

6) The ME keyword references the currently active high-level object, and you most often see it in CBF modules. With no active object, the "ME" reference will generate an error. IOW, don't use "ME" in general modules.

7) I've heard people claim that you can use DOTS everywhere and don't need to mess with the BANGS, in effect saying

Forms.myFormname.Custid.Backcolor
is functionally/syntactically equivalent to Forms!myFormName! CustID.BackColor

Another way I've heard to keep them separate is that you use a BANG in front of something YOU CREATE and a DOT in front of something that is an ACCESS keyword/feature/method/property/whatever..

I made FrmCustomers : !frmCustomers
Access made "Backcolor": .Backcolor

Code WITHIN frmCustomers can refer to frmCustomers with the "ME" keyword:

Me!CustID.Value

Many people find the With...End With construct useful:
Code:
With
ME
Code:
   .custid = "100"
   .Address = "1313 Mockingbird lane"
   .City = "Podunk"
   .State = "LA"
etc etc etc
Code:
End With

Jim Hare
"Remember, you're unique - just like everyone else"
Feel free to visit another free Access forum:
or my site,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top