Paste labels value into active field

atreidies
atreidies used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
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.


Author

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 Liason
Most Valuable Expert 2012

Commented:
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?

JeffCoachman
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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.
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
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

Nerd
Most Valuable Expert 2012
Top Expert 2013
Commented:
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)
Jeffrey CoachmanMIS Liason
Most Valuable Expert 2012

Commented:
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.

Jeff

Author

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

Author

Commented:
Success: with this

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

Author

Commented:
Thanks Much...
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
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.

Author

Commented:
Naturally...thanks again

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial