473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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.

15 11802
jimatqsi
1,271 Expert 1GB
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
@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
jimatqsi
1,271 Expert 1GB
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
@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
Steven Kogan
107 Expert 100+
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
@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
Steven Kogan
107 Expert 100+
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
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
32,556 Expert Mod 16PB
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
Steven Kogan
107 Expert 100+
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, 254 views)
Jul 14 '10 #11
NeoPa
32,556 Expert Mod 16PB
I'll have a look at it for you Steven.
Jul 14 '10 #12
Steven Kogan
107 Expert 100+
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
32,556 Expert Mod 16PB
That sounds right. I really should have included that in my posted code anyway. Good spot :)
Jul 14 '10 #14
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
32,556 Expert Mod 16PB
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

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

Similar topics

2
by: Stephan Rose | last post by:
Ok here's my situation... Got an MDI application with a panel to my right that has a tree view for the project stuff, etc. Now if I select an item in the tree view, the tree view gains input...
1
by: Paul Tsai | last post by:
Dear All, I used csharp web form to develop my company web application, and my question is how shuld I do if I want to focus on my textbox(ex. like Java Script form.txtID.focus()). Thanks !!...
3
by: Chris | last post by:
I have a form that calls ShowDialog on a number of different forms at different times. I want to be able to know anytime the form loses focus. But Form.Focused isn't ever true once a control has...
4
by: Rob | last post by:
Is there a way to keep a form on top of everything - even other apps ?
1
by: Tedmond | last post by:
Dear all, How to prevent a datagrid in Windows form getting focus? Even user click on it. I use barcode scanner as input and display the result in a datagrid. My problem is when the...
1
by: Manikandan | last post by:
Hi, I have a form with progress bar. I'm querying sql server with large number of records(around 1 lakh) for export The records are written to a text file I'm showing the process running by...
0
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hi Everyone, I have an issue thats bugging me some. I have a search form that I created to search for clients in the database from the Client form. User selects the client from a list and the...
3
by: =?Utf-8?B?SmVzc2U=?= | last post by:
I have searched and searched, with no luck. I have a Form that has several event listeners in it (Keypresses, speech recognition, afew others). When I click a component in the form (such as a...
1
by: dotnutshell | last post by:
I need to work out wether my windows form has focus of the mouse and keyboard. My project is the following: 2 or more applications. 1 application is a controller talking to some hardware The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.