Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
refer to a form control in vba
Gary Becker 
     
3 years ago
Is there a way to use the OnClick event of a control, to refer to it in VBA without using its name? I want to make a sub or function for the OnClick event. The user clicks on the control, VBA then sets the focus and copies the contents to the clipboard. So can I use something like Me!Currentcontrol.setfocus instead of using the control name like Me!CurrentAmount.setfocus?
Gary Becker OP  @Reply  
     
3 years ago
I kept trying things. And I think I figured it out. Instead of saying Me!CurrentControl I need to use JUST ActiveControl. That seems to work.
Gary Becker OP  @Reply  
     
3 years ago
This is what my function turned out to be. I pasted this in the On Click event of a bunch of fields so it will copy the value to the clipboard to paste into my Excel worksheets

Private Function MyCopy()
    
    If IsNull(ActiveControl) Then Exit Function
    
    ActiveControl.SetFocus
    ActiveControl.SelStart = 0
    ActiveControl.SelLength = Len(ActiveControl.Text)
    DoCmd.RunCommand acCmdCopy

End Function
Kevin Robertson  @Reply  
          
3 years ago
Add the function to a global module so the entire database can use it.

Public Function MyCopy()
    
    Dim Ctrl As Control
    
    Set Ctrl = Screen.ActiveForm.ActiveControl
    If IsNull(Ctrl) Then Exit Function
    
    Ctrl.SetFocus
    Ctrl.SelStart = 0
    Ctrl.SelLength = Len(Ctrl.Text)
    DoCmd.RunCommand acCmdCopy
    
    Set Ctrl = Nothing
    
End Function


See Event Handler

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/6/2026 10:18:22 PM. PLT: 1s