Paste labels value into active field

My plan is for the user to place the cursor in the field of a subform and click the label located in the detail section of the main form to paste its value into the field. Ive never coded for the paste function.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Are you trying to place a label's caption into a textbox?

Thi code in the label's click event would be:
        Me.MyTextbox = Me.MyLabel.Caption

Open in new window

(You don't need code to 'copy/paste')

Also, this has to be done with a standalone label.  Labels that are associated/attached to other controls don't have their own click events.

atreidiesAuthor Commented:
Each label will be stand alone...that should work but how do i reference the active textbox as the destination as the cursor could be located in any number of fields?
Jeffrey CoachmanMIS LiasonCommented:
Not sure, but depending of how your forms are set up, you may lose focus on the subform control/field when you click in the main form Detail section.
(you may be able to store the value though...)

I am also curious as to why you need to "Paste" a value from a subform into a main form.

Can you explain a bit more about what you are trying to ultimately accomplish here?

Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

atreidiesAuthor Commented:
On the subform the user will place the cursor into a field then click a label to be pasted into the field.  I say paste becuase they may wish to put multiple values into the destination field. Mbizups comment would work if I could reference the field that either has the focus or just lost.  if it does lose the focus perhaps the code could go into the lost focus function to grab the active labels value.
So you want the user to click a textbox, then click a label and have the label's caption go into the previously clicked textbox?

You could do something like this -

Define a module level variable, right under "Option Explicit" in your forms code:

Dim mTargetControl as string

Open in new window

In the click event of any textbox you want this functionality for, 'remember' the control name like this:

mTargetControl =Me.ActiveControl.Name

Open in new window

Then in the click event of labels you want this functionality for:

Me.Controls(mTargetControl) = me.ActiveControl.Caption

Open in new window

Actually, scrap my last post completely.  That won't work.

The textbox remains the "Active Control" when a label is clicked, so you just need this in the label's click event:

Me.ActiveControl = Me.MyLabel.Caption

Open in new window

(You can refer to the textbox generically as the 'active control', but I thingk you have to specify the label by name)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeffrey CoachmanMIS LiasonCommented:
I was still curious as to why you need to Paste a value from a subform into a main form...

In any event, I will bow out now to avoid any confusion.

atreidiesAuthor Commented:
i tried your code as well as using a variable for the caption and same error

"Obejct doesnt support this propery or method"

Dim PasteData As String
PasteData = Me.Label5.Caption
Me.ActiveControl = PasteData

Open in new window

atreidiesAuthor Commented:
Success: with this

Dim PasteData As String
PasteData = Me.Label5.Caption
Screen.ActiveControl.Value = PasteData
atreidiesAuthor Commented:
Thanks Much...
Glad to help out. The issue was that I didnt take your subform into account, I think.

You should also add some form of error handling to that, in case the sequence of user clicks is not what you are expecting.
atreidiesAuthor Commented:
Naturally...thanks again
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.