473,769 Members | 7,650 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
13 3932
> >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(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
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(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


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.c om...

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
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
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
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
9865
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...
1
7410
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
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3963
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
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.