Trapping/Validating Paste'd data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Gray

    Trapping/Validating Paste'd data

    Greetings all,

    Quick newbie type question:

    I would like to be able to trap non-numerical data entered into a
    textbox via CTRL+C and/or Shift+Insert.

    I realise that this data can be validated using the TEXTn_validate
    event but I would like to stop the user before it gets that far.

    Thanks in advance.

    David.


  • Martin Trump

    #2
    Re: Trapping/Validating Paste'd data

    In article <tnrkkvolkluhjc u1op02ff3do04jr 47rgi@4ax.com>, David Gray
    <blah@blah.co m> writes[color=blue]
    >I would like to be able to trap non-numerical data entered into a
    >textbox via CTRL+C and/or Shift+Insert.[/color]

    This was the subject of a long and IIRC slightly heated thread here on
    clbvm a while ago.

    Do you mean just integers in the TBox or is 1.234 acceptable? 1E-6? 001?
    Can you be more specific?

    TIA.

    --
    Martin Trump

    Comment

    • David Gray

      #3
      Re: Trapping/Validating Paste'd data


      Thanks for that.

      Cheers,
      Dave.

      On Tue, 26 Aug 2003 06:14:44 +0000 (UTC), erewhon@nowhere .com (J
      French) wrote:
      [color=blue]
      >The Text_Change event is probably what you are after
      >
      >You'll probably need to hold the original data in a local Static
      >Variable.
      >I would also have a Static Flag to prevent re-entrancy.
      >
      >SelPos will need holding ...
      >
      >Option Explicit
      >
      >Private Sub Text1_Change()
      > Static OldText As String
      > Static InRoutineFlag As Boolean
      >
      > If InRoutineFlag Then Exit Sub
      > InRoutineFlag = True
      >
      > Select Case LS_FailNumeric( Text1.Text)
      > Case True: Text1.Text = OldText
      > Case False: OldText = Text1.Text
      > End Select
      >
      > InRoutineFlag = False
      >End Sub
      >
      >Private Function LS_FailNumeric( Text$) As Boolean
      > Dim L9%
      > ' Note: "" passes the test
      > For L9 = 1 To Len(Text)
      > If InStr("01234567 89", Mid$(Text$, L9, 1)) = 0 Then
      > LS_FailNumeric = True
      > Exit Function
      > End If
      > Next
      >
      >End Function
      >
      >
      >
      >
      >On Mon, 25 Aug 2003 21:19:10 +0100, David Gray <blah@blah.co m> wrote:
      >[color=green]
      >>Greetings all,
      >>
      >>Quick newbie type question:
      >>
      >>I would like to be able to trap non-numerical data entered into a
      >>textbox via CTRL+C and/or Shift+Insert.
      >>
      >>I realise that this data can be validated using the TEXTn_validate
      >>event but I would like to stop the user before it gets that far.
      >>
      >>Thanks in advance.
      >>
      >>David.
      >>
      >>[/color][/color]

      Comment

      • Martin Trump

        #4
        Re: Trapping/Validating Paste'd data

        In article <o9dnkvc0c0i0go qgn089kt1g57cqc qk3ht@4ax.com>, David Gray
        <blah@blah.co m> writes[color=blue]
        >No just integers. The fields are for entering year, month & date all
        >in numerical format.
        >
        >I would like to be able to block no integers then use the validate
        >event to give a warning if data not between certain values (1-31, 1-12
        >& 1900-2999).[/color]

        David, perhaps I'm slow to catch on. When you say 'fields' do you mean
        separate text box entries for date, month number and year? They would
        have to be validated with reference to each other. Can you give a few
        examples of what you want to accept and perhaps one or two you don't?

        TIA.

        Regards
        --
        Martin Trump

        Comment

        • David Gray

          #5
          Re: Trapping/Validating Paste'd data

          Hi Martin,

          Nope it's me being a bit vague.

          Ok The fields are all individual text boxes and I need to validate
          each one is within a range of values. For example...

          T_YEAR values between 1858 and 2999
          T_MONTH values between 1 and 12
          T_DAY values between 1 and 31 (This would need to be cross
          referenced to month)

          Once each textbox has been validated I will join the 3 together and
          test with the ISDATE function.

          I guess my original question should have been. Can I disallow
          alphabetic or punctuation characters being entered or pasted by CTRL+V
          and/or SHIFT+INS ?

          Not having a problem with any of the validations, it's just the
          trapping of data being pasted in that I'm struggling with.

          Cheers,
          Dave.






          On Tue, 26 Aug 2003 20:54:40 +0100, Martin Trump
          <Martin@wmeadow .demon.co.uk> wrote:
          [color=blue]
          >In article <o9dnkvc0c0i0go qgn089kt1g57cqc qk3ht@4ax.com>, David Gray
          ><blah@blah.com > writes[color=green]
          >>No just integers. The fields are for entering year, month & date all
          >>in numerical format.
          >>
          >>I would like to be able to block no integers then use the validate
          >>event to give a warning if data not between certain values (1-31, 1-12
          >>& 1900-2999).[/color]
          >
          >David, perhaps I'm slow to catch on. When you say 'fields' do you mean
          >separate text box entries for date, month number and year? They would
          >have to be validated with reference to each other. Can you give a few
          >examples of what you want to accept and perhaps one or two you don't?
          >
          >TIA.
          >
          >Regards[/color]

          Comment

          • Don@home.com

            #6
            Re: Trapping/Validating Paste'd data

            On Tue, 26 Aug 2003 22:27:27 +0100, David Gray <blah@blah.co m> wrote:
            [color=blue]
            >Hi Martin,
            >
            >Nope it's me being a bit vague.
            >
            >Ok The fields are all individual text boxes and I need to validate
            >each one is within a range of values. For example...
            >
            >T_YEAR values between 1858 and 2999
            >T_MONTH values between 1 and 12
            >T_DAY values between 1 and 31 (This would need to be cross
            >referenced to month)
            >
            >Once each textbox has been validated I will join the 3 together and
            >test with the ISDATE function.
            >
            >I guess my original question should have been. Can I disallow
            >alphabetic or punctuation characters being entered or pasted by CTRL+V
            >and/or SHIFT+INS ?
            >
            >Not having a problem with any of the validations, it's just the
            >trapping of data being pasted in that I'm struggling with.
            >
            >Cheers,
            > Dave.
            >
            >[/color]

            I think what you need to use is the Validate Event and do you testing there and
            if it falls through make a beep, post a MsgBox, or what ever and set the Cancel
            Flag to True. This keeps the focus from moving on to another control....
            Something like this:::::

            Private Sub txtLineNumber_V alidate(Cancel As Boolean)
            Dim lRtn As Long
            On Error GoTo NumberError
            lRtn = CLng(txtLineNum ber.Text)
            'You could also test for inside you range here like
            If lRtn < 1815 Or lRtn > 2999 then goto NumberError
            Exit Sub

            NumberError:
            txtLineNumber.S elStart = 0
            txtLineNumber.S elLength = Len(txtLineNumb er.Text)
            Cancel = True 'Stop it from going off somewhere else
            End Sub

            Just Food for Thought...

            Have a good day...

            Don

            Comment

            • Mike Williams

              #7
              Re: Trapping/Validating Paste'd data

              "David Gray" <blah@blah.co m> wrote in message
              news:9mjnkvcimr fo335e184mv0ne0 ka8ihr28p@4ax.c om...
              [color=blue]
              > Ok The fields are all individual text boxes and I need
              > to validate each one is within a range of values.[/color]

              Personally I prefer to allow the user to enter whatever he wants and then
              analyse and interpret it only when he leaves the text box, displaying the
              results of the interpretation in the text box. However, if you want to
              prevent him from entering anything other than digits in the first place (as
              you say you want to do) then there are various ways to accomplish that.
              Using the API to set the Style to ES_NUMBERS (or whatever it is called)
              doesn't do what you want, because it still allows the user to paste other
              stuff in, so you will need to write a bit of code.

              Here is an example that checks for the Shift or Ctrl keys in the KeyDown
              event (before the keypress event occurs) and sets a flag (using the Tag
              property) if one or other is held down. The KeyPress event then checks the
              flag and rejects the keypress if Ctrl or Shift were held down (thereby
              preventing Ctrl V and Shift Insert). It then checks for a digit (0 to 9). As
              it stands the code will reject all attempts to paste stuff in (using Ctrl V
              or Shift Insert), including attempts to paste in digits. If you want to
              reject "pasted" stuff and yet still accept "pasted digits" then that, too,
              can be easily accomplished with a little bit of extra code.

              By the way, you say that you will use the IsDate function to check the
              validity of the date when you finally construct it from the threee text box
              inputs. If so, then make sure that you ask the user to enter all four digits
              for the year. That "IsDate" function accepts all sorts of rubbish,
              especially when the year is specified using just two digits.

              Print IsDate("32/08/03")

              I apologise to Martin for diving in on this one, but if he will insist on
              sitting back in Trump Towers quaffing brandy then he deserves all he gets
              ;-)

              Code follows:

              Mike

              Private Sub Text1_KeyDown(K eyCode As Integer, Shift As Integer)
              If Shift <> 0 Then
              KeyCode = 0
              Text1.Tag = "reject"
              Else
              Text1.Tag = ""
              End If
              End Sub

              Private Sub Text1_KeyPress( KeyAscii As Integer)
              If Text1.Tag = "reject" Then KeyAscii = 0
              If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
              KeyAscii = 0
              End If
              End Sub






              Comment

              • David Gray

                #8
                Re: Trapping/Validating Paste'd data


                Hi Mike,
                [color=blue]
                >
                >Personally I prefer to allow the user to enter whatever he wants and then
                >analyse and interpret it only when he leaves the text box, displaying the
                >results of the interpretation in the text box. However, if you want to
                >prevent him from entering anything other than digits in the first place (as
                >you say you want to do) then there are various ways to accomplish that.[/color]



                I agree. However I'm taking a selection of our screens written in
                Powerhouse4GL sat on an OpenVMS Alpha platform and attempting to write
                them in VB. It's just a proof of concept sort of exercise and because
                of this I need to make the VB screen behave exactly the same as
                original the Powerhouse. Plus any changes would baffle the hell out
                of our users. :)

                [color=blue]
                >Here is an example that checks for the Shift or Ctrl keys in the KeyDown
                >event (before the keypress event occurs) and sets a flag (using the Tag
                >property) if one or other is held down. The KeyPress event then checks the
                >flag and rejects the keypress if Ctrl or Shift were held down (thereby
                >preventing Ctrl V and Shift Insert). It then checks for a digit (0 to 9). As
                >it stands the code will reject all attempts to paste stuff in (using Ctrl V
                >or Shift Insert), including attempts to paste in digits. If you want to
                >reject "pasted" stuff and yet still accept "pasted digits" then that, too,
                >can be easily accomplished with a little bit of extra code.[/color]

                That sounds exactly what I need to do.

                [color=blue]
                >By the way, you say that you will use the IsDate function to check the
                >validity of the date when you finally construct it from the threee text box
                >inputs. If so, then make sure that you ask the user to enter all four digits
                >for the year. That "IsDate" function accepts all sorts of rubbish,
                >especially when the year is specified using just two digits.[/color]

                I was planning on validating all three textbox fields, stringing the
                date together with a four digit year then using isdate function.

                Cheers
                Dave.




                Comment

                • David Gray

                  #9
                  Re: Trapping/Validating Paste'd data


                  Thanks for all the suggestions. I may well be back later with more
                  questions ;-)


                  Cheers
                  Dave.


                  On Mon, 25 Aug 2003 21:19:10 +0100, David Gray <blah@blah.co m> wrote:
                  [color=blue]
                  >Greetings all,
                  >
                  >Quick newbie type question:
                  >
                  >I would like to be able to trap non-numerical data entered into a
                  >textbox via CTRL+C and/or Shift+Insert.
                  >
                  >I realise that this data can be validated using the TEXTn_validate
                  >event but I would like to stop the user before it gets that far.
                  >
                  >Thanks in advance.
                  >
                  >David.
                  >[/color]

                  Comment

                  • David Gray

                    #10
                    Calculate yyyy:mm:dd between two dates


                    Greetings all,

                    Yep I'm failry new to VB as you can probably guess. :-)


                    Given two dates I would like to calculate the difference between them
                    in years months and days. Datediff does not seem to do what I need.


                    S_DATE = 10-AUG-2002
                    E_DATE = 11-AUG-2002

                    Result = 1 year, 0 month, 1 day


                    Has anyone got a routine for this or do I need to code one up
                    taking into account leap years , days in months spanned etc etc.

                    Thanks in advance,
                    Dave.




                    Comment

                    • Rick Rothstein

                      #11
                      Re: Trapping/Validating Paste'd data

                      > >Here is an example that checks for the Shift or Ctrl keys in the KeyDown[color=blue][color=green]
                      > >event (before the keypress event occurs) and sets a flag (using the Tag
                      > >property) if one or other is held down. The KeyPress event then checks[/color][/color]
                      the[color=blue][color=green]
                      > >flag and rejects the keypress if Ctrl or Shift were held down (thereby
                      > >preventing Ctrl V and Shift Insert). It then checks for a digit (0 to 9).[/color][/color]
                      As[color=blue][color=green]
                      > >it stands the code will reject all attempts to paste stuff in (using Ctrl[/color][/color]
                      V[color=blue][color=green]
                      > >or Shift Insert), including attempts to paste in digits. If you want to
                      > >reject "pasted" stuff and yet still accept "pasted digits" then that,[/color][/color]
                      too,[color=blue][color=green]
                      > >can be easily accomplished with a little bit of extra code.[/color]
                      >
                      > That sounds exactly what I need to do.[/color]

                      Actually, it won't stop the pasting of bad data by right-clicking on the
                      TextBox and then selecting Paste from that menu (or from clicking on a Paste
                      option on a regular menu I would guess). Here is a method I've posted in the
                      past that stops both non-digit keystrokes and invalid pastes. To see it
                      work, start a new project, put a TextBox on the Form and paste the following
                      into the Form's code window.

                      Rick - MVP

                      Dim LastPosition As Long

                      Private Sub Text1_Change()
                      Static LastText As String
                      Static SecondTime As Boolean
                      If Not SecondTime Then
                      With Text1
                      If .Text Like "*[!0-9]*" Then
                      Beep
                      SecondTime = True
                      .Text = LastText
                      .SelStart = LastPosition
                      Else
                      LastText = .Text
                      End If
                      End With
                      End If
                      SecondTime = False
                      End Sub

                      Private Sub Text1_MouseDown (Button As Integer, _
                      Shift As Integer, X As Single, Y As Single)
                      With Text1
                      LastPosition = .SelStart
                      'Place any other MouseDown event code here
                      End With
                      End Sub

                      Private Sub Text1_KeyPress( KeyAscii As Integer)
                      With Text1
                      LastPosition = .SelStart
                      'Place any other KeyPress checking code here
                      End With
                      End Sub


                      Comment

                      • Rick Rothstein

                        #12
                        Re: Calculate yyyy:mm:dd between two dates

                        > Given two dates I would like to calculate the difference between them[color=blue]
                        > in years months and days. Datediff does not seem to do what I need.
                        >
                        >
                        > S_DATE = 10-AUG-2002
                        > E_DATE = 11-AUG-2002
                        >
                        > Result = 1 year, 0 month, 1 day[/color]

                        That looks like only 1 day to me.<g>

                        [color=blue]
                        > Has anyone got a routine for this or do I need to code one up
                        > taking into account leap years , days in months spanned etc etc.[/color]

                        This is a modification of some code I've posted in the past. I think it does
                        what you want. Note that is properly singularizes and/or pluralizes the text
                        parts.

                        Rick - MVP

                        Function YMD(StartDate As Date, EndDate As Date) As String
                        Dim TempDate As Date
                        Dim NumOfYears As Long
                        Dim NumOfMonths As Long
                        Dim NumOfWeeks As Long
                        Dim NumOfDays As Long
                        Dim NumOfHMS As Double
                        Dim TSerial1 As Double
                        Dim TSerial2 As Double
                        NumOfYears = DateDiff("yyyy" , StartDate, EndDate)
                        TSerial1 = TimeSerial(Hour (StartDate), _
                        Minute(StartDat e), Second(StartDat e))
                        TSerial2 = TimeSerial(Hour (EndDate), _
                        Minute(EndDate) , Second(EndDate) )
                        NumOfHMS = 24 * (TSerial2 - TSerial1)
                        If NumOfHMS < 0 Then
                        NumOfHMS = NumOfHMS + 24
                        EndDate = DateAdd("d", -1, EndDate)
                        End If
                        StartDate = DateSerial(Year (EndDate), _
                        Month(StartDate ), Day(StartDate))
                        If StartDate > EndDate Then
                        StartDate = DateAdd("yyyy", -1, StartDate)
                        NumOfYears = NumOfYears - 1
                        End If
                        NumOfMonths = DateDiff("m", StartDate, EndDate)
                        StartDate = DateSerial(Year (EndDate), _
                        Month(EndDate), Day(StartDate))
                        If StartDate > EndDate Then
                        StartDate = DateAdd("m", -1, StartDate)
                        NumOfMonths = NumOfMonths - 1
                        End If
                        NumOfDays = Abs(DateDiff("d ", StartDate, EndDate))
                        YMD = CStr(NumOfYears ) & " year" & _
                        IIf(NumOfYears = 1, "", "s")
                        YMD = YMD & ", "
                        YMD = YMD & CStr(NumOfMonth s) & " month" & _
                        IIf(NumOfMonths = 1, "", "s")
                        YMD = YMD & ", "
                        YMD = YMD & CStr(NumOfDays) & " day" & _
                        IIf(NumOfDays = 1, "", "s")
                        End Function


                        Comment

                        • David Gray

                          #13
                          Re: Calculate yyyy:mm:dd between two dates


                          That does the job perfectly!

                          Thanks a lot.

                          Dave.


                          On Wed, 27 Aug 2003 11:58:38 -0400, "Rick Rothstein"
                          <rickNOSPAMnews @NOSPAMcomcast. net> wrote:
                          [color=blue][color=green]
                          >> Given two dates I would like to calculate the difference between them
                          >> in years months and days. Datediff does not seem to do what I need.
                          >>
                          >>
                          >> S_DATE = 10-AUG-2002
                          >> E_DATE = 11-AUG-2002
                          >>
                          >> Result = 1 year, 0 month, 1 day[/color]
                          >
                          >That looks like only 1 day to me.<g>
                          >
                          >[color=green]
                          >> Has anyone got a routine for this or do I need to code one up
                          >> taking into account leap years , days in months spanned etc etc.[/color]
                          >
                          >This is a modification of some code I've posted in the past. I think it does
                          >what you want. Note that is properly singularizes and/or pluralizes the text
                          >parts.
                          >
                          >Rick - MVP
                          >
                          >Function YMD(StartDate As Date, EndDate As Date) As String
                          > Dim TempDate As Date
                          > Dim NumOfYears As Long
                          > Dim NumOfMonths As Long
                          > Dim NumOfWeeks As Long
                          > Dim NumOfDays As Long
                          > Dim NumOfHMS As Double
                          > Dim TSerial1 As Double
                          > Dim TSerial2 As Double
                          > NumOfYears = DateDiff("yyyy" , StartDate, EndDate)
                          > TSerial1 = TimeSerial(Hour (StartDate), _
                          > Minute(StartDat e), Second(StartDat e))
                          > TSerial2 = TimeSerial(Hour (EndDate), _
                          > Minute(EndDate) , Second(EndDate) )
                          > NumOfHMS = 24 * (TSerial2 - TSerial1)
                          > If NumOfHMS < 0 Then
                          > NumOfHMS = NumOfHMS + 24
                          > EndDate = DateAdd("d", -1, EndDate)
                          > End If
                          > StartDate = DateSerial(Year (EndDate), _
                          > Month(StartDate ), Day(StartDate))
                          > If StartDate > EndDate Then
                          > StartDate = DateAdd("yyyy", -1, StartDate)
                          > NumOfYears = NumOfYears - 1
                          > End If
                          > NumOfMonths = DateDiff("m", StartDate, EndDate)
                          > StartDate = DateSerial(Year (EndDate), _
                          > Month(EndDate), Day(StartDate))
                          > If StartDate > EndDate Then
                          > StartDate = DateAdd("m", -1, StartDate)
                          > NumOfMonths = NumOfMonths - 1
                          > End If
                          > NumOfDays = Abs(DateDiff("d ", StartDate, EndDate))
                          > YMD = CStr(NumOfYears ) & " year" & _
                          > IIf(NumOfYears = 1, "", "s")
                          > YMD = YMD & ", "
                          > YMD = YMD & CStr(NumOfMonth s) & " month" & _
                          > IIf(NumOfMonths = 1, "", "s")
                          > YMD = YMD & ", "
                          > YMD = YMD & CStr(NumOfDays) & " day" & _
                          > IIf(NumOfDays = 1, "", "s")
                          >End Function
                          >[/color]

                          Comment

                          • Jeff Brown

                            #14
                            Re: Calculate yyyy:mm:dd between two dates

                            try converting it to a julianized date format and then performing your
                            calculations. I remeber doing something similiar to that in school, or
                            trying evaluating each aspect individually.

                            difference in years ,,, then differnce in months ,... etc
                            "David Gray" <police@spamcop .net> wrote in message
                            news:0fdpkv028p tq6qin656ccj1rh vqhc32fdd@4ax.c om...[color=blue]
                            >
                            > Greetings all,
                            >
                            > Yep I'm failry new to VB as you can probably guess. :-)
                            >
                            >
                            > Given two dates I would like to calculate the difference between them
                            > in years months and days. Datediff does not seem to do what I need.
                            >
                            >
                            > S_DATE = 10-AUG-2002
                            > E_DATE = 11-AUG-2002
                            >
                            > Result = 1 year, 0 month, 1 day
                            >
                            >
                            > Has anyone got a routine for this or do I need to code one up
                            > taking into account leap years , days in months spanned etc etc.
                            >
                            > Thanks in advance,
                            > Dave.
                            >
                            >
                            >
                            >[/color]



                            Comment

                            Working...