Results 1 to 8 of 8
  1. #1
    zdjbel is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Apr 2012
    Posts
    42

    Delete tabs and spaces in combo box after pasting text

    Hello,

    I use a combo box on a form to take the user to the desired record. Often they paste text onto the combo box to search for the particular record but they also sometimes past blank spaces and/or tabs. I want to clean the pasted text but the code erases the pasted text and the search doesn't function. The search works normally when I remove the replace and trim limes of code. What am I doing wrong here?

    Private Sub cmbGoToPr_AfterUpdate()
    On Error GoTo cmbGoToPr_AfterUpdate_Err


    'Remove tabs and spaces


    cmbGoToPr = Replace(cmbGoToPr, vbTab, " ")
    cmbGoToPr = Trim(cmbGoToPr)

    'Now search for record
    DoCmd.SearchForRecord , "", acFirst, "[PrNumber] = " & "'" & Screen.ActiveControl & "'"


    cmbGoToPr_AfterUpdate_Exit:
    Exit Sub


    cmbGoToPr_AfterUpdate_Err:
    MsgBox Error$
    Resume cmbGoToPr_AfterUpdate_Exit


    End Sub


    Many thanks!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    It looks like your "'" & Screen.ActiveControl & "'" is being treated as a string literal. I suggest removing all the quotes.

    Try the following to actually use the ActiveControl
    Code:
     DoCmd.SearchForRecord , "", acFirst, "[PrNumber] = " &   Screen.ActiveControl
    Last edited by orange; 05-08-2025 at 05:25 AM. Reason: spelling

  3. #3
    zdjbel is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Apr 2012
    Posts
    42
    Thank you for your response but, no, that didn't solve the issue. What I'm looking to do is e.g. paste "tab space space PR1234567", remove tab space space and then search for PR1234567.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,557
    Use the Replace function then.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,935
    Suggest use a string variable , assign active control to it then do your trim/replace and filler on that variable

    also use the before update event and reference the .text property since if the pasted text does not meet the rules of the combo it will fail

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    zdjbel,

    I mocked up a form with some of my test data. Table Articles, and a sample form. Txt13 is the textbox for the search active control. I have attached zip with a gif showing the table, the form and copying and pasting a value into search text box. Your syntax works in my mockup. I tried removing the quotes as I suggested earlier and it did not work-- but your syntax does.

    Good luck.

    Update: I deleted the original message since it didn't attach the zip file. Trying again.


    Also, I see CJ has responded he has lots of experience and is an active developer.
    Attached Files Attached Files
    Last edited by orange; 05-08-2025 at 09:39 AM. Reason: updated

  7. #7
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    If you want to remove both tabs and spaces, why would you replace a tab with a space? Consider posting text examples of what is being pasted. Use code tags (hash button on posting toolbar - #) for more than a couple of lines of code to maintain indentation. Also, I believe you would have to use code tags to maintain 2 or more spaces, otherwise the forum will remove extra spaces between words. Not sure but I think code tags will prevent that but will test right now
    Code:
    three   spaces
    EDIT - Yup. Works.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    maybe when you copy, you copy some Non-printable characters.
    you delete the non-printable characters and leave just the printable ones:
    Code:
    Private Sub cmbGoToPr_AfterUpdate()
    On Error GoTo cmbGoToPr_AfterUpdate_Err
    
    
    
    
    'Remove tabs and spaces
    cmbGoToPr = RemoveNonPrintable(cmbGoToPr)
    cmbGoToPr = Trim$(cmbGoToPr)
    
    
    
    
    'Now search for record
    DoCmd.SearchForRecord , "", acFirst, "[PrNumber] = " & "'" & Screen.ActiveControl & "'"
    
    
    
    
    cmbGoToPr_AfterUpdate_Exit:
    Exit Sub
    
    
    
    
    cmbGoToPr_AfterUpdate_Err:
    MsgBox Error$
    Resume cmbGoToPr_AfterUpdate_Exit
    
    
    End Sub
    
    
    Function RemoveNonPrintable(strInput As String) As String
        Dim regEx As Object
        Set regEx = CreateObject("VBScript.RegExp")
        
        With regEx
            .Global = True
            .IgnoreCase = True
            ' Match all characters except printable ASCII (32–126)
            .Pattern = "[^\x20-\x7E]"
        End With
        
        RemoveNonPrintable = regEx.Replace(strInput, "")
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 16
    Last Post: 09-03-2024, 10:39 AM
  2. Tabs within Tabs within Tabs
    By Ellpee in forum Forms
    Replies: 12
    Last Post: 09-16-2020, 09:51 AM
  3. Replies: 2
    Last Post: 04-26-2020, 02:01 AM
  4. Replies: 2
    Last Post: 10-23-2014, 08:19 PM
  5. Tabs on Tabs?
    By NateHaze in forum Forms
    Replies: 4
    Last Post: 06-01-2011, 11:32 AM

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