Setting focus on a field in a subform from a main form (1 Viewer)

rks062148

New member
Local time
Today, 09:44
Joined
Oct 19, 2012
Messages
9
Hi,

I have a mainform for registering data about a horse entering a show. The horse can enter many class events. After the horse data is entered in the main form, the tab control moves into the subform where the class entries for this horse are posted.

Let's say you enter the first class entry and tab across to a field, and you are done entering data at this point and leave the cursor resting there.
You mouse up to the main form and advance the record to the next horse.

You repeat the process on the main form, but when you tab off into the subform again...the first line appears where you left the cursor last resting on a middle field in the record.

The first field you want to fill out is Class...so it would be nice for the subform to begin by setting focus back onto Class.

Question - when tabbing off a Main form and entering into a subform...how do you guarantee cursor focus will be on a specific field in the subform??

Thanks. Bob
 

VilaRestal

';drop database master;--
Local time
Today, 14:44
Joined
Jun 8, 2011
Messages
1,046
I can't replicate the behaviour you describe. Moving to a new record in the parent then tabbing into the subform always goes into the first control of the subform no matter what control last had the focus in the subform before. Perhaps there's something about your configuration that could cause that but it's not something I've ever noticed.

You could put code in the subform control's Enter event that will force focus to go to the first control on its form. E.g. in the parent form's module, let's say the subform control is called Child1, and the first field on its form is called Field1 then

Private Sub Child1_Enter()
Me.Child1.Form.Field1.SetFocus
End Sub

However, that would be pretty annoying if the user uses the mouse instead of Tab and clicks on a control other than Field1.
 

rks062148

New member
Local time
Today, 09:44
Joined
Oct 19, 2012
Messages
9
Hi and thank you for your reply.

I will clarify a bit here.

We have the Main Form with its controls. It is a form entry and controlled by tabbing from one field to the other. Upon the entry into the last control ( a text box) I then tab again and the tabbing order continues into the subform.

Guess I forgot to mention that this is actually a table in the subform.

So as a horse is registered,this table subform allows for many classes to be assigned to this horse.

The table record goes like Class, Fee, Rider....

Lets say that after the Rider is entered, we leave the cursor parked at the end of this field and do not leave it.

Now we go up to the main form again and press a right arrow button advancing to the next Horse ( main record).

The next horse is entered and we again tab off into the subform.

This time the cursor is where we left it at the end of Rider...

I would like the next subform, table entry, to have the cursor focus back over on Class without having to put it there manually.

regards
bob
 

VilaRestal

';drop database master;--
Local time
Today, 14:44
Joined
Jun 8, 2011
Messages
1,046
Ah I see, as datasheets or continuous forms the control with the focus is remembered in the subform. I think one way to fix it would be in the parent form's current event put code that sets the focus to the subform's first control and then sets the focus to the parent form's first control. So let's say the first control on the parent form is HorseName then it would be:

Code:
Private Sub Form_Current()
    Me.Child1.Form.Class.SetFocus
    Me.HorseName.SetFocus
End Sub

So when the user tabs to Child1 then the Class control will have the focus.
 

VilaRestal

';drop database master;--
Local time
Today, 14:44
Joined
Jun 8, 2011
Messages
1,046
Or an improved version that will work no matter what the controls are called and for any number of sbforms:

Code:
Private Sub Form_Current()
    SetFocusFirstControls Me.Form
End Sub

in the parent form's module and

Code:
Public Sub SetFocusFirstControls(ByRef frm As Form)
    On Error Resume Next
    Dim ctl As Control
    For Each ctl In frm.Detail.Controls
        With ctl
            If .ControlType = acSubform Then SetFocusFirstControls .Form
            If .TabIndex = 0 Then
                .SetFocus
                .SelStart = 0
                .SelLength = 0
            End If
        End With
    Next ctl
End Sub

in a standard module.

Reusable and uses recursion, which is always fun ;)
 

rks062148

New member
Local time
Today, 09:44
Joined
Oct 19, 2012
Messages
9
Hi again.

Well, I think we have it solved.

The end result is - - -

You have left the cursor in the third table data field in the subform -

When you then reach up onto the main form and tell the form to advance to the next record...the next horse entry.

But here on the Main Form on current state - I have entered the code..

Private Sub Form_Current()
Me.[tblClasses subform1].Form.Class.SetFocus
End Sub

So as soon as the Main form gained focus and went to the next record, the subform ( which is actually called tblClasses subform1, has its focus conditioned to be on Class.

Since this subform was built with a space in its name, I had to bracket it to declare the name properly.

Sure enough, I tabbed to the last control of the main form, and then tabbed off of it ....into the subform...and walla - - the cursor is sitting on Class!!

A minor point I know but when you are madly registering these horses, every keystroke saved and not having to remember small nuances = addes up.

Thanks again.

Bob
 

rks062148

New member
Local time
Today, 09:44
Joined
Oct 19, 2012
Messages
9
Hi,

You have been a big help and I thank you very much.

If you would like to consider this other matter - -then here it is as well.

In that main horse form again is the control Horse and Owner.

The format in the table representing these fields has format set to >.

So no matter what the typist is typing into the form fields they end up as all caps...for consistency.

Now the show is over and it is time to send a letter of thanks for partiipation to the horse owners. With a bit of mailmerge, the letter is a bit personalized, talking about their horse and so forth.

Here is the issue - -

The main form is typed in as brenda jones and shows as BRENDA JONES.

The horse name is typed as buttercup and shows as BUTTERCUP.

Again because the table format is >

Now over in the letter, it would be nice to address the owner as Brenda Jones and say Dear Brenda, only one table field holds her complete name.

And it would be nice to mentin the horse as Buttercup.

The report is using a query to collect the data for presentation.

I am sure some code is needed here involving Str and Len and so forth, to get the first name and raise the first letter to cap, same for the horse.

would this conditioning be put in as an expression in the query or some code somewhere and what would that code expression look like.

Keep in mind that the > seems to be useful for the way the FORM looks but however you typed your data, is exactly how it is listed in the main form's table.

Thanks again

Bob
 

VilaRestal

';drop database master;--
Local time
Today, 14:44
Joined
Jun 8, 2011
Messages
1,046
Check out StrConv(s, vbProperCase), will make all words first letter upper-case, other letters lower-case. You could either process the input in place or just format it for display. It has it's limitations when applied to full sentences (acronyms and hyphens and things aren't handled quite right). Surnames of the type O'Something or McSomething are the only problem for processing names.

You might want to look at this thread where we've been refining it's output for one purpose.

But anyway, StrConv(, vbProperCase) would be a natural starting point.
 

Users who are viewing this thread

Top Bottom