473,890 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.SetFocus is not selecting the whole field

Seth Schrock
2,965 Recognized Expert Specialist
I have a textbox that I use as a search field. To trigger the search, I use both an AfterUpdate event on the textbox and I have a button for those that are more mouse oriented.

Both the button's OnClick and the textbox's AfterUpdate events call the same private sub. In that subroutine I test the results to see if there was a match and if not, I do a .SetFocus back to the search textbox so that the user can retype a new value.

Problem: If the subroutine is called from the OnClick event of the textbox, then the cursor goes to the beginning of the field instead of selecting the whole field.

If the subroutine is called from the button's OnClick event, then the whole field is selected like I want.

I did check Access's setting for what to do when entering a field and it is set to select the whole field. I initially thought that it was because the control already had focus if I triggered the subroutine from the textbox's AfterUpdate event, so I tried having it set focus on the button and then going back to the textbox. There was no change for either method of triggering the subroutine so I took it back out.

Here is my code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub FindLoan()
  3. If Me.txtLoanNumberSearch & "" <> "" Then
  4.     Me.Recordset.FindFirst "LoanNumber = " & Me.txtLoanNumberSearch
  5.     If Me.Recordset.NoMatch Then
  6.         Me.imgWarning.Visible = True
  7.         Me.lblInvalidLoanNumber.Visible = True
  8.         DoCmd.GoToRecord , , acNewRec
  9.         Me.txtLoanNumberSearch.SetFocus
  10.     Else
  12.         Me.imgWarning.Visible = False
  13.         Me.lblInvalidLoanNumber.Visible = False
  14.         Me.txtLoanNumberSearch = ""
  15.     End If
  16. End If
  18. End Sub
Both events that call this subroutine, only call the subroutine and nothing else so there is no other code that is ran.

I'm out of ideas.
Dec 17 '12
31 11702
5,501 Recognized Expert Moderator Expert
- STOP between lines 7 and 8 of last posted code.
- Arrange the window so that you can see both the form and the vba in debug mode.
- {F8} thru the code code and watch the behavior of the form let's see what's happening in slow motion.
Dec 18 '12 #21
Seth Schrock
2,965 Recognized Expert Specialist
I have tried stepping through the code while watching the form (I have dual monitors), but I can never see the cursor. Even when the code is done running I still can't find the cursor. Just now I tried using the Stop like you suggested instead of using a break point, but I still can't see the cursor.
Dec 18 '12 #22
Seth Schrock
2,965 Recognized Expert Specialist
Well I decided to try my own suggestion. It didn't work. However I did find out something interesting. I now have FindLoan only being called from the command button's GotFocus event. If I just type in a number (while on a new record) and
1) press tab
2) press enter
3) click on the button
It does the behavior that we can't figure out. If I then click the button again, it does exactly what I want it to do. It almost seems like the control hasn't been saved and that is causing it to not work. However, I'm using the .Text property not the .Value property so I would think that that shouldn't matter, but it seems to. Is there code that could save the control so that the first time I run the code it thinks that it is the same as when I run it the second time?
Dec 18 '12 #23
32,584 Recognized Expert Moderator MVP
Maybe I can trick the system so that instead of having an AfterUpdate event on the textbox, I will just let the pressing of the enter key take me to the next control (the command button that does work) and have a GotFocus event that triggers the FindLoan subroutine. Do you think that would work or some variation of it?
Good thinking, but not a good plan. AfterUpdate() is there because there are so many possible ways of causing the control to update.

The active form will never display when its code is running. Certainly not in 2003 or before. Even in stopped mode that form can never be selected.
Dec 18 '12 #24
32,584 Recognized Expert Moderator MVP
Is there code that could save the control so that the first time I run the code it thinks that it is the same as when I run it the second time?
I only wish I knew what you meant Seth.

PS. We cross-posted so you may need to check my last post too if you haven't seen it already ;-)
Dec 18 '12 #25
Seth Schrock
2,965 Recognized Expert Specialist
Something is different between running the code the first time and the second time (as described in post #23). I was just wondering if we there was a way to make the code make the difference occur the first time it is ran so that whatever is in place to make it work the second time also is in place the first time in runs.
Dec 18 '12 #26
5,501 Recognized Expert Moderator Expert
Neopa: I watch active forms all of the time while stepping thru code, have since V97, hence why I suggested do so with Seth's form. Now I wasn't expecting dual monitors; however, that shouldn't make a difference.

Seth: Remove the code I suggested from the Afterupdate event.
In the "gotfocus" event of the control we're working with (in this example I've named it "combo3"
IN this example it's a combo-box; thus, the need for the "text" and not the value as the value might be from a bound hiden column:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Combo3_GotFocus()
  2. Dim z_int_length
  3. With Me.Combo3
  4.       z_int_length = Len(.Text)
  5.     .SelStart = 0
  6.     .SelLength = z_int_length
  7. End With
  8. End Sub
Dec 18 '12 #27
32,584 Recognized Expert Moderator MVP
Neopa: I watch active forms all of the time while stepping thru code, have since V97, hence why I suggested do so with Seth's form. Now I wasn't expecting dual monitors; however, that shouldn't make a difference.
Not dual monitors Z. My point is that active forms cannot be selected (brought to the front) when they are running code in break mode. That said of course, as long as it is visible already, I expect it remains visible. I'd be surprised (though I'd believe if you told me it does.) if you could see things like selection highlighting change as the code runs though.

I sort of see where you may be coming from, but I doubt the simple matter of repeating the code would be enough. Let's see what Z's come up with and see if that throws any more light on the matter.
Dec 19 '12 #28
Seth Schrock
2,965 Recognized Expert Specialist
Okay. This is weird. I'm on vacation so I emailed the database to myself to work on it at home. I currently have it so that my command button has the GotFocus event (I haven't taken that off since I tried it back in post #23). So FindLoan is only called from the command button's GotFocus event so that both hitting enter while in the textbox and clicking the button triggers the event. It works perfectly on my computer at home. At first I thought that it was because of adding Z's code in the textbox's GotFocus event, but I then commented that out and it still worked.

So... What could be different about my setup at work and my setup at home that makes this not work on my work PC?
Dec 19 '12 #29
32,584 Recognized Expert Moderator MVP
So... What could be different about my setup at work and my setup at home that makes this not work on my work PC?
That's a good question, but not one you should consider asking others to work out for you. It would be interesting if you could tell us, bearing in mind that only you have access to either machine. Trying to work it out from the limited information available to us so that you don't need to look at what is in front of you is not a good balance of effort, I would suggest.
Dec 19 '12 #30

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

Similar topics

by: Pete | last post by:
Is there any way to change the default search to "Any Part Of Field" instead of whole field? The first thing I ever do when searching for something in a field is change the default setting from "Whole Field" to "Any part of field" because that way I don't have to key in the entire word I'm searching for. It is especially frustrating having to explain this to all of the people using my database. Any help would be appreciated. Thanks.
by: tshad | last post by:
Is there a way to go into the edit mode by just clicking on a field in the row of the DataGrid you want to edit? I would like to "not" show the Edit button when I show the grid. I would like to show the text boxes or dropdownlist by just selecting the field. Is this possible? Thanks,
by: SDL | last post by:
The code below says error 2108 must save field before setfocus? All I want to do is check out the input field and if in error set the cursor back on it after the MsgBox displays. In this case I read the file and didn't find it. If INVOICE_NBR1 = 0 Then MsgBox "INVOICE NUMBER is not on file. Re-enter or enter NAME or PHONE.",vbOKOnly Me.INV_NBR.SetFocus
by: greg | last post by:
I'm doing a join on a table with a cross tab query and I only want to select the last field in the crosstab query but I get a syntax error, even if I reference it with the correct dynamically generated field name (via vba). Is there any way that I can select just one of the dynamically generated fields from the crosstab query? Here is an abbreviated example: SELECT a.ref_number, a.Timestamp, a.Segment, b. FROM...
by: John | last post by:
I have a field on my form of which I'm setting the value programmatically. I don't want the users to be able to setfocus to the field. I've set the tabstop property to false. However, the user is still able to click in the field and then it gets focus. How can I prevent the user from selecting the field? thanks, john
by: Katherine | last post by:
I'm trying to filter the records on the mainform (MailingList) of my database using a field contained in a subform (Donations). I was basing my code off Allen Browne's Access Tips page (here: http://allenbrowne.com/ser-28.html) since it has worked so well in the past. However, now I'm trying to filter by date instead of by a string or a number, and I'm getting nowhere. My goal is to have users type a date into the unbound combo box and...
by: MattFitzgerald | last post by:
I have a main menu ("Frm_Startup") which contains a drop down box for selecting a field "LE ID" from my customer table where the "LE ID" is the primary key and there is another field with the customers "Legal Entity Desc" I have a button "Add new Order" which opens a new form where I can enter new orders. In the form for new orders the Default value for the LE ID is the value selected in the main menu . I wanted to pick up the customers...
by: hr833 | last post by:
I hoping to make things easier for my cilents. Below i have the fields for a table Table one Month Customer Transaction Amt I wish to prompt the user for the month in which the records belong to. Eg. In the Month of September i enter 100 records.
by: cloh | last post by:
Hey all, I have a table in Access with 13 rows and 2 columns. The first column has numbers 1 to 13 in ascending order, and the second column has three-letter codes that correspond to each number, like so: ID_number Code 1 ARS 2 CAD In VB, I have a loop that generates a bunch of Excel files, so my loop goes for i = 1 to 13. I would like to be able to name the worksheet in the...
by: jvborg | last post by:
I am experiencing a problem when trying to move the setfocus to text field txtSearchDump, if i do not place the text into this field the query which i am initially trying to run does not execute. Any advice would be very helpful Thanks Joe Select Case Me.optSearchCriteria.Value Case 1 strSQL = strSelect & strFrom & strTitle Case 2 strSQL = strSelect & strFrom & strDescription
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,...
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...
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,...
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...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.