Best way to transfer variables between functions

  • Thread starter Thread starter tobesurveyor via AccessMonster.com
  • Start date Start date
T

tobesurveyor via AccessMonster.com

Good Morning,
I am a novice when it comes to VBA. I have made a couple of applications
using the same methods of using VBA. I am now going back and trying to make
things quicker as well as trying to clean up my code.

Is there a way to pass variables between functions in code?
Right now I have an OnClick event to call Launch01 thru Launch14 based upon
the image they click on the form. When they click on of these, the area
clicked highlights. I want that highlight color to change based upon an
option in a table called tblChoiceMenuColor. Unfortunately I do not know how
to transfer the variable solved for in the Private Sub FindColor for the
variable ColorCode to each Me.bxLaunch##.BackColor in each OnClick event.
Please see below.

Private Sub FindColor()

ColorNameID = DLookup("[ChoiceMenuColorID]", "tblLocalSettings",
"[LocalSettingsID] = 1")
ColorImgName = DLookup("[FileName]", "tblChoiceMenuColor", "
[ChoiceMenuColorID] = " &
ColorNameID)
ColorCode = DLookup("[ColorCode]","tblChoiceMenuColor","
[ChoiceMenuColorID] = "& ColorNameID)
End Sub

Private Sub Launch01()
FindColor
Me.bxLaunch01.BackStyle = 1
Me.bxLaunch01.BackColor = 16250335 <<<<I want to pass the variable
ColorCode Here
End Sub

Private Sub Launch02()
FindColor
Me.bxLaunch02.BackStyle = 1
Me.bxLaunch01.BackColor = 10485760 <<<<I want to pass the variable
ColorCode Here
End Sub

Any help would be greatly appreciated.
Thanks,
CF
 
Make FindColor a function rather than a sub:

Function FindColor()
ColorNameID = DLookup("[ChoiceMenuColorID]", "tblLocalSettings",
"[LocalSettingsID] = 1")
ColorImgName = DLookup("[FileName]", "tblChoiceMenuColor", "
[ChoiceMenuColorID] = " &
ColorNameID)
ColorCode = DLookup("[ColorCode]","tblChoiceMenuColor","
[ChoiceMenuColorID] = "& ColorNameID)

FindColor = ColorCode 'Comment: Since it's this code you want to pass back
to sub
End Function


Now in the Launch codes:

Private Sub Launch01()
colorIwant = FindColor
Me.bxLaunch01.BackStyle = 1
Me.bxLaunch01.BackColor = ColorIWant
End Sub

Since you've got multiple launch codes I would declare the Variable
ColorIWant as (at minimum) Public and would set the Function Findcolor
outside of the Form's code

BAC


tobesurveyor via AccessMonster.com said:
Good Morning,
I am a novice when it comes to VBA. I have made a couple of applications
using the same methods of using VBA. I am now going back and trying to make
things quicker as well as trying to clean up my code.

Is there a way to pass variables between functions in code?
Right now I have an OnClick event to call Launch01 thru Launch14 based upon
the image they click on the form. When they click on of these, the area
clicked highlights. I want that highlight color to change based upon an
option in a table called tblChoiceMenuColor. Unfortunately I do not know how
to transfer the variable solved for in the Private Sub FindColor for the
variable ColorCode to each Me.bxLaunch##.BackColor in each OnClick event.
Please see below.

Private Sub FindColor()

ColorNameID = DLookup("[ChoiceMenuColorID]", "tblLocalSettings",
"[LocalSettingsID] = 1")
ColorImgName = DLookup("[FileName]", "tblChoiceMenuColor", "
[ChoiceMenuColorID] = " &
ColorNameID)
ColorCode = DLookup("[ColorCode]","tblChoiceMenuColor","
[ChoiceMenuColorID] = "& ColorNameID)
End Sub

Private Sub Launch01()
FindColor
Me.bxLaunch01.BackStyle = 1
Me.bxLaunch01.BackColor = 16250335 <<<<I want to pass the variable
ColorCode Here
End Sub

Private Sub Launch02()
FindColor
Me.bxLaunch02.BackStyle = 1
Me.bxLaunch01.BackColor = 10485760 <<<<I want to pass the variable
ColorCode Here
End Sub

Any help would be greatly appreciated.
Thanks,
CF
 
Back
Top