By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

VBA opening a form, want to set focus, feels like everything have been tried out

P: 48
Hi All,

Thanks for reading this :)

I have a form A that consists of a main form A and a sub form A.

In sub form A, I have a field which I can double click. Then, Main form B opens up with a search field. Sub form B has a query as recordset. The query use the search field value as a filter, and the filter is updated every time text is entered into the search field.

What I want to happen is to set focus on the search field once form b is opened. I have tried this code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub VismaID_DblClick(Cancel As Integer)
  2. DoCmd.OpenForm "Form B", acNormal
  3. Forms![Form B].[Search].SetFocus
  4. End Sub 
I have also tried a combination of

1.
Expand|Select|Wrap|Line Numbers
  1. Private Sub VismaID_DblClick(Cancel As Integer)
  2. DoCmd.OpenForm "Form B", acNormal
  3. End Sub 
and
2. a OnEvent in Form B (tried all of them)

I have used F8 to go stepwise throug the whole code, and what happens then is that the macro first opens the form B, runs through the onevent and then goes back to Sub Dblclick which then ends.

However, I am unable to set focus on the search field either way.

I have a feeling it got to do something with the subform B... somehow. Because, if I hit ALT+Tab twice, the search field gets focus.

Any suggestions on where to troubleshoot or solve it?

Thanks in advance.

Martin
Jul 13 '10 #1

✓ answered by NeoPa

You may find that there is more of a problem due to the order that your FormB controls appear in. Nevertheless, this should resolve the issue :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub VismaID_DblClick(Cancel As Integer)
  5.     Call DoCmd.OpenForm("[Søke etter vismaråvarer]", acNormal)
  6.     Call DoCmd.SelectObject(acForm, "[Søke etter vismaråvarer]", False)
  7.     Call Forms("[Søke etter vismaråvarer]").Søk.SetFocus
  8.     Cancel = True
  9. End Sub
I didn't have a form with subforms to play with. If you find further problems then attach a copy of the database in 2003 or earlier version and I'll look further into it for you.

Share this Question
Share on Google+
15 Replies


Expert 100+
P: 1,221
If I remember correctly, you have to set the focus to form A first, then set the focus to an object on Form A.

Jim
Jul 13 '10 #2

P: 48
@jimatqsi
Hey Jim,

Thanks for having a look on this.

My goal is to set focus on a object on Form B.

Do I understand you correctly if I say you suggest

1. Set focus to Form B
2. Set focus on an object on Form B

I tried this code
Expand|Select|Wrap|Line Numbers
  1. Private Sub VismaID_DblClick(Cancel As Integer)
  2. DoCmd.OpenForm "Søke etter vismaråvarer", acNormal
  3. Forms![søke etter vismaråvarer].SetFocus
  4. Forms![søke etter vismaråvarer]!Søk.SetFocus
  5. End Sub
Søk = search object
[Søke etter vismaråvarer] = Form B

Well, I had tried that as well.... not working :)
Jul 13 '10 #3

Expert 100+
P: 1,221
It looks to me like you got it right, except ... you are opening Form B and then setting the focus on Form B (where it already is) and then setting focus to an object on Form B.

Shouldn't lines 3 and 4 refer to a different form, Form A?

Jim
Jul 13 '10 #4

P: 48
@jimatqsi
Well, the thing is that it looks to me that Form B does not have the focus, since the code in Form A continue to run after Form B has been opened...

if not, this code should have worked ??
Expand|Select|Wrap|Line Numbers
  1. Private Sub VismaID_DblClick(Cancel As Integer) 
  2. DoCmd.OpenForm "Form B", acNormal 
  3. Forms![Form B].[Search].SetFocus 
  4. End Sub  
Form A is not of interest once Form B has been opened. The Private Sub VismaID_DblClick is run from Form A.
Jul 13 '10 #5

Expert 100+
P: 107
Opening a form with the double-click event has the unfortunate effect of always returning focus to the calling form at the end of the event. I'd like a solution to that problem. A side-effect is that the opened form appears on the screen, but keystrokes go to the calling form.

A workaround is using the click event instead (or some other event). If you open the form from a click event then the focus automatically goes to the form you are opening.
Jul 13 '10 #6

P: 48
@Steven Kogan
Hehe, Nice to know. Makes me feel better about not being able to solve the thing.

Hmm, a workaround would be nice, cause it feels user-safer to have doubleclick event...

Anyone in the forum with a solution to it, or is the only way to use a different event like Steven suggests?
Jul 13 '10 #7

Expert 100+
P: 107
I'm interested in hearing other solutions, but here is one idea.

This is an odd workaround, untested: The double-click event can have code at the end to set the form timer to something like a quarter second. The double-click event would end, then the form timer event would fire. The timer event would set the form timer to no longer fire (set the timer to zero), and then it would open the other form... I'm not sure, but I think this would leave the focus on the other form.

There are several drawbacks to this approach, one being that between the double-click event and the timer event the user might perform another action. Try using a very low value for the form timer.

I hope someone has a more proper solution to using the double-click event to open another form and set the focus to that form.
Jul 13 '10 #8

P: 48
Good idea, I will try out in some few days. Will come back to you with the results.

In the meantime, if other people have suggestions as well, they are very welcome to share them :)

Martin
Jul 14 '10 #9

NeoPa
Expert Mod 15k+
P: 31,433
You may find that there is more of a problem due to the order that your FormB controls appear in. Nevertheless, this should resolve the issue :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub VismaID_DblClick(Cancel As Integer)
  5.     Call DoCmd.OpenForm("[Søke etter vismaråvarer]", acNormal)
  6.     Call DoCmd.SelectObject(acForm, "[Søke etter vismaråvarer]", False)
  7.     Call Forms("[Søke etter vismaråvarer]").Søk.SetFocus
  8.     Cancel = True
  9. End Sub
I didn't have a form with subforms to play with. If you find further problems then attach a copy of the database in 2003 or earlier version and I'll look further into it for you.
Jul 14 '10 #10

Expert 100+
P: 107
Recreating the error in a new database was worthwhile.

The problem seems to occur with the double-click event on a textbox control.

With a label control focus goes to the other form properly. I haven't tested with other control types.

I've created a new database with two forms and put in code to open the second form using the double-click event. With a label control double-click event the second form automatically gets the focus. I'm also able to set focus on the subform succesfully.

With a textbox control the focus stays on the calling form.

Attached is the zipped Access 2003 test database. The textbox control to double-click is in Form1's subform.

It would be great to get the double-click event of the textbox control to work such that the opened form gets focus.
Attached Files
File Type: zip test.zip (34.0 KB, 155 views)
Jul 14 '10 #11

NeoPa
Expert Mod 15k+
P: 31,433
I'll have a look at it for you Steven.
Jul 14 '10 #12

Expert 100+
P: 107
Ah! I've got it figured out.

Add the line:
Cancel=True

That fixes it.

I think the focus returns to the calling form to perform the double-click event of selecting the word that was double-clicked.
Jul 14 '10 #13

NeoPa
Expert Mod 15k+
P: 31,433
That sounds right. I really should have included that in my posted code anyway. Good spot :)
Jul 14 '10 #14

P: 48
Thanks again NeoPa, and Steven too for the final touchline :)

Works great!

About the order, the search field is in the form header (the only field as well) while the subform is in the "detail area".

Will save that string for later use. Couldnt ever never found out that by myself. Really!

Thanks' :)

Martin
Jul 15 '10 #15

NeoPa
Expert Mod 15k+
P: 31,433
Martin Lang: About the order, the search field is in the form header (the only field as well) while the subform is in the "detail area".
I'm pleased that worked for you. Sometimes switching between controls in different sections can be tricky. Especially using TAB. In such cases you first have to give focus to the new section, then go from there. It seems this wasn't necessary in this case.
Jul 15 '10 #16

Post your reply

Sign in to post your reply or Sign up for a free account.