Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    zdjbel is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Apr 2012
    Posts
    42

    Deleting blank spaces after pasting data into a form text field

    Hello,



    I often have to copy/paste data into from an outside source to a text field on my form. When I paste the data, blank spaces at the beginning and/or end of the text are pasted as well. How can I get rid of them automatically after pasting the data?

    Thank you.

  2. #2
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    Code:
    Dim MyString, TrimStringMyString = "  <-Trim->  "' Initialize string.TrimString = LTrim(MyString)    ' TrimString = "<-Trim->  ".TrimString = RTrim(MyString)    ' TrimString = "  <-Trim->".TrimString = LTrim(RTrim(MyString))    ' TrimString = "<-Trim->".' Using the Trim function alone achieves the same result.TrimString = Trim(MyString)    ' TrimString = "<-Trim->".
    is this what u look ing for?
    mystring fill from paste
    ur field is in place of trimstring
    trim do both sides so thats ur choice i guess

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,568

  4. #4
    zdjbel is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Apr 2012
    Posts
    42
    Thank you Ano and Welshgasman. I applied the code to the AfterUpdate event of the combo box and it works only it deletes all the blanks and the characters I want to keep leaving the cell empty. Not sure but am I supposed to insert the name of the combo box somewhere in the code? This is what thee code looks like

    Private Sub cmbGoToPr_AfterUpdate()
    On Error GoTo cmbGoToPr_AfterUpdate_Err


    Dim MyString, TrimString


    MyString = " <-Trim-> " ' Initialize string.
    TrimString = LTrim(MyString) ' TrimString = "<-Trim-> ".
    TrimString = RTrim(MyString) ' TrimString = " <-Trim->".
    TrimString = LTrim(RTrim(MyString)) ' TrimString = "<-Trim->".


    ' Using the Trim function alone achieves the same result.
    'TrimString = Trim(MyString) ' TrimString = "<-Trim->".

    DoCmd.SearchForRecord , "", acFirst, "[PrNumber] = " & "'" & Screen.ActiveControl & "'"


    cmbGoToPr_AfterUpdate_Exit:
    Exit Sub


    cmbGoToPr_AfterUpdate_Err:
    MsgBox Error$
    Resume cmbGoToPr_AfterUpdate_Exit


    End Sub


    Thank you.

  5. #5
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    i not see any field from u , u just run example not touching any field u are trying to use.
    u ignored :
    mystring fill from paste instead " <-trim-> "
    ur field is in place of trimstring !!
    so only mystring if paste is not accepted in : [ur result field] = trim([ur paste])
    if
    Screen.ActiveControl is ur past i guess: trim(Screen.ActiveControl)

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,568
    Oh my lord
    You just use
    Me.mycontrolname = Trim(me.controlname) where controlname is actually the name of your control name.

    Everything else was just examples.
    In Access you actually have to define your variables. So Dim MyString is just rubbish.
    You need Dim MyString as String
    You need that actual type for every variable.

    Dim a,b,c,d as string is useless. Just the d is a string, everything else is a Variant.
    Last edited by Welshgasman; 09-01-2024 at 02:39 AM.
    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

  7. #7
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    Me.mycontrolname = Trim(me.controlname)
    u mean this should be in after update from the field

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    u mean this should be in after update from the field
    why would you allow unwanted field value first then fix it after? You would use BeforeUpdate on the control and fix it, then allow the update.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    quite simple before update is for conditions with options to cancel
    after update is for adjusting

  10. #10
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    "quite simple before update is for conditions with options to cancel
    after update is for adjusting"

    Says you.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    if u put all in before why is there an after ?
    if u put statements without cancel option it is useless done if update is cancelled
    so its not me who developed before and after update, but i try to use it efficient

  12. #12
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    I often have to copy/paste data into from an outside source to a text field on my form
    I applied the code to the AfterUpdate event of the combo box and it works only it deletes all the blanks and the characters I want to keep leaving the cell empty.

    Perhaps the question needs clarification.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  13. #13
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    perhaps the question needs clarification.
    looking at his procedure it is useless to ask clarification .
    at that moment he thought i sent for him to use , ignoring my instructions.
    it works means it give no error so he is happy
    then he realized no improvement. i assume he read again what i answer 1st time and change the procedure with improvement of welshgasman
    or just trim in the searchforrecord bec its only used once

  14. #14
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Quote Originally Posted by ano View Post
    looking at his procedure it is useless to ask clarification .
    at that moment he thought i sent for him to use , ignoring my instructions.
    it works means it give no error so he is happy
    then he realized no improvement. i assume he read again what i answer 1st time and change the procedure with improvement of welshgasman
    or just trim in the searchforrecord bec its only used once
    In his first post he's referencing a textbox and in the 2nd a combo box and all the characters being deleted. Makes no sense to me.
    But since you apparently speak for him, I'll just leave it there.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  15. #15
    zdjbel is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Apr 2012
    Posts
    42
    Thank you all for your input and especially Welshgasman for the clearest, simplest explanation. The Me.mycontrolname = Trim(me.controlname) worked and now it makes sense to me why it would be in the BeforeUpdate event rather than the AfterUpdate where I initially thought it would go (thank you Micron).

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

Similar Threads

  1. Find (not replace) spaces in a text field
    By catluvr in forum Queries
    Replies: 2
    Last Post: 03-08-2023, 12:36 PM
  2. Replies: 4
    Last Post: 08-27-2016, 03:25 PM
  3. Replies: 2
    Last Post: 07-09-2014, 06:41 PM
  4. Pasting formatted text into memo field
    By joekuhn in forum Access
    Replies: 0
    Last Post: 07-08-2011, 02:01 PM
  5. Deleting Spaces
    By grgerhard in forum Import/Export Data
    Replies: 2
    Last Post: 04-30-2006, 06:42 PM

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