Limit Character per vbNewLine in Memo

calvinle

Registered User.
Local time
Yesterday, 16:31
Joined
Sep 26, 2014
Messages
332
Hey everyone,

I am wondering if anyone knows how to limit the character per line in a memo textbox?

I have a textbox that allows user to put note there, however due to the resolution of different user, it appears that sometime the textbox shrink bigger/smaller on some machine, so i want to make the textbox very large but limit the character per line in that textbox.
Once it reaches 70 characters, it will automatically jump to the next vbNewLine.
If they are in the 2nd line already, they can enter a maximum of 70 characters, then it will jump on next line.

Basically, a line can contains maximum of 70 character., and max of line per textbox is 12 lines.

Anyone have ever work on this kind of thing?

Thanks
 
on your keydown event of your memo textbox control:

Private Sub MemoField_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Or _
KeyCode = vbKeyBack Or _
KeyCode = vbKeyLeft Or _
KeyCode = vbKeyRight Or _
KeyCode = vbKeyDown Or _
KeyCode = vbKeyUp Or _
KeyCode = vbKeyPageDown Or _
KeyCode = vbKeyPageUp Then Exit Sub
If Len(Me.yourMemoControl.Text) >= 70 Then KeyCode = 0
End Sub
 
Do you want it to jump to the next line in the middle of a word or should it wrap text like normal text wrapping does? Why isn't the text wrapping that exists for text boxes sufficient for you needs? Basically I'm not understanding why you need to do this. Could you upload some screen shot that illustrates your problem.
 
This will only do the first line and breaks on spaces so it isn't a proper solution yet.
I have not had time to extend it to multiple lines but should give you some ideas. This would be done by looking for the last new line.

Code:
Private Sub ctrlMemo_Change()
  
 Dim LastSpace As Integer
  
     With Me.ctrlMemo
  
         If Len(.Text) >= 70 Then
             LastSpace = InStrRev(.Text, " ")
             .Text = Left(.Text, LastSpace) & vbNewLine & Mid(.Text, LastSpace + 1)
         End If
 
     End With
  
 End Sub
 
my first post failed.
it only check for the first 70 chars.

this one limits each line to 70.
plus it word wrap if the word will break within 70 chars.
Code:
Private Sub MemoField_KeyPress(KeyAscii As Integer)
    Dim l As Long
    Dim lastchar As String
    Dim strMemo As String
    
    If KeyAscii > 31 And KeyAscii < 256 Then
        strMemo = Me.MemoField.Text
        l = Len(strMemo)
        
        If (l Mod 70) = 0 And l >= 70 Then
            lastchar = Right(strMemo, 1)
            l = InStrRev(strMemo, " ")
            'only wordwrap if l <= 10 characters
            'you can adjust this
            If lastchar <> " " And Chr(KeyAscii) <> " " And Len(strMemo) - l <= 10 Then
                Me.MemoField = Left(strMemo, l) & vbCrLf & Mid(strMemo, l + 1)
            
            Else
            
                Me.MemoField = Me.MemoField.Text & vbCrLf
            End If
            Me.MemoField.SelStart = Len(Me.MemoField)
        End If
    End If

End Sub
 
Last edited:
Hi,

That works for typing to max 70 characters, but it does not limit to a max of 12 lines of the memo textbox.

Also, what happen if the user copy and paste instead of typing?

Thanks for the code! I would never think how to get there..
 
rather than try and do this, just use the zoom facility

on a click (or maybe a double-click)

runcommand accmdzoombox

also shift+F2 does the same thing on any text box.

----
you can limit the overall length of the field (if you like) by the field properties
 
i think my code will fail if you are inserting text rather than appending a text at the end.

my strong suggestion is just set the RightMargin property of your memofield control on the form. therefore, no need coding it.
 
i think my code will fail if you are inserting text rather than appending a text at the end.

That is why the Change Event is better suited to the task than KeyPress.
 
not entirely, you see using our code breaks the memo in an ugly way when we are in insertion point.

im just too lazy writing the code to correct this.
 

Users who are viewing this thread

Back
Top Bottom