SetFocus on embedded subform - error 438

G

Guest

I have a data entry form with several embedded subforms. I am trying to
automatically move to a specific control on one of the subforms, but am
getting error 438 (object does not support this method).

What am I doing wrong?

strFormName is the name of the main parent form
frmLastUpdated is the name of one of the embedded subforms
EventDate is a control on the frmLastUpdated subform

Public Static Sub CreateLastUpdated(strFormName As String)

Forms(strFormName).Form.SetFocus
Forms(strFormName).Form!frmLast_Updated.SetFocus
Forms(strFormName)!frmLast_Updated.Form!EventDate.SetFocus

P.S. I am new to programming, and I keep getting stymied and stuck and hung
up for hours on syntax-type questions like this -- despite VBA for dummies
and online help. What's the best way to educate myself so I can code better
and faster?
 
A

Allen Browne

Anita, there could be several things going on here.

Which line gives the error?
The first one looks okay, assuming the form is actually open.

The second line doesn't need the ".Form" bit to refer to the subform
control. The line should work if the form is open long enough to have the
subform loaded, though it could fail if you call this too early (e.g. in the
Open or Load event of another subform on the same form.) Also worth checking
that the subform control is named frmLast_Updated, since the name of the
subform control is not necessarily the same as the name of the form loaded
into it (its SourceObject.)

The 3rd line looks okay if the control is named EventDate. Again, the
control's Name may be different from its Control Source. For example, if the
text box is named txtEventDate, Access would recognise the name EventDate as
a *field* of the form's RecordSource, but not as a control on the form.
Access can SetFocus only to a control, so it tells you that the object (a
field) does not support the SetFocus method, i.e. you can SetFocus to a text
box but not to a field.

Your final question (in the PS) was about how to learn this stuff. If you
have no previous experience with programming (like many who start out in
Access), you probably feel like you are facing an enormous learning curve.
You are learning basic programming techniques (variables, types, loops,
branches, operators, keywords, assignments, comparisons, ...) as well as the
object hierarchies (forms, controls, properties, methods, collections,
recordsets, ...), as well as database-specific concepts (relations, indexes,
types, validation, and the SQL language), and when and how to use the events
Access exposes to you. There really is a bunch of stuff to come to grips
with--no matter how fast you learn, it's going to take some time, so be
patient with yourself.

Find out how you learn. If you can learn from books, get some good ones
(beyond Dummies), and work through creating and experimenting with the
examples, chapter by chapter. You will learn by doing. Ultimately, you don't
know what works until you have experimented enough to know what doesn't work
too.

Others learn by interaction with others. If that's you, get into a class
that is more than just "Intro to Access", or get some videos. Again, you
have to experiment to grasp what works and what does not.

And feel free to keep asking questions as you experiment. It is important
that you understand why and how something works, not just that it works.
 
G

Guest

Allen -- I can't thank you enough for not only posting an "answer" but for
taking the time to explain it thoroughly and offer advice. THANK YOU!

The 3rd line was the culprit -- the Name of the Control Source and the field
were different, just as you guessed.

(With the name corrected, I'm now finding that the setfocus code works from
all of my embedded subforms but one. Can't for the life of me figure out why
this one particular subform is behaving differently than the rest, but it's a
bug I can live with.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top