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

Part of the code that I want to exe

Status
Not open for further replies.

bhujanga

Programmer
Oct 18, 2007
181
US
Part of the code that I want to execute when a particular command button is clicked upon, is to move to the first record of the form. This is a small form in which all 14 records of the small dataset in 'continuous' mode. The number of records never changes, as they are just settings to be used elsewhere, so we toggle them in this small form.
This command button is for setting them back to their defaults which are just stored in an unseen field in the same form. Hence the 'Set to Defaults' button just runs a little UPDATE SQL that changes 'Current_State' to 'Default'.
That all works fine with one caveat, which is that whatever the last record that was changed was doesn't update unless you have since left that record. Hence in my code before running the SQL, I want to move them out of that record.
I plan to do this by sending them to the first record and then to the next record (in case they were already on the first record). It is further noted that this form is a subform and the command button resides on this subform itself, not the parent form.

So, I use:
DoCmd.GoToRecord acDataForm, "AP_Parameter_Selection!AP_Parameter_Subform_Search_Fields" , acFirst

and I've tried every combination of using/not using quotes, brackets, not specifying parent form, using "Forms!", etc. that I can think of and nothing works.
In most variations I get an error message telling that the specified form is not open. What am I doing wrong?
 
Forget that workaround. Simply commit the form changes to the database. Try adding
Me.dirty = false
before your code. This will commit the changes. If the command button is on the parent form then
me.YourSubformControlName.Form.dirty = false
 

TRY
Code:
docmd.GoToRecord acDataForm,me.name,acFirst
 
The Me.Dirty thing worked great. A curious thing happens with this small block of code that I've not noticed happening elsewhere. I have a docmd.setwarnings (False) at the beginning of the code so it doesn't alert me about the changes it's going to make. As soon as I put in any other statement before the SQL run, it seems to disable it and the warnings come back on, so I had to place the setwarnings statement right before the SQL run. Not a big deal but rather odd.
Thanks for your help.
 
Neither PWISE's code and your code will ever work. You can never refer to a subform by name. The only way to reference a subform is through its parent form and subform control. Assume your subform is called "SubA" and you open its parent form. If you looped the Forms collection you will only see the parent form in the collection of open forms. Another way to think of this is to put the same subform on two different forms or twice on the same form. If you tried to refer to it by name, which instantiation would you be talking about.

So if you want to reference a control on "SubA" opened as a subform on a parent form, you cannot call it by name
Forms("SubA").textBox1
you have to go through the parent form and its subformcontrol and then the subform in the subformcontrol
Forms("ParentForm").SubAControl.Form.textbox1

Your docmd code would work when called from the subform by simply
docmd.gotorecord ,,AcFirst

Since there is no way to supply a subform name this code without the first two arguments defaults to the active form.
 
Your docmd code would work when called from the subform by simply
docmd.gotorecord ,,AcFirst

When I tried this, it said that an argument wasn't optional. I'm happy with the Me.Dirty method for this instance, but I'm trying to understand why things do what they do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top