Insert vbCr every 10 characters

How can I insert a vbCr every 10 characters in real time (as the user is typing in a form)?

Hello, my name is Bob
Hello, my
name is Bo

But I only want it to insert the vbCr if the user hasn't entered a new line.  So I want it to have a vbCr at least every 10 characters.

Hello,<user-entered break>
my name is Bob.
my name is

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlopederiiAuthor Commented:
One additional requirement -- it needs to work even if the user is modifying what has been typed earlier.  So if they are deleting characters, the position of the non-user-entered vbCrs should be moved accordingly.
Here's some code to get you started:

Dim charCount As Integer

Private Sub Text0_GotFocus()
    charCount = 0
End Sub

Private Sub Text0_KeyPress(KeyAscii As Integer)
Text2 = Text2 & " " & KeyAscii

    If charCount >= 10 Then
        SendKeys (vbCrLf)
        charCount = 0
    End If
    Select Case KeyAscii
    Case 8
        If charCount > 0 Then
            charCount = charCount - 1
        End If
    Case 13
        charCount = 0
    Case Else
        charCount = charCount + 1
    End Select
End Sub

That'll work, the only thing you have to do is change the "GotFocus" subroutine to figure out what charCount should be if your text field is not initially empty.

Oh, one other thing: make sure you set the "enter key behaviour" to "new line in field" instead of "default"


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I would do this in the AfterUpdate Event of the control they are entering. However, I suspect you may want to check for CR and LF in chich case you can use vbCrLf rather than vbCr

Cheers, Andrew

dim strTEXT as string
dim strOld as string

    strOLD = Replace(me.txtMyControl, vbCr, "")
    for cnt = 1 to len(strOLD)
        strTEXT = strTEXT & Mid(strOLD,cnt)
        if cnt mod 10 = 0 then
           strTEXT = strTEXT & vbCR
        end if
    Next cnt

    Me.txtMyControl = strTEXT
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

AlopederiiAuthor Commented:
Thank you both for your input.  I created code on AfterUpdate to format it then.

One final step and then I'll award the points:
How can I make a text box have vertical AND horizontal scroll bars?
AlopederiiAuthor Commented:
Actually, I am going to split the points.  You both helped to point me in the right direction.

I am going to post a separate question about the scrollbars.  Feel free to answer that one as well. ;)
The textbox has a ScrollBars property that you can set to Neither, Both, horizontal or verticle.

Cheers, Andrew
AlopederiiAuthor Commented:
Am I looking in the wrong place?  When I go to Properties > Format the Scroll Bars proerty only shows None or Vertical.  I am using Access 2002.
I have commented on your other question, the Scrollbar is only None or Vertical in teh standard text box but if you insert the Microsoft Forms 2 Textbox all 4 options are available. You insert this as an ActiveX control.

Cheers, Andrew
Per request - points reduced for split.

Alopederii- please accept a comment by wide_awake as the answer.

TextReport- please collect your points in:

EE Moderator
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.