Limit number of lines in text box

N

NORBERT

Need to limit the number of lines a text box.
eg:
line1 <return>
line2 <return>
line3 <return>
line4 <return>

the total characters typed are 20 less than the Field
Size=200.
 
A

Arvin Meyer

NORBERT said:
Need to limit the number of lines a text box.
eg:
line1 <return>
line2 <return>
line3 <return>
line4 <return>

the total characters typed are 20 less than the Field
Size=200.

Count the number of Carriage returns in the text box:

Function CountCharInString(strIn As String, strChar As String) As Integer
' Counts occurrences of a character in a string
' Arvin Meyer - 12/14/1999

Dim intInstr As Integer
Dim intRet As Integer
intInstr = 0
intRet = 0
Do
intInstr = InStr(intInstr + 1, strIn, strChar)
If intInstr > 0 Then intRet = intRet + 1
Loop While intInstr > 0

CountCharInString = intRet

End Function

And use the count like this:

Sub TestBoxToCount_BeforeUpdate(Cancel As Integer)

If CountCharInString(Me.[TestBoxToCount],Chr(13) & Chr(10)) = 4 Then
MsgBox "You have reached the end of your string"
Cancel = True
End If

End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
N

Norbert

-----Original Message-----
Norbert,

If you're using Access 2000 or higher, then this is another way to do it:
Private Sub txtMyTextbox_Change()
Dim MyArray() As String
Dim iLineCount As Integer

MyArray = Split(Me.txtMyTextbox.Text, vbCrLf)
If UBound(MyArray) > 3 Then
DoCmd.Beep
MsgBox "You can't have more than 4 lines."

'Only allow 4 lines.
Me.txtMyTextbox.Text = Left (Me.txtMyTextbox.Text,
Len(Me.txtMyTextbox.Text) - 1)
'Reset the cursor.
Me.txtMyTextbox.SelStart = Len (Me.txtMyTextbox.Text)
End If
End Sub



Graham R Seach
Microsoft Access MCP, MVP
Sydney, Australia





.
Thanks Graham for your tip!
When user is editing the txtMyTextbox
All the lines are typed already
line1 <return>
line2 <return>
line3 <return>
line4 <return>

User goes to line1 add some text and press <return> then
the msgbox appears several times until deletes all the
characters in the last line.
Can it be prevented?

Thanks again
Norbert
 
G

Graham R Seach

Norbert,

Replace the following line:
Me.txtMyTextbox.Text = Left(Me.txtMyTextbox.Text,
Len(Me.txtMyTextbox.Text) - 1)

....with this one...
Me.txtMyTextbox.Text = Left(Me.txtMyTextbox.Text,
InStrRev(Me.txtMyTextbox.Text, vbCrLf, -1))

Graham R Seach
Microsoft Access MCP, MVP
Sydney, Australia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top