473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function to insert text adding a carriage return for some reason

Seth Schrock
2,965 Expert 2GB
I found a function online that finds the cursor in a textbox, and then allows me to insert a string where the cursor is. When I call it as the website suggested, it works fine. However, it selects the whole field. I added a part that makes it so that it finds where the cursor was + the length of the added string and sets the .SelStart to this value. However, when I try to use this added ability, it adds a carriage return at my .SelStart point. I can't figure out what is happening.

Here is the original way that I called the function:
Expand|Select|Wrap|Line Numbers
  1. LegalWording = ReplaceViaPosition(Me.LegalWording.Text, gCursorPosition, gCursorLen, "%year%", True)
Here is the way that adds the carriage return:
Expand|Select|Wrap|Line Numbers
  1. With Me.LegalWording
  2.                 .SetFocus
  3.                 LegalWording = ReplaceViaPosition(.Text, gCursorPosition, gCursorLen, "%month%", False)
  4.                 .SelStart = intNextStart
  5.             End With
Interesting note: If i comment out the line .SelStart = intNextStart, then it wipes out the entire string that was in the textbox (Me.LegalWording), inserts the carriage return and does not add the string that I wanted to add.

Here is the entire ReplaceViaPosition() function:
Expand|Select|Wrap|Line Numbers
  1. Public intNextStart As Integer
  2. ' ARGUMENTS:
  3. ' *strText: The original text to modify
  4. ' *intStart: The start position to enter or replace text (0 based)
  5. ' *intLen: The length of the selection to replace (0 based) (default 0)
  6. ' *strAdd: The text to add to the string (default ZLS)
  7. ' *bPadAddition: True to pad the added text with a space (default False)
  8. ' * * * * * * * *If the addition is at the start of the string no leading
  9. ' * * * * * * * *space will be added.
  10. ' * * * * * * * *If the padding results in two consecutive spaces the
  11. ' * * * * * * * *consecutive space will be removed
  12. ' * * * * * * * *Padding operations will be bypassed if strAdd is a ZLS
  13.  
  14. Public Function ReplaceViaPosition( _
  15.    ByVal strText As String, _
  16.    intStart As Integer, _
  17.    Optional intLen As Integer = 0, _
  18.    Optional strAdd As String = "", _
  19.    Optional bPadAddition As Boolean = False _
  20.    ) As String
  21. On Error GoTo Error_Proc
  22. Dim Ret As String
  23. '=========================
  24.  Dim strOriginal As String
  25. '=========================
  26.  
  27.  'retain the original in case of error
  28.  strOriginal = strText
  29.  
  30.  'set the value of where the cursor should be after the inserted text
  31.  intNextStart = intStart + Len(strAdd)
  32.  
  33.  'validate the position and length to make sure
  34.  'they're within an appropriate range
  35.  If ((intStart) > Len(strText)) Or _
  36.     (intStart + intLen) > Len(strText) Then
  37.    'bad entry, let's raise error 9: Subscript out of range
  38.    Err.Raise 9, "ReplaceViaPosition", "Subscript out of range"
  39.  End If
  40.  
  41.  'remove any text that is selected due to intLen
  42.  If intLen > 0 Then
  43.    strText = Left(strText, intStart) & Mid(strText, intStart + intLen + 1)
  44.  End If
  45.  
  46.  If Len(strAdd) <> 0 Then
  47.    'add strAdd to the string in the specified position
  48.    strText = Left(strText, intStart) & strAdd & Mid(strText, intStart + 1)
  49.  
  50.    'perform the padding if required
  51.    If bPadAddition Then
  52.  
  53.      'work on the leading space first
  54.  
  55.      'if we're adding to the start of the string we don't need
  56.      'any leading spaces
  57.      If intStart > 0 Then
  58.        'find out if the char before the addition is a space
  59.        If Mid(strText, intStart, 1) <> " " Then
  60.          'it's not a space, we'll need to enter one
  61.          strText = Left(strText, intStart) & " " & Mid(strText, intStart + 1)
  62.          'we added a space, so let's increase our intStart by one
  63.          'to compensate for the next check
  64.          intStart = intStart + 1
  65.          'increase intNextStart by 1 to account for the added space
  66.          intNextStart = intNextStart + 1
  67.        End If
  68.      End If
  69.  
  70.      'work on the trailing space next
  71.  
  72.      'find out if the char after the addition is a space
  73.      If Mid(strText, intStart + 1 + Len(strAdd), 1) <> " " Then
  74.        'it's not a space, we'll need to enter one
  75.        'but first check if it's the end of the text...
  76.        If (intStart + Len(strAdd)) < Len(strText) Then
  77.          'ok, we're safe to add a space after the addition
  78.          strText = Left(strText, intStart + Len(strAdd)) & " " & Mid(strText, intStart + Len(strAdd) + 1)
  79.          'increase intNextStart by 1 to account for the added space
  80.          intNextStart = intNextStart + 1
  81.        End If
  82.      End If
  83.  
  84.    End If
  85.  
  86.  End If
  87.  
  88.  Ret = strText
  89.  
  90. '=========================
  91. Exit_Proc:
  92.  ReplaceViaPosition = Ret
  93.  Exit Function
  94. Error_Proc:
  95.  Ret = strOriginal
  96.  MsgBox "Error: " & Trim(Str(Err.Number)) & vbCrLf & _
  97.    "Desc: " & Err.Description & vbCrLf & vbCrLf & _
  98.    "Module: modReplaceViaPosition, Procedure: ReplaceViaPosition" _
  99.    , vbCritical, "Error!"
  100.  Resume Exit_Proc
  101.  Resume
  102. End Function
I added lines #19 & 20, 54 & 55, and 68 & 69 so that I could then set the cursor at the end of the inserted text.
Here is the link to the website where I found this function: ReplaceViaPosition

The function obviously works as it works when just performing its original purpose, so I don't think that the function is the problem (I could be wrong). I think that there is something about the way that I call the function my way that throws it for a loop.

gCursorPosition and gCursorLen are declared as integers.
Dec 29 '12 #1
6 2641
TheSmileyCoder
2,322 Expert Mod 2GB
I don't see anything that would add a carriage return, in what you have posted. The function code, is that the original or your modified version?
Dec 30 '12 #2
Seth Schrock
2,965 Expert 2GB
The function code is my slightly edit version. The lines that I added are lines 19-20, 54-55, and 68-69. I looked through as well and couldn't find anything either. However, I'm very unfamiliar with the Left() and Mid() functions and all the properties that were used in them, so I couldn't be sure.

I've actually just finished creating my own function to do this and it works fine. The main difference that I see is that I did my .SelStart inside the insertBlank procedure instead of in the calling procedure. Not sure why that would make a difference. Here is what I created:
Expand|Select|Wrap|Line Numbers
  1. Public Sub insertBlank1(strText As String, intStart As Integer, intLen As Integer, _
  2.                         strAddition As String, Optional blnSpaceCheck As Boolean = False)
  3.  
  4. Dim strLeft As String
  5. Dim strRight As String
  6. Dim strFinal As String
  7.  
  8.  
  9. '**************************************
  10. 'Grab text to the left of the selection
  11. '**************************************
  12.  
  13. 'Check if the selection starts at the beginning of the textbox
  14. If intStart = 0 Then
  15.     strLeft = ""
  16. Else
  17.     strLeft = Left(strText, intStart)
  18. End If
  19.  
  20.  
  21. '***************************************
  22. 'Grab text to the right of the selection
  23. '***************************************
  24.  
  25. 'Check if the selection ends at the end of the string
  26. If (intStart + intLen) = Len(strText) Then
  27.     strRight = ""
  28. Else
  29.     strRight = Mid(strText, intStart + intLen + 1)
  30. End If
  31.  
  32.  
  33. '****************************************************
  34. 'Checking for spaces around where strAddition will go
  35. '****************************************************
  36.  
  37. If blnSpaceCheck = True Then
  38.  
  39.     'If strLeft = "" then strAddition will go at the beginning
  40.     'of the textbox and doesn't need a preceding space
  41.     If strLeft <> "" Then
  42.  
  43.         'Check if there is a space at the end of the string
  44.         If Right(strLeft, 1) <> " " Then
  45.             strLeft = strLeft & " "
  46.         End If
  47.  
  48.  
  49.     End If
  50.  
  51.  
  52.     'If strRight = "" then strAddition will go at the end
  53.     'of the textbox and doesn't need a proceding space
  54.     If strRight <> "" Then
  55.  
  56.         'Check if there is a space at the beginning of the string
  57.         If Left(strRight, 1) <> " " Then
  58.             strRight = " " & strRight
  59.         End If
  60.  
  61.     End If
  62.  
  63. End If
  64.  
  65. strFinal = strLeft & strAddition & strRight
  66.  
  67. With Forms!frmPenalCodes!LegalWording
  68.     .SetFocus
  69.     .Value = strFinal
  70.     .SelStart = Len(strLeft) + Len(strAddition)
  71. End With
  72.  
  73. End Sub
Personally, I think mine is clearer than theirs, but I might be a little biased :)
Dec 30 '12 #3
zmbd
5,501 Expert Mod 4TB
Seth,
I'm with Rabbit, there isn't anything obvious that should be doing this...

So, to my next tool in the bag-o-tricks:

Are you familiar with the watch window?
This is like the coolest... often better than the locals window.

Here's what I do when strange things happen and I just can't seem to track them down with the usual methods.
Open the VBA editor.
Find the variable that I'm trying to track...
Select
<shift><F9>
This should open the watch window and add the variable to that window.
I do this for each variable of interest (this is a great tool for trouble shooting code when all seems lost... there are other options too - in the VBE ribbon goto debug, add watch, [Help])
Now add a stop before the weirdness begins.
[F8]
And you can watch how things are progressing.
Dec 31 '12 #4
Seth Schrock
2,965 Expert 2GB
I think that I have reached the ultimate in weirdness. If I place a stop at the very beginning of the function (line 26 of the OP to be exact) and watch strText, the function works beautifully. If I then remove the Stop, it goes back to adding the carriage return. Do you have anything in your bag of tricks for this?

I think that I'm going to stick with the function that I created (post #3). I think it is much more clear of what is going on and easier to read than the one I found online.
Dec 31 '12 #5
NeoPa
32,556 Expert Mod 16PB
That happens sometimes Seth. A bitch really, but there you go.

The trick there is to go old-school and get the code to log the values as they are changed. This allows the code to run in real-time. It also shows exactly what does what and where in your code. The The Immediate Pane (Ctrl-G) is where your results should be printed to. It may be slow and laborious, but it is a way to debug that isn't exposed to that real-time problem found when tracing through some code.
Dec 31 '12 #6
zmbd
5,501 Expert Mod 4TB
NeoPa beat me to the punch... been very busy with family events.
Yes, that stupid CR shows up. I've even had an occasion where there were spaces between letters ("wantedthistext" and "w a n t e d t h i s t e x t" is what I got!) didn't show up in the watch-window, only at run-time, turned out to be an interference from one of the IT "watch-dog" programs because this didn't happen at home.
Dec 31 '12 #7

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

Similar topics

5
by: stephane | last post by:
Hi, I don't know how to test something. I read at file.txt caracter by caracter And I would like to know when it is the end of my line in the text file. exemple of a line: ...
8
by: Steven | last post by:
i need to force a carriage return with a textarea field at X number of characters. anybody know how to do this? tks
2
by: Paradigm | last post by:
I want to insert a carriage return into a text field. I am using insert into and \n for newline. The MYSQL database is accessed by a MS Access front end and the text field appears with a small...
2
by: Torsten Zachert | last post by:
I would like to insert some text with embedded carriage return/line feed into a MS Access text field with OleDb and C# ADO.NET. I tried to use "\n" in combination with "\r". If I display the input...
3
by: Simon Middlemiss | last post by:
I have a multiline textbox which I would like populated in the following manner. "Line1 Line2 Line3 Line4" etc. I know how to do it in code, but is there a way to do it in the design view?
1
by: Jason | last post by:
hi, all, after I read a string from a text file, which contains a hidden character (carriage return) some where in the string, how can I remove this hidden character, because when I populated it...
2
by: Contro | last post by:
Hi guys! I was wondering if you could help me. Basically, I'm wanting to do a sort of auto complete in an access text box, with the help of visual basic, where the user will put in, for...
4
by: whitej77777 | last post by:
I am trying to write a user defined function that will allow me to strip off the last carriage return and line feed from a text field. We have address fields stored in a text field for our ERP...
2
by: Bobby | last post by:
Hi I'm trying to export some data from an Access table to Sage Line 50 using VBA. It works fine, except that very occasionally one of the fields contains a carriage return. If I step through my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.