Solved

How do I prevent pasting from the Clipboard into a Text Box?

Posted on 2008-10-30
24
257 Views
Last Modified: 2013-11-28
I am trying to prevent end-users from using the copy/paste function to enter data into an MS Access text box.

How can this be accomplished?
0
Comment
Question by:colin4
  • 11
  • 6
  • 4
  • +2
24 Comments
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility

  I'm not sure that you can but you should be able to detect if they do.

Method 1:
  I don't know if OnChange would get fired once for a paste or for each character.  if once, then you could set an internal counter for the number of characters, increment in the OnChange as they typed.  In the OnChange, you'd also check to see if the len of the control is > your internal count.  If so, they pasted.

Method 2:
  Somewhat like one, but use the Kepress event to keep track of the character count and OnChange to detect if the value in the control is longer then the count.

  Wait a bit and see if anyone jumps in, but that's the only thing that comes to mind right off...

JimD.
0
 
LVL 14

Expert Comment

by:pteranodon72
Comment Utility
To prevent pasting, you need to remove the default menus on the form and context menu for the control.  You can replace them with limited versions, that include only the functions you want, or just remove them altogether if you don't need any of the built-in functionality.

HTH,

pteranodon
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility

  What about Ctrl/V?

JimD.
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 125 total points
Comment Utility
colin4,

The issue here is that Access makes no clear distiction between what is "Pasted" into a text box and what is "manually entered" into that same textbox.

With that in mind, you can use brute force and clear the windows clipboard when you enter the control, thus leaving you with nothing to paste.:

Code from here:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=205

Put this in a module:

Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
 
Public Function ClearClipboard()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
End Function


Then call this function from the "Enter" event of the textbox:

Private Sub YourtextBox_Enter()
    Call ClearClipboard
End Sub

Works for me!

JeffCoachman
0
 

Author Closing Comment

by:colin4
Comment Utility
Thanks for the quick and excellent response.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
;-)
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
Jeff,

  I don't see how that's going to work in all cases.  If I enter the control, then go and copy and paste, I'm going to be able to do that.

JimD.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
Jim,

I tested it, and it seemed to prevent Pasting, regardless of the method.

Because as soon as you *Enter* the control (Clicking in a field or tabbing into it, will trigger the OnEnter Event), the clipboard is cleared.
So when I tried: Copy-->Paste or Ctrl-C-->Cntrl-V, or used the Right Click, I could not paste.

You got different results when you tried it?
Or did I mis-understand something?

JeffCoachman
0
 
LVL 10

Expert Comment

by:calpurnia
Comment Utility
But if you enter the field and *then* go and copy something from elsewhere, returning the focus to the database window doesn't trigger the OnEnter event again...
0
 

Author Comment

by:colin4
Comment Utility
Thanks, JDettman, you are right.

The above solution will NOT work in all cases.  I'm afraid I was so happy to find a solution, I didn't thoroughly test the results.  I'd like to give Genius another chance to come up with a final solution, so here are the details.

By adding the CALL statement to the "On Key Down", "On Enter" and "On Key Click", I can trap everything EXCEPT the Right-Click Context Menu.  So, if you're already in the text box, copy something to the clipboard and then hit Ctrl+V, the clipboard will be emptied.  However, if you Right-Click, the Context Menu pops-up with a valid Paste option.

All I need now is a way of suppressing the Context Menu from popping up through a right-click.

Any ideas?
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
To All,

Yes you are correct, but I don't know if:
Entering the field, then going somewhere else, then copying, then returning to the field, then pasting...
Was a realistic scenario.
;-)

I never said my solution was perfect.
You can punch holes in any solution here.

I clearly stated that my solution was "Brute force".

In case no other solution was presented.
Or the asker need something as simple as this.

As we all know, every possible scenario can never be accounted for.

;-)

Jeff
0
 

Author Comment

by:colin4
Comment Utility
Thanks for your help boag2000.

I didn't intend to criticize you or minimize the effectiveness of your solution by adding my follow-up comments above.

Nobody expects perfection in this field, and you've done a great job in helping me resolve my issue.

As to the one remaining problem, I did find a way of suppressing the righ-click context menu for the form in question.  Simply selecting "No" for the "Shortcut Menu" property did the trick.  Now, no matter how the end-user tries to execute a paste operation, the process is blocked.

Pasting in now completely disabled in the TextBox.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
colin4,

You can disable the right click by creating your own right click menu and not include Cut Copy or Paste. (I usually just include  Sort ASC and Sort DESC)
Then in the startup options, set your custom right-click as the default.
(If you need help with this, let me/us know)

Of course, in the interest of full disclosure I must say that:
"This is not perfect"
;-)

Because a user can hold down shift and bypass this setting.

Then we have to talk about MDE files, User Level security, and Runtime alternatives to make this more fullproof.

Again no system will be 100 percent fullproof.
For example, I am sure you can totally disable the clipboard altogether while this databse is open. This would assure that a user could never paste, it would also turn off copy and pasete for all other apps as well.
You could call the ClearCliboard code form everysingle event on every single object and event in the database, but this would limit you ability to copy and paste were it wis needed.
 
The more restrictive you make it for one situation, the more it "bleeds over" into other parts of your app where this functionality is not desired.

;-)

Jeff
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
colin4,


" I did find a way of suppressing the righ-click context menu for the form in question.  Simply selecting "No" for the "Shortcut Menu" property did the trick"

Do you mean unchecking the box called: "Allow Default Shortcut menus"?
If so like I said, this can be easilly bypassed by holding down shift, when opening the DB.
(And most users already know this)

See what I mean?

;-)

Jeff
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
colin4,

Please bear in mind that you never actually told us WHY you need this?

What are users copying that you don't want pasted?
;-)

Perhaps there may be a better way besides trying to disable pasting.

Jeff
0
 

Author Comment

by:colin4
Comment Utility
Sorry.

The Access form in question includes a TextBox intended to contain descriptive text.

The data from the Access form is stored in a SQL Server 2000 back-end.  The SQL Server 2000 back-end database exports delimited text files representing the data to a UNIX server for later use.  

A probelm arose when the data-entry operator began cutting description text directly into the TextBox from Web sites that included embedded carriage returns.  The SQL Server export process passed the embedded carriage returns into the delimited text files.  Since each row of the pipe-delimited text file represents a record, the embedded carriage returns wreaked havoc when the files were then imported into other systems.

The quickest solution was to inhibit paste operations in the Access front-end GUI since the TextBox already validates the text to prevent carriage returns..
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
Not to muddy the waters,...

But what if you wrote a simpe sub to strip out the carrige returns from the Web text?

Woud disabling the paste command even be needed?
:-O

Jeff
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
<<A probelm arose when the data-entry operator began cutting description text directly into the TextBox from Web sites that included embedded carriage returns. >>

  I think Jeff's approach to your problem is a better one and I'll tell you why;  You can't always control the source of the information.

 Lately, one of my clients has started receiving PO data containing a carriage return or other non-visible characters.  They also had manual entry of orders and occasionally someone will cut and paste and pickup something they shouldn't.  While I could handle the manual entry through the form, I could not control the PO data.

  Since this is SQL Server,  I would think about using a trigger to scan the field and remove anything other then a alphabetic character.

FWIW,
JimD.
0
 
LVL 10

Expert Comment

by:calpurnia
Comment Utility
I can also imagine the poor data entry person might be rather frustrated at having to retype everything, rather than being able to cut and paste where appropriate.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
colin4,

Here is some fairly basic code to remove Carrige Returns from a string:

    Replace([YourString], Chr(13), "")

JeffCoachman
0
 

Author Comment

by:colin4
Comment Utility
Thanks to all of you for your suggestions.

For now, preventing pasting is an acceptable solution for everyone involved.  The TextBox data tended to be unnecessarily long when you could simply paste "anything" into it and now requires more thought on the part of the operator.

For future modifications, I will remember your suggestions for removing specific characters from a string.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
colin4,

More importantly is that you state the "actual" issue.
Which was in this case: "How to remove carrige Returns from a string".

Not really "Preventing pasting"

Because had you removed the carrige returns, there would not be an issue after that.
:-)

JeffCoachman
0
 

Author Comment

by:colin4
Comment Utility
Hi Jeff,

Carriage returns are not the ONLY issue, since HTML contains numerous special control characters.  It just happens that carriage returns were the most obvious problem.  I was simply providing an example.

Pasting HTML directly from web sites into the Textbox is truly the problem.  Writing code to trap every conceivable HTML control code would be an arduous task at best.  Preventing pasting is the solution.

No, the request was not mischaracterized.

Thanks, again, for your help.

0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
OK

Thanks for the clarification

;-)

Jeff
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now