Look For CR in Text Data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen Saunders

    #1

    Look For CR in Text Data

    Hey Folks,

    This one currently has me stumped. I have to check for the existence of a
    carriage return (Enter key, hex 0D) in text data. First I tried:

    'Check comment to not contain the CR (13) character
    For i = 1 To Len(txtComment. Text)
    On Error GoTo BogusComment2
    If Mid(txtComment. Text, i, 1) = CStr(13) Then
    MsgBox "Comment cannot contain the enter character"
    'Strip the CR from the comment
    txtComment.Text = Left(txtComment .Text, (i - 1)) + _
    Right(txtCommen t.Text, (Len(txtComment .Text) - i))
    Exit Sub
    End If
    On Error GoTo 0
    Next i

    This doesn't find the CR because CStr(13) equals a two character string
    ("13")

    Then I tried:

    'Check comment to not contain the CR (13) character
    For i = 1 To Len(txtComment. Text)
    On Error GoTo BogusComment2
    If CInt(Mid(txtCom ment.Text, i, 1)) = 13 Then
    MsgBox "Comment cannot contain the enter character"
    'Strip the CR from the comment
    txtComment.Text = Left(txtComment .Text, (i - 1)) + _
    Right(txtCommen t.Text, (Len(txtComment .Text) - i))
    Exit Sub
    End If
    On Error GoTo 0
    Next i

    This code traps to BogusComment when the first (non CR) character in the
    string is checked.

    Will using a KeyPress event to fix this?

    Stephen Saunders


  • Stephen Saunders

    #2
    Re: Look For CR in Text Data - Never mind

    I found that:

    Sub txtComment_KeyP ress(KeyAscii As Integer)

    If KeyAscii = 13 Then
    'Issue message
    MsgBox "Enter is not allowed in message text"
    KeyAscii = 0
    End If

    End Sub

    works.

    Sorry about asking. I hope this helps someone else.

    Stephen Saunders

    "Stephen Saunders" <kd6eut@earthli nk.net> wrote in message
    news:6pCYd.1661 $qW.357@newsrea d3.news.atl.ear thlink.net...[color=blue]
    > Hey Folks,
    >
    > This one currently has me stumped. I have to check for the existence of a
    > carriage return (Enter key, hex 0D) in text data. First I tried:
    >
    > 'Check comment to not contain the CR (13) character
    > For i = 1 To Len(txtComment. Text)
    > On Error GoTo BogusComment2
    > If Mid(txtComment. Text, i, 1) = CStr(13) Then
    > MsgBox "Comment cannot contain the enter character"
    > 'Strip the CR from the comment
    > txtComment.Text = Left(txtComment .Text, (i - 1)) + _
    > Right(txtCommen t.Text, (Len(txtComment .Text) - i))
    > Exit Sub
    > End If
    > On Error GoTo 0
    > Next i
    >
    > This doesn't find the CR because CStr(13) equals a two character string
    > ("13")
    >
    > Then I tried:
    >
    > 'Check comment to not contain the CR (13) character
    > For i = 1 To Len(txtComment. Text)
    > On Error GoTo BogusComment2
    > If CInt(Mid(txtCom ment.Text, i, 1)) = 13 Then
    > MsgBox "Comment cannot contain the enter character"
    > 'Strip the CR from the comment
    > txtComment.Text = Left(txtComment .Text, (i - 1)) + _
    > Right(txtCommen t.Text, (Len(txtComment .Text) - i))
    > Exit Sub
    > End If
    > On Error GoTo 0
    > Next i
    >
    > This code traps to BogusComment when the first (non CR) character in the
    > string is checked.
    >
    > Will using a KeyPress event to fix this?
    >
    > Stephen Saunders
    >
    >[/color]


    Comment

    • Stephen Saunders

      #3
      Re: Look For CR in Text Data - Never mind

      I found that:

      Sub txtComment_KeyP ress(KeyAscii As Integer)

      If KeyAscii = 13 Then
      'Issue message
      MsgBox "Enter is not allowed in message text"
      KeyAscii = 0
      End If

      End Sub

      works.

      Sorry about asking. I hope this helps someone else.

      Stephen Saunders

      "Stephen Saunders" <kd6eut@earthli nk.net> wrote in message
      news:6pCYd.1661 $qW.357@newsrea d3.news.atl.ear thlink.net...[color=blue]
      > Hey Folks,
      >
      > This one currently has me stumped. I have to check for the existence of a
      > carriage return (Enter key, hex 0D) in text data. First I tried:
      >
      > 'Check comment to not contain the CR (13) character
      > For i = 1 To Len(txtComment. Text)
      > On Error GoTo BogusComment2
      > If Mid(txtComment. Text, i, 1) = CStr(13) Then
      > MsgBox "Comment cannot contain the enter character"
      > 'Strip the CR from the comment
      > txtComment.Text = Left(txtComment .Text, (i - 1)) + _
      > Right(txtCommen t.Text, (Len(txtComment .Text) - i))
      > Exit Sub
      > End If
      > On Error GoTo 0
      > Next i
      >
      > This doesn't find the CR because CStr(13) equals a two character string
      > ("13")
      >
      > Then I tried:
      >
      > 'Check comment to not contain the CR (13) character
      > For i = 1 To Len(txtComment. Text)
      > On Error GoTo BogusComment2
      > If CInt(Mid(txtCom ment.Text, i, 1)) = 13 Then
      > MsgBox "Comment cannot contain the enter character"
      > 'Strip the CR from the comment
      > txtComment.Text = Left(txtComment .Text, (i - 1)) + _
      > Right(txtCommen t.Text, (Len(txtComment .Text) - i))
      > Exit Sub
      > End If
      > On Error GoTo 0
      > Next i
      >
      > This code traps to BogusComment when the first (non CR) character in the
      > string is checked.
      >
      > Will using a KeyPress event to fix this?
      >
      > Stephen Saunders
      >
      >[/color]


      Comment

      • Raoul Watson

        #4
        Re: Look For CR in Text Data


        "Stephen Saunders" <kd6eut@earthli nk.net> wrote in message
        news:6pCYd.1661 $qW.357@newsrea d3.news.atl.ear thlink.net...[color=blue]
        > Hey Folks,
        > This doesn't find the CR because CStr(13) equals a two character string
        > ("13")
        >[/color]

        Try this Steve..

        Put a text box and label on a form and a command button:

        Private Sub Command1_Click( )
        Dim t$
        Dim x
        t$ = Text1.Text

        x = InStr(t$, Chr$(13))
        If x > 0 Then
        Label1.Caption = "CR found at pos " & x
        Else
        Label1.Caption = "No CR found"
        End If

        End Sub


        Comment

        • Raoul Watson

          #5
          Re: Look For CR in Text Data


          "Stephen Saunders" <kd6eut@earthli nk.net> wrote in message
          news:6pCYd.1661 $qW.357@newsrea d3.news.atl.ear thlink.net...[color=blue]
          > Hey Folks,
          > This doesn't find the CR because CStr(13) equals a two character string
          > ("13")
          >[/color]

          Try this Steve..

          Put a text box and label on a form and a command button:

          Private Sub Command1_Click( )
          Dim t$
          Dim x
          t$ = Text1.Text

          x = InStr(t$, Chr$(13))
          If x > 0 Then
          Label1.Caption = "CR found at pos " & x
          Else
          Label1.Caption = "No CR found"
          End If

          End Sub


          Comment

          • preben nielsen

            #6
            Re: Look For CR in Text Data


            "Stephen Saunders" <kd6eut@earthli nk.net> skrev i en meddelelse
            news:6pCYd.1661 $qW.357@newsrea d3.news.atl.ear thlink.net...[color=blue]
            > Hey Folks,
            >
            > This one currently has me stumped. I have to check for the
            > existence of a carriage return (Enter key, hex 0D) in text data.
            > First I tried:
            >
            > 'Check comment to not contain the CR (13) character
            > For i = 1 To Len(txtComment. Text)
            > On Error GoTo BogusComment2
            > If Mid(txtComment. Text, i, 1) = CStr(13) Then
            > MsgBox "Comment cannot contain the enter character"
            > 'Strip the CR from the comment
            > txtComment.Text = Left(txtComment .Text, (i - 1)) + _
            > Right(txtCommen t.Text, (Len(txtComment .Text) - i))
            > Exit Sub
            > End If
            > On Error GoTo 0
            > Next i
            >
            > This doesn't find the CR because CStr(13) equals a two character
            > string[/color]

            CStr(13) is NOT a CR, CStr(13) is "13" ! CHR(13) is a CR ! ;-)

            And what's wrong with

            IF instr(1, txtComment.Text , vbCR) > 0 the
            MsgBox "Comment cannot contain the enter character"

            txtComment.Text = Replace(txtComm ent.Text, vbCR, "")
            End IF


            --
            /\ preben nielsen
            \/\ prel@post.tele. dk


            Comment

            • preben nielsen

              #7
              Re: Look For CR in Text Data


              "Stephen Saunders" <kd6eut@earthli nk.net> skrev i en meddelelse
              news:6pCYd.1661 $qW.357@newsrea d3.news.atl.ear thlink.net...[color=blue]
              > Hey Folks,
              >
              > This one currently has me stumped. I have to check for the
              > existence of a carriage return (Enter key, hex 0D) in text data.
              > First I tried:
              >
              > 'Check comment to not contain the CR (13) character
              > For i = 1 To Len(txtComment. Text)
              > On Error GoTo BogusComment2
              > If Mid(txtComment. Text, i, 1) = CStr(13) Then
              > MsgBox "Comment cannot contain the enter character"
              > 'Strip the CR from the comment
              > txtComment.Text = Left(txtComment .Text, (i - 1)) + _
              > Right(txtCommen t.Text, (Len(txtComment .Text) - i))
              > Exit Sub
              > End If
              > On Error GoTo 0
              > Next i
              >
              > This doesn't find the CR because CStr(13) equals a two character
              > string[/color]

              CStr(13) is NOT a CR, CStr(13) is "13" ! CHR(13) is a CR ! ;-)

              And what's wrong with

              IF instr(1, txtComment.Text , vbCR) > 0 the
              MsgBox "Comment cannot contain the enter character"

              txtComment.Text = Replace(txtComm ent.Text, vbCR, "")
              End IF


              --
              /\ preben nielsen
              \/\ prel@post.tele. dk


              Comment

              • five of nothing

                #8
                Re: Look For CR in Text Data

                Of course, to be clearer, it would be better to just use vbCr and be
                done with it.

                Comment

                • five of nothing

                  #9
                  Re: Look For CR in Text Data

                  Of course, to be clearer, it would be better to just use vbCr and be
                  done with it.

                  Comment

                  • UKtheBunny
                    New Member
                    • Oct 2005
                    • 3

                    #10
                    Here's a solution

                    dim i as integer

                    private sub Command1_Click( )

                    open "orig.txt" for input as #1
                    'This would be the file you are trying to find the CR

                    while not eof(1)
                    a = input$(1, 1)
                    i = i + 1

                    if a$ = chr(13) then
                    MsgBox (msg = "CR Found at" & i & "Char")
                    'This will give you the number of char's reached until CR was found
                    end if

                    wend
                    end sub

                    I could get more detailed but this should be enough for you. If not let me know.

                    Comment

                    Working...