473,623 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

48 New Member
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
15 11838
jimatqsi
1,276 Recognized Expert Top Contributor
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
Martin Lang
48 New Member
@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,276 Recognized Expert Top Contributor
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
Martin Lang
48 New Member
@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_DblClic k is run from Form A.
Jul 13 '10 #5
Steven Kogan
107 Recognized Expert New Member
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
Martin Lang
48 New Member
@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 Recognized Expert New Member
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
Martin Lang
48 New Member
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,566 Recognized Expert Moderator MVP
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

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

Similar topics

2
4012
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 focus. After this happens, my MDI children no longer are able to obtain keyboard focus and therefore no longer receive keyboard commands. Needless to say, that's a bad thing. :)
1
1668
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 !! Sincerely yous, Paul
3
2342
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 focus and Form.Leave does not fire when showdialog is called on another form. Anyone have any idea how to capture the form losing focus? Chris
4
2203
by: Rob | last post by:
Is there a way to keep a form on top of everything - even other apps ?
1
1654
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 datagrid got focus, the scanner input will be discarded. I cannot find any event to handle the input. Thanks,
1
4169
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 updating the progress bar If the form loses focus(i.e selecting any other application) means progress bar is not updating The coding is below
0
1046
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 focus is sent back to the Client form. In the client form I have a combo filled with Client objects. THe problem is that the search form calls a the following function: Public Sub LoadPatient(ByRef lPatientId As String) Dim s As Integer s =...
3
2656
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 button), the form itself loses focus. How can I get it back? I have tried this.Focus() from w/in the form in a timer with no luck as well as several other things. Thanks!
1
1316
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 client application are connected using a named pipe or .NET remoting. When an event occurs on the hardware, the controlling process also sends the events to the clients which are connected. Now the clients could be numerous, but I only want the...
0
8224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8163
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8667
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8610
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8469
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6104
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
1775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1471
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.