473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.
Jul 17 '05 #1
13 3898
In article <tn********************************@4ax.com>, David Gray
<bl**@blah.com> writes
I would like to be able to trap non-numerical data entered into a
textbox via CTRL+C and/or Shift+Insert.


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
Jul 17 '05 #2

Thanks for that.

Cheers,
Dave.

On Tue, 26 Aug 2003 06:14:44 +0000 (UTC), er*****@nowhere.com (J
French) wrote:
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("0123456789", 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 <bl**@blah.com> wrote:
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.


Jul 17 '05 #3
In article <o9********************************@4ax.com>, David Gray
<bl**@blah.com> writes
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).


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
Jul 17 '05 #4
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
<Ma****@wmeadow.demon.co.uk> wrote:
In article <o9********************************@4ax.com>, David Gray
<bl**@blah.com> writes
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).


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


Jul 17 '05 #5
Don
On Tue, 26 Aug 2003 22:27:27 +0100, David Gray <bl**@blah.com> wrote:
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.


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_Validate(Cancel As Boolean)
Dim lRtn As Long
On Error GoTo NumberError
lRtn = CLng(txtLineNumber.Text)
'You could also test for inside you range here like
If lRtn < 1815 Or lRtn > 2999 then goto NumberError
Exit Sub

NumberError:
txtLineNumber.SelStart = 0
txtLineNumber.SelLength = Len(txtLineNumber.Text)
Cancel = True 'Stop it from going off somewhere else
End Sub

Just Food for Thought...

Have a good day...

Don
Jul 17 '05 #6
"David Gray" <bl**@blah.com> wrote in message
news:9m********************************@4ax.com...
Ok The fields are all individual text boxes and I need
to validate each one is within a range of values.


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(KeyCode 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


Jul 17 '05 #7

Hi Mike,

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.
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. :)

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.
That sounds exactly what I need to do.

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.


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

Cheers
Dave.


Jul 17 '05 #8

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 <bl**@blah.com> wrote:
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.


Jul 17 '05 #9

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.


Jul 17 '05 #10
> >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 theflag 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). Asit stands the code will reject all attempts to paste stuff in (using Ctrl Vor 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.


That sounds exactly what I need to do.


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
Jul 17 '05 #11
> 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
That looks like only 1 day to me.<g>

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.


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(StartDate), Second(StartDate))
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(NumOfMonths) & " month" & _
IIf(NumOfMonths = 1, "", "s")
YMD = YMD & ", "
YMD = YMD & CStr(NumOfDays) & " day" & _
IIf(NumOfDays = 1, "", "s")
End Function
Jul 17 '05 #12

That does the job perfectly!

Thanks a lot.

Dave.
On Wed, 27 Aug 2003 11:58:38 -0400, "Rick Rothstein"
<ri************@NOSPAMcomcast.net> wrote:
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


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

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.


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(StartDate), Second(StartDate))
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(NumOfMonths) & " month" & _
IIf(NumOfMonths = 1, "", "s")
YMD = YMD & ", "
YMD = YMD & CStr(NumOfDays) & " day" & _
IIf(NumOfDays = 1, "", "s")
End Function


Jul 17 '05 #13
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" <po****@spamcop.net> wrote in message
news:0f********************************@4ax.com...

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.


Jul 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: 47computers | last post by:
Pretty new to PHP, I recently started learning about error trapping. As of right now, I include the following into a page in my website: -------BEGIN PASTE-------- error_reporting(E_ERROR |...
19
by: zacks | last post by:
I have a .NET 2.0 MDI application where the child form has a Tab Control. Each of the Tab in the Tab Control has a Validating event to handle what it should do when the user changes tabs. But...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.