Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I prevent a Paste command?

Status
Not open for further replies.

rarich01

Programmer
Oct 22, 2001
14
US
I have a text box that is meant for only numerical data. The problem was, how to enforce this. Well, I accomplished this by using its keypress event, and allowing only certain keys to be processed in the text box. However, I found that if I copy textual information from another location, and then paste it into the text box, the textbox accepts it just fine.

So, my question is, how do I prevent an access 2000 (or access XP), text box control from accepting paste commands?
 
Why not take a slightly different approach...audit the text box data using the "Before Update" event. In the example below, I'm expecting the field to be 3 numeric characters (if input):

Code:
Private Sub txt_DeptNbr_BeforeUpdate(Cancel As Integer)
Dim ErrMsg As String
If IsNull(Me!txt_DeptNbr) Or Me!txt_DeptNbr = "" Then Exit Sub

If (Len(Me!txt_DeptNbr)<>3)Or(IsNumeric(Me!txt_DeptNbr)= False) Then
    ErrMsg = &quot;Dept # must be 3 numerics (or left blank).&quot;
    MsgBox ErrMsg, vbExclamation, &quot;Invalid Dept # &quot;
    DoCmd.CancelEvent
    Exit Sub
End If
End Sub

This wouldn't solve the paste issue per se.... but would provide essentially the same data auditing requirement.
Regards- Ump 38
 
Too much work for what you want to do. Use properties of your text field and make it general number. Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
Setting properties alone to &quot;general number&quot; will definitely work and result in a generic Access error message when the user enters something other than numerics... which isn't always the easiest text for the user to interpret. If the numeric text field is fixed length, you may want to include setting of input mask for that field that will essentially disable anything other than numeric characters being entered.

My earlier suggestion... granted requiring a slight bit of work ... was intended to provide a little more straight-forward user feedback that clearly specifies the field requirements and eliminates guessing as to &quot;what did I do wrong?&quot;.
 
If the text box is bound to a table field, you should MASK and set a validation rule/text properties so you're covered all the time. This is where data validation belongs, for the most part, not at a form or module level.

If the text box is NOT bound, but is just collecting data for some other process, have you considered using a combo box instead? Combos (combeaux?) kinda make the wrong data entry harder to do....

Jim How many of you believe in telekinesis? Raise my hand...
Another free Access forum:
More Access stuff at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top