UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Minimizing Vba Coding Linking Form Button's Id To Code By The Button's Caption    
 
   
sjodom
post May 10 2011, 03:32 PM
Post#1



Posts: 8
Joined: 12-April 11



Hello,
I am currently designing a form that has several buttons, each with the same function/code, but differs only by the number associated with the button's caption. Is there any way to adjust the single line of code based off the unique caption of the button clicked without having to create duplicates of code?
Ex.
Button 1's Caption = "01"
Button 2's Caption = "02"
etc
Sub Button_Click()
'code
Selection_Number = "01" 'repeated in 'x' amount of subs for 'x' amount of unique button IDs
'code
End Sub
I am fairly new migrating to Access, with experience in VB from Excel. I assume there is a simple (easy) solution staring me in the face, but have not yet found it.
Any help would be appreciated! Thank you!
Go to the top of the page
 
theDBguy
post May 10 2011, 04:21 PM
Post#2


Access Wiki and Forums Moderator
Posts: 75,722
Joined: 19-June 07
From: SunnySandyEggo


Hi,
welcome2UA.gif
What version of Access are you using? Not sure I completely understand your question but for starters, you can move your common code out of the buttons' subroutines and place them in a separate subroutine. Then, you can just call the new subroutine from each button code.
Hope that helps...
Go to the top of the page
 
sjodom
post May 11 2011, 01:52 PM
Post#3



Posts: 8
Joined: 12-April 11



: ) Thank you for the welcome. I am using Access 2010.
I think I follow you, but I wonder if this could be simplified even further by a subroutine that would read the text off the button. My thought process is to minimize how many routine/subroutines actually exist. If I can call the same code each time without utilizing different codes to maintain later, this would be great. However, I don't fully know the limitations of Access yet.
I would want the code to look something like this (knowing full well it would not be written this way):
Selection_Number = thisForm.ActiveButton.Caption
Just to transmit the text to variable.
Again, thank you for your thoughts! I am looking to test it now.
Go to the top of the page
 
theDBguy
post May 11 2011, 01:57 PM
Post#4


Access Wiki and Forums Moderator
Posts: 75,722
Joined: 19-June 07
From: SunnySandyEggo


Hi,
ithout knowing your full intention, we might be going in circles. But in Access, you can refer to the Active control by using something like this:
Screen.ActiveControl
So to apply that to what I think you want to do, try:
Selection_Number = Screen.ActiveControl.Caption
Hope that helps...
Go to the top of the page
 
sjodom
post May 11 2011, 02:47 PM
Post#5



Posts: 8
Joined: 12-April 11



Oh that's an awesome function! And so simple!!
Thank you for interpreting solving this without me being able to fully explain what I am looking to do. You just saved me a whole lot of work now and years down the road.
Go to the top of the page
 
theDBguy
post May 11 2011, 02:49 PM
Post#6


Access Wiki and Forums Moderator
Posts: 75,722
Joined: 19-June 07
From: SunnySandyEggo


Hi,
Glad we could help. Good luck with your project.
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    18th July 2019 - 10:56 PM