Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296

    How to get text field to stay focused and highlight text after hitting enter?

    I'm trying to use a barcode scanner to be able to scan a barcode and look up the respective ticket in a table. I am starting by getting the form to split up the barcode as the barcodes are made up as such: 243455-01-050


    The first 6 are the order number.
    The middle 2 are the ticket number as there can be multiple tickets per an order.
    The last 3 are the step numbers as there are multiple steps per a ticket.

    So I am using the following code on the AfterUpdate:
    Code:
    Option Compare Database
    
    'Private Sub CboName_Change()
    'Me.DhrScanTxt.SetFocus
    'End Sub
    
    
    Private Sub DhrScanTxt_AfterUpdate()
    
    
    Dim Status As String
    Status = Me.DhrScanTxt
    
    
    Me.Text26 = Status
    
    
    Dim SoNum As Long
    SoNum = Left(Status, 6)
    Me.SoNumTxt = SoNum
    
    
    Dim DhrNum As Long
    DhrNum = Right(Left(Status, 9), 2)
    Me.DhrNumTxt = DhrNum
    
    
    Dim StepNum As Long
    StepNum = Right(Status, 3)
    Me.StepNumTxt = StepNum
    
    
    
    
    
    
    
    
    
    
    
    
    End Sub
    Problem is it doesn't seem to update the other text boxes.
    I tried "on change" but it then only takes the 2 and opens the vba and tries to type it in there and opens a windows explorer searching for a file?
    I closed out the explorer and hover over the highlighted code bit which is [Status = Me.DhrScanTxt] and it says [Me.DhrScanTxt] is null? which doesnt make sense since I just scanned it in and it shows the 2 in the text box.

    The way the barcode scanners are made it also does like 5-7 carriage returns after entering the text so I tried the "on enter" property and it works but I since it hits enter several times I end up on one of the other fields. (I don't want to remove those other fields from tab order because It needs to be useable with just a keyboard)

    I tried adding a [Me.DhrScanTxt.SetFocus] to the end and it doesn't work. it still goes to the next field since it does several carriage returns.
    When I get there I want it to highlight the text so that the user can scan several steps quickly. I haven't figured out how to do that yet so if you know that would be a nice extra bit.

    Thanks for taking the time to help!

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    The scanner should only send one carriage return after a successful scan. Check it's configuration.
    The controls after update event is the correct one to to set the other fields values.

    I've used quite a few scanners over the years and not experienced multiple CR's (Enters). Are you definitely reading the barcode correctly?
    What happens if you simply scan the barcode into notepad or something similar ?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Click image for larger version. 

Name:	Untitled.png 
Views:	18 
Size:	2.7 KB 
ID:	48903Click image for larger version. 

Name:	Untitled2.png 
Views:	17 
Size:	4.7 KB 
ID:	48904
    It goes down 7 lines and has that weird blank character there. Problem is I think its the barcode not the scanner. I tried a different computer with a different(but same model) scanner and It does the same thing.
    Unfortunately I have to work around this if that's the case because there's tens of thousands of these barcodes already.

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    You can test if it's the scanner by simply scanning some other barcode on a book or tin of beans. See what you get.
    If the carriage returns are always present in the barcode, you can deal with them by changing the default behaviour of the text box enter key and/or using the change event to monitor the input.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Minty View Post
    You can test if it's the scanner by simply scanning some other barcode on a book or tin of beans. See what you get.
    If the carriage returns are always present in the barcode, you can deal with them by changing the default behaviour of the text box enter key and/or using the change event to monitor the input.
    Apologies for the late reply I don't have access to this account on the weekends.
    I tried scanning a different barcode and it does not do the carriage returns. So it is the barcodes.
    How would I go about dealing with them?

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    If you change the default behaviour of the enter key in the text box, you will be able to strip them out of the extraneous text.
    The difficulty will be to "know" when the barcode scanner has completed it's task. and fire the VBA to do the rest.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Minty View Post
    If you change the default behaviour of the enter key in the text box, you will be able to strip them out of the extraneous text.
    The difficulty will be to "know" when the barcode scanner has completed it's task. and fire the VBA to do the rest.
    Yea I changed it to do a new line on enter and uh it doesn't seem to update anymore. Its kinda of glitchy, like it only seems to update when I click another field or 2.
    Edit: It also says it creates a new line in the field which could be a problem.

  8. #8
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Yes that the only way to circumvent it using the CR's to move to the next field.

    I guess you could just get it to move to another control and accept the CR's in that box, count the number of characters, then fire the update?
    e.g.

    • The first <Enter> in theory moves from your barcode control to a small virtually hidden control,
    • Accepts the remaining 6 <Enter> key strokes as new lines in the hidden control, counting them in the on change event.
    • fire the update to the other controls,
    • move to the next textbox you really want to be on.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  9. #9
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Minty View Post
    Yes that the only way to circumvent it using the CR's to move to the next field.

    I guess you could just get it to move to another control and accept the CR's in that box, count the number of characters, then fire the update?
    e.g.

    • The first <Enter> in theory moves from your barcode control to a small virtually hidden control,
    • Accepts the remaining 6 <Enter> key strokes as new lines in the hidden control, counting them in the on change event.
    • fire the update to the other controls,
    • move to the next textbox you really want to be on.
    How would I go about counting how many times a function ran?
    Something like this?
    Code:
    Private Sub TxtEnterbox_Change()
    Dim Count As Integer
    If (Count >= 6) Then
            Me.DhrScanTxt.SetFocus
            Count = 0
    End If
    Count = Count + 1
    End Sub

  10. #10
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    I also tried it on enter and I cant get it to seem to count.

  11. #11
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    You'll have to declare the count variable outside the change event, otherwise it will always revert to 0 every time something changes.

    Put the

    Code:
    DIM  iCount as integer
    before the other form procedures (After Option Explicit at the top of the module) and remember to reset it after it's successful.
    (Use iCount as Count is a reserved word and a function and will bite you later)
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  12. #12
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Minty View Post
    You'll have to declare the count variable outside the change event, otherwise it will always revert to 0 every time something changes.

    Put the

    Code:
    DIM  iCount as integer
    before the other form procedures (After Option Explicit at the top of the module) and remember to reset it after it's successful.
    (Use iCount as Count is a reserved word and a function and will bite you later)
    Like this?
    Code:
    End Sub
    
    Dim iCount As Integer
    Private Sub TxtEnterbox_Enter()
    If (iCount >= 4) Then
            Me.DhrScanTxt.SetFocus
            iCount = 0
    End If
    iCount = iCount + 1
    End Sub
    In between the subs?
    I am not familiar with "Option Explicit". Upon googling I see that its a function but I did not include it?

  13. #13
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    I didn't have Option explicit in this. I added it and put it below that at the top of the code.
    Code:
    Option Compare Database
    Option Explicit
    Dim iCount As Integer
    
    
    Private Sub TxtEnterbox_Enter()
    If (iCount >= 6) Then
            Me.DhrScanTxt.SetFocus
            iCount = 0
    End If
    iCount = iCount + 1
    End Sub

  14. #14
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    At the top of all modules underneath

    Code:
    Option Compare Database
    Option Explicit
    
    Dim iCount as Integer
    You should add Option Explicit to all your code modules.
    It forces you to declare variables, and can massively save you time spotting typo's
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  15. #15
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Sorry I missed your latest post.

    That might do the trick. Add a debug.print to see if it's working

    Code:
    Private Sub TxtEnterbox_Enter()
    If (iCount >= 6) Then
            Me.DhrScanTxt.SetFocus
            iCount = 0
    End If
    Debug.Print iCount
    iCount = iCount + 1
    End Sub
    Edit - you'll need to use the change event possibly?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Text boxes filled with black when focused
    By milton837 in forum Forms
    Replies: 7
    Last Post: 09-08-2019, 02:36 PM
  2. How to highlight all text when tabbing to a field
    By Allen Sundholm in forum Forms
    Replies: 3
    Last Post: 07-27-2019, 07:56 PM
  3. Replies: 15
    Last Post: 02-17-2019, 06:19 PM
  4. Replies: 5
    Last Post: 01-24-2018, 10:45 AM
  5. Highlight text in memo field
    By silverspr in forum Programming
    Replies: 2
    Last Post: 03-27-2011, 04:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums