Full Version: Subforms
UtterAccess Forums > Microsoft® Access > Access Forms
NewbieNew
I have four controls on a subform. Two of those controls are Combo Boxes.
want want to do something like the following:
If Comb14 (in the subform) = "Discount" then
Cost.setfocus
end if
(I can't get this to work)
Now, do I need to refer to the subform to do this?
like.....Me.Subform.Form!Combo14 ??
FOr is it just that there's something different about Combo Boxes?
Appreciate any suggestions. I want to learn this stuff, so If you could just point me in a general direction, I'll try to do the rest...
Thanks
darnellk
Looks like you're on the right track.
You could try referencing your combo like this:
MainFormName!SubformControlName!ComboBoxName
theDBguy
Hi,
welcome2UA.gif
It could be a combination of both. Where do you have this code? If in the main form, then yes, you'll need to reference the subform control too. What is the bound column of the combobox? If to a numeric column, you'll need to use the numeric equivalent of "Discount."
Just my 2 cents... 2cents.gif
NewbieNew
I'm getting an 'Object Required' error when I use that particular line of code.
Just so I'm straight, you mean the Subform Control name, which is the source for the Combo Box? (On my subform, Combo14 is controlled by the control called Area...
Thanks
darnellk
Please look at DBGuys question as well to clarify a couple of things for us.
believe I made a mistake with my example and you were closer than I was. According to the wiki link here, it looks like you do need to include the ".Form" reference. So it should be:
MainFormName!SubformControlName.Form!ComboBoxName
I'll try to explain it with an example:
If you want to place Form2 inside of Form1 as a subform, it may look like this:
Form1 (MainFormName)
--subForm2 (SubformControlName) - usually named the same as Form2, but I add the prefix "sub" to it so I know it's a subform control
----Form2 (this is the SourceObject of subForm2)
------Combo14 (ComboBoxName)
I hope this clarifies the reference a bit.
NewbieNew
Okay, I can't get anything to work, so let me try to provide some additional info....
My problem is that, for some reason, when I'm using a Combo Box in my subform, I can't get my code to work....
So let's say, in my subform, I have a combobox that allow me to choose from 20 different book titles. If one specific title comes up, say, Moby Richard, I want the program to stop.....(Because I know, at this point, if I can do that, I can make the rest work)
I've tried everything....
(So, for example: )
If Forms!MainForm!Subform.Combobox = "Moby Richard" then
stop
end if
Interestingly, in the subform, if the control isn't a combobox, I can make it work....
So that's the weak link, I think......the combo box.....
Again, thanks.
darnellk
To quote DBGuys response:
If your combo box has a hidden column storing an ID related to "Discount", you can either check for the numeric ID value, or use "ComboBox.Column(1)" which would look for the "Discount" text in a 2 column combo box (combo box column numbers are zero based - 1st column is column 0, 2nd column is column 1, etc.).
For instance, is your combo box like this?
1st Column : 2nd Column
ID : Description
1 : Standard Rate
2 : Discount
3 : Additional Fee
etc.
two different checks would look like:
Forms!MainForm!Subform.Combobox.Column(1) = "Discount"
OR
Forms!MainForm!Subform.Combobox = 2
NewbieNew
Thanks for the suggestions and help....
This is what I used....
If Me!Combo14.Column(1) = "discount" Then
And it worked perfectly....
Thanks to all...
Galaxiom
Ignoring the fact that the OP needed Column(1), no. You didn't actually make a mistake.
That table just leaves out your perfectly legitimate syntax for simplicity and avoiding further confusion in an already bewildering topic for new developers.
One can refer to controls and fields in the subformcontrol's SourceObject without the .Form provided the Bang operator is used. It won't work with a Dot.
It works because the Bang in that context indicates the default Collection, in this case, Controls.
The first collection Acess finds in the subformcontrol is Controls in the SourceObject.
(After this it will go for the default collection of the form's RecordSource, ie Fields if it doesn't find a control by the name.)
Dots are actually meant for properties.
Dots also work in most places in DAO where a Bang would be expected because it exposes the items in the Controls collection as Properties of the form object.
However although the controls are exposed as Properties of the form used as the SourceObect Property they are not carried through as Properties to the subformcontrol itself, so Dots don't work in that context.
However most developers stick to the reference via the .Form Property of the subformcontrol for several reasons:
They learnt how to refer to objects from that page you quoted and don't know about the other syntax.
Intellisense does not support the Bang.
Consistency (remembering that properties of the SourceObject must include .Form)
darnellk
Thanks for the clarification Galaxiom. I was pretty sure I had used the bang operator successfully before, but did not want to steer the OP in the wrong direction.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.