473,756 Members | 7,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3928
In article <tn************ *************** *****@4ax.com>, David Gray
<bl**@blah.co m> 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("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 <bl**@blah.co m> 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.co m> 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.co m> 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_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
Jul 17 '05 #6
"David Gray" <bl**@blah.co m> wrote in message
news:9m******** *************** *********@4ax.c om...
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(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


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.co m> 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

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

Similar topics

9
2110
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 | E_PARSE); set_error_handler("SendErrorReport"); function SendErrorReport($errorNumber, $errorMessage, $errorFile, $errorLine, $vars) {
19
3226
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 these Validating Events are also fired when either the child form or the main (parent) form Close icon is clicked. And I need for these events to know if they are being invoked because the app (or child window) is being closed. I have set a boolean...
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10046
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9857
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.