acCmdCopy is not working

K

KiwiMan

Hi
I am using access 2003.

I need to copy the contents of a control (text box, called ID) from a form so
I can paste the contents of the field in another application. I am trying to
do this so whenever I move to a new record, using the Record Navigation
Buttons, the ID field is copied to the clipboard.

I am using the following:

Me!ID.SetFocus
DoCmd.RunCommand acCmdCopy

This seems to work fine when I use the code in a button or in a (On Got Focus)
or (On Lost Focus) procedures.
The problem is that when I use it in the (On Current) event of a Form, it
will give me the following error (to me it is equivalent to the blue screen
of death):
Run-Time error '2046':
The Command or Action 'Copy' isn't available now

I have tried so many things and been looking around on the MS website and
other independant forums without much luck. My questions are:
1) Why the code doesn't work through the (On Current) event only
2) Is there a way to solve the problem

Thank you in advance
 
D

Dale Fye

My guess is that you have the "Behavior Entering Field" property of the
database set to "Go to start of field" instead of "Select entire field". You
can change this property through the Options menu, Advanced, Editing in
Office 2007, if you want. Personally, I prefer the start of field option, so
in order to get it to work in the current event, you are going to actually
have to select the text in that field.

Try:

me.ID.SetFocus
me.ID.SelStart = 0
me.ID.SelLength = Len(me.ID)
DoCmd.RunCommand acCmdCopy
 
K

KiwiMan via AccessMonster.com

Thank you Dale

The "Behavior entering field" is actually set to "Select entire field". I
tried your suggestion but unfortunately it didn't work and gave the same
error message. I tried your code with a command button and it works fine. As
before, it is only when I put it in the "On Current" of a form that it
generates the error.

The problem is clearly related to the module it is in, and it doesn't work
with On Current. Probably because of the change of the record. It is strange
because the record is changed and the field is selected but Access can't copy
it. If I copy it manually it works. I wonder if it has something to do with
'automation'?

Any ideas?

Dale said:
My guess is that you have the "Behavior Entering Field" property of the
database set to "Go to start of field" instead of "Select entire field". You
can change this property through the Options menu, Advanced, Editing in
Office 2007, if you want. Personally, I prefer the start of field option, so
in order to get it to work in the current event, you are going to actually
have to select the text in that field.

Try:

me.ID.SetFocus
me.ID.SelStart = 0
me.ID.SelLength = Len(me.ID)
DoCmd.RunCommand acCmdCopy

----
HTH
Dale
Hi
I am using access 2003.
[quoted text clipped - 23 lines]
Thank you in advance
 
D

Dale Fye

KiwiMan,

I've done a little playing around, and I cannot tell you why it is doing
what it is, but I'm thinking it has to be some sort of a timing issue,
although I inserted a pause after the SelLength line and before the cmdCopy
and it still did not work. Interestingly, whenever I put a breakpoint in my
code, either before the Copy command or in the error handler, and then used
F5 to execute the rest of the code, it works fine.

Sorry I couldn't be more help. Would appreciate knowing what you figure out
when you do so.
----
Dale



KiwiMan via AccessMonster.com said:
Thank you Dale

The "Behavior entering field" is actually set to "Select entire field". I
tried your suggestion but unfortunately it didn't work and gave the same
error message. I tried your code with a command button and it works fine. As
before, it is only when I put it in the "On Current" of a form that it
generates the error.

The problem is clearly related to the module it is in, and it doesn't work
with On Current. Probably because of the change of the record. It is strange
because the record is changed and the field is selected but Access can't copy
it. If I copy it manually it works. I wonder if it has something to do with
'automation'?

Any ideas?

Dale said:
My guess is that you have the "Behavior Entering Field" property of the
database set to "Go to start of field" instead of "Select entire field". You
can change this property through the Options menu, Advanced, Editing in
Office 2007, if you want. Personally, I prefer the start of field option, so
in order to get it to work in the current event, you are going to actually
have to select the text in that field.

Try:

me.ID.SetFocus
me.ID.SelStart = 0
me.ID.SelLength = Len(me.ID)
DoCmd.RunCommand acCmdCopy

----
HTH
Dale
Hi
I am using access 2003.
[quoted text clipped - 23 lines]
Thank you in advance
 
K

KiwiMan via AccessMonster.com

Thank you Dale

At least I know now that it is not my computer or my version of Access. I
will keep trying. I will try to put a 'break' but I must find a way to
continue execution after that.

Will post if I resolve the issue.

Kiwiman

Dale said:
KiwiMan,

I've done a little playing around, and I cannot tell you why it is doing
what it is, but I'm thinking it has to be some sort of a timing issue,
although I inserted a pause after the SelLength line and before the cmdCopy
and it still did not work. Interestingly, whenever I put a breakpoint in my
code, either before the Copy command or in the error handler, and then used
F5 to execute the rest of the code, it works fine.

Sorry I couldn't be more help. Would appreciate knowing what you figure out
when you do so.
----
Dale
Thank you Dale
[quoted text clipped - 35 lines]
 
A

AccessVandal via AccessMonster.com

Current event will clear the setfocus, so you can't do a text copy.

I have test this code, seems to work on the current event.

Option Compare Database
Option Explicit
Dim stroldValue As String

Private Sub Form_Current()
If Not Me.NewRecord Then
stroldValue = Me.Text0.OldValue
End If
If Me.NewRecord Then
Me.Text0 = stroldValue
Me.Text0.SetFocus
DoCmd.RunCommand acCmdCopy
Me.Undo
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top