Capture Ctrl+V to a new record

Shaimaa T

Registered User.
Local time
Today, 18:48
Joined
Aug 11, 2014
Messages
40
Hi all,

I want to handle the event when a user clicks Ctrl+C for a complete record in a datasheet then goes to a new record and presses

Ctrl+V.

I have added the following
Code:
Private Sub Text40_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 22
MsgBox "Please copy and paste the desired values one at a time."

End Select
End Sub



However, since this is for a complete selected record not for a single textbox , I do not know where to add this event (in the form

as a whole it would not work , and for a single control it would work only when selecting that control).

Also, I want to be able to handle the event of right clicking via the mouse and choosing Delete Record , or paste (that is ,

display a message to the user that he cannot delete a record , and cannot update all records at the same time- this is because the

underlying view contains multiple base tables so I wanted to pop up a user-friendly message instead of the database error message).
 
Shaimaa T, Welcome to AWF :)

Check out the Form Key events. Instead of the controls Key press. There are also other related events like Before Delete, Mouse Click etc.
 
pr2-eugin, Thank you :)

Actually, I have tried the Form's Key Press event handler which seems the most relevant and this did not trigger any event.

So I was wondering how to do it. I was just checking now the Detail event but this doesn't seem to work either.

Regarding the before delete , I will check it thank you :)

Would you please help me resolve the key issue ?
 
However, since this is for a complete selected record not for a single textbox , I do not know where to add this event (in the form
as a whole it would not work , and for a single control it would work only when selecting that control).

Also, I want to be able to handle the event of right clicking via the mouse and choosing Delete Record , or paste (that is ,
display a message to the user that he cannot delete a record ,...
First of all I think your main problem stems from the fact that you're using the wrong kind of form. If you don't want users to be able to copy and paste whole records then you want to use a Continuous Form. That way you don't need to trap for any key or mouse events.

In any case, Ctrl+V can be trapped by:
1. Set the Key Preview property of the form to Yes
2. Use the following code:
Code:
Private intKeyControl As Long

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If intKeyControl = vbKeyControl Then
        If KeyCode = vbKeyV Then
            KeyCode = 0    ' Nullify
            MsgBox "Can't paste"
        End If
    Else
        If KeyCode = vbKeyControl Then
            intKeyControl = KeyCode
        Else
            intKeyControl = 0
        End If
    End If
End Sub
... it would actually be better to nullify Ctrl+C instead of Ctrl+V so when they attempt to paste it just pastes nothing without any message.

As for the mouse trap, use one of the Mouse events.
 
Thanks for your response, vbaInet

Excuse me if I am new to this (the thing is that I am supporting a client's application so I did not create the application nor can I alter the original settings). As far as I know, the form can be viewable as a datasheet and as other forms but what is a continuous form and a non continuous one and how can this be specified?

Regarding the code section , I did not get this part

If KeyCode = vbKeyControl Then
intKeyControl = KeyCode
Else
intKeyControl = 0

Also intKeycontrol should be replaced with the Shift ? since the if condition won't be recognized otherwise?

Best Regards,
Shaimaa
 
Also intKeycontrol should be replaced with the Shift ? since the if condition won't be recognized otherwise?
And yes I forgot about the Shift key. Use a combination of the Shift argument (i.e. acCtrlMask) and the KeyCode (vbKeyV) in the Key Up event. Much neater that way.
 
The arguments in Key Down event are Key Code and Shift.

So we can use Shift instead of the intKeyControl?

I mean, you just declared a long variable - what will ever make the condition
If intKeyControl = vbKeyControl Then true?
 
I found that the Key Up won't work well if you use the Shift argument so use the Key Down event instead. Here it is:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If Shift = acCtrlMask And KeyCode = vbKeyV Then
        KeyCode = 0
        Shift = acShiftMask
        MsgBox "Can't paste"
    End If
End Sub
 
As soon as I press any key, the event is fired... it doesn't way for a Ctrl+C or +V so it never enters the If Condition

Any suggestions?
 
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = acCtrlMask And KeyCode = vbKeyV Then
KeyCode = 0
Shift = acShiftMask
MsgBox "Can't paste"
End If
End Sub

Form's Key Down after checking Key Preview to Yes.
 
If that's the only code you have in any of the Key Down/Key Up/Key Ascii events of the form and any of the controls, it's not possible for that to happen.
 
I want the application to wait till I press Ctrl then V before it fires the Key Down event and not as soon as I press any key . How can I handle this?
 
Shaimaa your question has been answered with solutions. Like I said in my last post, if you have any other code, that could be causing it.
 
Thank you vbaInet , I misread the previous post - sorry my mistake :)

It is working fine now ...will move to the deletion and mouse event handling
 
No problem.
But remember, users can still right-click and use the Paste menu option and you still need to code for the mouse event too.
 
The problem was that I put a breakpoint and on any key press I would jump straight away to the event handler without waiting for the next press after Ctrl.

When I did not add a breakpoint , I got the message "Can't paste"
 
The problem was that I put a breakpoint and on any key press I would jump straight away to the event handler without waiting for the next press after Ctrl.

When I did not add a breakpoint , I got the message "Can't paste"
Makes sense.
 

Users who are viewing this thread

Back
Top Bottom