473,883 Members | 1,763 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()
  2.  
  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
  11.  
  12.         Me.imgWarning.Visible = False
  13.         Me.lblInvalidLoanNumber.Visible = False
  14.         Me.txtLoanNumberSearch = ""
  15.     End If
  16. End If
  17.  
  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 11701
NeoPa
32,584 Recognized Expert Moderator MVP
Seth:
Should I just take away the button's OnClick event and let the AfterUpdate of the textbox do its work?
Essentially yes. I would do away with the Command Button completely, but if you must have it then it really needs no code associated.

Seth:
basically, I have just replaced my line 9 with your With statement.
Please try my slightly amended With code too, separately. I doubt it will make much difference, as I was not allowing for the fact that the code is only ever run when the update has completed, but it would be interesting to know anyway.
Dec 18 '12 #11
Seth Schrock
2,965 Recognized Expert Specialist
I just tried the amended With code and it did the same thing.

You mentioned in post #4 that the other option would be to select another control and then move back to the textbox. I tried this and that doesn't work either in that it still goes to the beginning of the textbox and doesn't select the whole field. Is this the sign of a design problem on my part?

Also for clarification, in post #7 you said that the .Value of the textbox hadn't been changed so the .Text would need to be used. I thought that the value had already changed when the AfterUpdate event occurred. My understanding was that the BeforeUpdate event triggered, then the change happened, and then the AfterUpdate event was triggered. Is this not correct?

@Z Here is my code that has NeoPa's tweak.
Expand|Select|Wrap|Line Numbers
  1. Private Sub FindLoan()
  2. Dim intLength As Integer
  3.  
  4. If Me.txtLoanNumberSearch & "" <> "" Then
  5.     Me.Recordset.FindFirst "LoanNumber = " & Me.txtLoanNumberSearch
  6.     If Me.Recordset.NoMatch Then
  7.         Me.imgWarning.Visible = True
  8.         Me.lblInvalidLoanNumber.Visible = True
  9.         DoCmd.GoToRecord , , acNewRec
  10.         With Me.txtLoanNumberSearch
  11.             .SetFocus
  12.             intLength = Len(.Text)
  13.             .SelStart = 0
  14.             .SelLength = intLength
  15.         End With
  16.     Else
  17.  
  18.         Me.imgWarning.Visible = False
  19.         Me.lblInvalidLoanNumber.Visible = False
  20.         Me.txtLoanNumberSearch = ""
  21.     End If
  22. End If
  23.  
  24. End Sub
Dec 18 '12 #12
zmbd
5,501 Recognized Expert Moderator Expert
Let's see what is actually in the txtLoanNumberSe arch at run time:
insert between lines 11 and 12
Expand|Select|Wrap|Line Numbers
  1. debug.print "text = " & .text
  2. debug.print "value = " & .value
<ctrl><G> to get the debug window up and then run your form both ways; please report back what is printed in each case for both properties.
Dec 18 '12 #13
Seth Schrock
2,965 Recognized Expert Specialist
Both values were the same. While the form was sitting on a new record, I typed in 987654321 and I got:
text = 987654321
Value = 987654321

I then typed in 123456789 and I got:
text = 123456789
Value = 123456789

So the value does get changed prior to the AfterUpdate.
Dec 18 '12 #14
NeoPa
32,584 Recognized Expert Moderator MVP
Seth:
I just tried the amended With code and it did the same thing.
Thank you. I was starting to suspect that I had been mistaken earlier by the time we discussed not needing the code behind the Command Button. It was a thought, but clearly flawed for exactly the reason you included in your own reasoning. I would still avoid the use of the extra variable (intLength), but I'm sure that makes no practical difference.

What I'm going to suggest now is a variation on Z's request. Please use the following With block in place of your current one and post back the results. This will hopefully throw some light onto what the code thinks it's doing at the time :
Expand|Select|Wrap|Line Numbers
  1.         With Me.txtLoanNumberSearch
  2.             Call .SetFocus
  3. Debug.Print .SelStart, .SelLength, Len(.Text), .Text,
  4.             .SelStart = 0
  5.             .SelLength = Len(.Text)
  6. Debug.Print .SelStart, .SelLength;
  7.         End With
If, for any reason, what you've typed into the control is not included in the results, then please include that in your post too.

I expect you'll have guessed by now that the behaviour you describe is unexpected, so we're interested in helping you to determine why it is.
Dec 18 '12 #15
Seth Schrock
2,965 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1.  0             9             9            123456789      0             9 
  2.  0             9             9            987654321      0             9 
  3.  0             5             5            99999          0             5 
This is with form sitting on a new record.

Each time column 4 was what I typed into the textbox. And this does work if I use the command button (which I haven't removed yet).
Dec 18 '12 #16
NeoPa
32,584 Recognized Expert Moderator MVP
This indicates that your code is working in all situations. It implies that something is happening after the code completes that resets the selection. Can you confirm that when the full contents are not selected the cursor is nevertheless found within that control?

Bear in mind that, the AfterUpdate() procedure is triggered when you leave a control, after the update has occurred, but before the change of selection. This implies that a change of selection is still pending and should occur after your code has completed execution. This would naturally cancel out any selection made within the code.
Dec 18 '12 #17
Seth Schrock
2,965 Recognized Expert Specialist
You are correct that the cursor is still within the control that I'm wanting. It gets positioned at the beginning of the control.

I mapped out all the events that trigger and when. Because of the record moves the form's OnCurrent event triggers. However, it triggers before the .SetFocus occurs and does not occur afterwards. In fact no other event/function/subroutine occurs after the .SetFocus. Here are then the subroutines that I can see that could affect the change of selection:

Expand|Select|Wrap|Line Numbers
  1. Private Sub FindLoan()
  2.  
  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.         With Me.txtLoanNumberSearch
  10.             Call .SetFocus
  11.             Debug.Print .SelStart, .SelLength, Len(.Text), .Text,
  12.             .SelStart = 0
  13.             .SelLength = Len(.Text)
  14.             Debug.Print .SelStart, .SelLength:
  15.         End With
  16.     Else
  17.  
  18.         Me.imgWarning.Visible = False
  19.         Me.lblInvalidLoanNumber.Visible = False
  20.         Me.txtLoanNumberSearch = ""
  21.     End If
  22. End If
  23.  
  24. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtLoanNumberSearch_AfterUpdate()
  2. FindLoan
  3.  
  4. End Sub
No code has been edited out for clarity or any other reason. That is the entire code. Line 8 of FindLoan runs, the form's OnCurrent event triggers, it comes back to line 9. The only lines of code that run after setting the focus, start, and length are the End With, End If, End If, End Sub, End Sub. If there is anything else you need, just let me know.
Dec 18 '12 #18
NeoPa
32,584 Recognized Expert Moderator MVP
What I'm thinking of is not code Seth, but the action of whatever you hit to cause the focus to move. Even though that was done before all the code runs, it should only take effect afterwards. That said, it would tend to leave the cursor elsewhere than in that control, and would be consistent across new and existing records. That's as far as I've got I'm afraid :-(
Dec 18 '12 #19
Seth Schrock
2,965 Recognized Expert Specialist
Hmmm... Well I normally have been typing in the number and pressing Enter. Your comments prompted me to try other methods: Tab and clicking in another field. Both resulted in the same thing. I have tried adding a line between lines 8 & 9 that set the focus on another control. I did the same thing (move focus to another control and back again) in the AfterUpdate event of the textbox and that did the same thing still. Obviously you are correct in saying that it must be what causes the event to trigger that is throwing it off.

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?

This would take care of the problem with clicking the button causing the FindLoan sub to run twice if I remove the OnClick event. Both advancing from the textbox to the command button and clicking on the button would cause the GotFocus event to run.
Dec 18 '12 #20

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

Similar topics

11
9186
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.
1
1267
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,
1
8421
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
0
1317
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...
2
1317
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
7
6765
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...
3
1254
MattFitzgerald
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...
3
1264
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.
5
1441
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...
1
7323
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
0
9944
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...
1
10860
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
9583
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7975
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...
0
7134
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();...
0
5804
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...
1
4620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3239
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.