Run VBA code saved as string

  • Thread starter Thread starter undrline via AccessMonster.com
  • Start date Start date
U

undrline via AccessMonster.com

I need to convert it back from a string into code. I'm a newbie - so I'm
really looking for a "how-to."

Function mouseover(buttonname)
Dim buttonname2 As String

buttonname2 = "Me." & buttonname & "a.Visible"
buttonname = "Me." & buttonname & ".Visible"

' BROKEN PART . . . the string is not equal to true or false, it is equal to
the string value
If buttonname = True Then
buttonname = False
buttonname2 = True
End If

End Function
 
Instead of a string, why not use control references.

Dim ctlButtonA as Control
Dim ctlButton As Control

Set ctlButtonA = Me.Controls(ButtonName & "A")
Set ctlButton = Me.Controls(ButtonName)

If ctlButton = True Then
ctlButton = False
ctlButtonA = True
End If
 
Thanks for the reply. I am trying to control the visibility of images
(graphic button images) as a mouseOver. So, the mousemove event will hide
the visibility of one image, and show the visibility of the other. All the
images sit on top of another image. I want this image to have a mousemove
event that toggles them back the way they were (if you move your mouse off of
the button image, you have to move on to the background image, and it reverts)
.. This is my mouseOut. Because I don't want to create a whole series of
"if" statements for each button, that I then have to modify if I ever add
buttons, I figured I could make a function. So, I figured I'd tie it to a
naming convention. mouseover = button1, mouseout = button1a, mouseover =
button2, mouseout = button2a, and so forth. So, in building the mouseout
function, if I ever have to add more "buttons" I'll only have to worry about
one event.

Since an image isn't a control, will this still work? Does setting a control
to true/false show/hide it?



John said:
Instead of a string, why not use control references.

Dim ctlButtonA as Control
Dim ctlButton As Control

Set ctlButtonA = Me.Controls(ButtonName & "A")
Set ctlButton = Me.Controls(ButtonName)

If ctlButton = True Then
ctlButton = False
ctlButtonA = True
End If
I need to convert it back from a string into code. I'm a newbie - so I'm
really looking for a "how-to."
[quoted text clipped - 14 lines]
End Function
 
PMFJI, but while an image may not be a control, you have to put it in a
control to display it.
 
PMFJI = Pardon me for jumping in.

So your control has an image? If so, set the visible property of the
control to true or false

Dim ctlButtonA as Control
Dim ctlButton As Control

Set ctlButtonA = Me.Controls(ButtonName & "A")
Set ctlButton = Me.Controls(ButtonName)

If ctlButton.visible = True Then
ctlButton.Visible = False
ctlButtonA.Visible = True
End If

You should be able to just do the following which may be simpler.

Me.Controls(ButtonName).Visible = False
Me.Controls(ButtonName & "A").Visible = True
 
Thanks for the definition. Thank you for the help. I'm worried about a few
things related to the main variable; I feel like it should be declared
globally somehow. I have four places the mouse pointer could already be when
the forms opens: on the button, on another button, on the image placemat, or
outside the image placemat. I can see "order" problems occurring. Here's
what I'm about to try . . . I have two events and two functions:



FUNCTION - MOUSEOVER

Function mouseover(buttonname)
Dim buttonname As Control

If Me.Controls(buttonname).Visible = True Then
Me.Controls(buttonname).Visible = False
Me.Controls(buttonname & "a").Visible = True
End If

End Function



FUNCTION - MOUSEOUT

Function mouseout(buttonname)

If Me.Controls(buttonname).Visible = False Then
Me.Controls(buttonname).Visible = True
Me.Controls(buttonname & "a").Visible = False
End If

End Function


MOUSEMOVE - OVER button1 IMAGE

Private Sub CreateAudit_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
' CreateAudit is a transparent button on top of the image.
' Native buttons (versus image buttons) were already aligned properly and had
onClick events
' So, I just made them transparent and brought them to the front.

Dim str_buttonname As String

str_buttonname = "button1"
mouseover (str_buttonname)

End Sub


MOUSEMOVE - OVER IMAGE PLACEMAT

Private Sub Image8_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)

mouseout (buttonname)

End Sub




John said:
PMFJI = Pardon me for jumping in.

So your control has an image? If so, set the visible property of the
control to true or false

Dim ctlButtonA as Control
Dim ctlButton As Control

Set ctlButtonA = Me.Controls(ButtonName & "A")
Set ctlButton = Me.Controls(ButtonName)

If ctlButton.visible = True Then
ctlButton.Visible = False
ctlButtonA.Visible = True
End If

You should be able to just do the following which may be simpler.

Me.Controls(ButtonName).Visible = False
Me.Controls(ButtonName & "A").Visible = True
[quoted text clipped - 4 lines]
 
Yes, it doesn't like how I'm passing my variables . . .

Thanks for the definition. Thank you for the help. I'm worried about a few
things related to the main variable; I feel like it should be declared
globally somehow. I have four places the mouse pointer could already be when
the forms opens: on the button, on another button, on the image placemat, or
outside the image placemat. I can see "order" problems occurring. Here's
what I'm about to try . . . I have two events and two functions:

FUNCTION - MOUSEOVER

Function mouseover(buttonname)
Dim buttonname As Control

If Me.Controls(buttonname).Visible = True Then
Me.Controls(buttonname).Visible = False
Me.Controls(buttonname & "a").Visible = True
End If

End Function

FUNCTION - MOUSEOUT

Function mouseout(buttonname)

If Me.Controls(buttonname).Visible = False Then
Me.Controls(buttonname).Visible = True
Me.Controls(buttonname & "a").Visible = False
End If

End Function

MOUSEMOVE - OVER button1 IMAGE

Private Sub CreateAudit_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
' CreateAudit is a transparent button on top of the image.
' Native buttons (versus image buttons) were already aligned properly and had
onClick events
' So, I just made them transparent and brought them to the front.

Dim str_buttonname As String

str_buttonname = "button1"
mouseover (str_buttonname)

End Sub

MOUSEMOVE - OVER IMAGE PLACEMAT

Private Sub Image8_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)

mouseout (buttonname)

End Sub
PMFJI = Pardon me for jumping in.
[quoted text clipped - 22 lines]
 
New version below. Let's me do the mouseover, but not the mouseout. No
error prompt. I'm guessing that it's not saving "buttonname" for use in
mouseout(buttonname). If that is what I'm doing wrong, how do I get it to
remember "buttonname?" Otherwise, what am I doing wrong?

Thanks for continuing to help.



Private Sub Form_Open(Cancel As Integer)
' FORM OPENS
Dim buttonname As Control
End Sub

Function mouseover(buttonname)
' FUNCTION MOUSEOVER
If Me.Controls(buttonname).Visible = True Then
Me.Controls(buttonname).Visible = False
Me.Controls(buttonname & "a").Visible = True
End If
End Function

Function mouseout(buttonname)
' FUNCTION MOUSEOUT
If Me.Controls(buttonname).Visible = False Then
Me.Controls(buttonname).Visible = True
Me.Controls(buttonname & "a").Visible = False
End If
End Function

Private Sub CreateAudit_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
' MOUSEMOVE BUTTON
buttonname = "button1"
mouseover (buttonname)
End Sub

Private Sub Image8_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
' MOUSEMOVE IMAGE PLACEMAT
mouseout (buttonname)
End Sub



Yes, it doesn't like how I'm passing my variables . . .
Thanks for the definition. Thank you for the help. I'm worried about a few
things related to the main variable; I feel like it should be declared
[quoted text clipped - 56 lines]
 
Well, first of all don't dim ButtonName as a control. You should Dim it as
a string.
Second, dim it at the top of the class module, so it will be available
throughout the module and will retain its value.

Option Explicit
Dim strButtonName as String

Function MouseOver(ButtonName)
'Set strButtonName to the current button name for future use
strButtonName = ButtonName

If Me.Controls(buttonname).Visible = True Then
Me.Controls(buttonname).Visible = False
Me.Controls(buttonname & "a").Visible = True
End If

End Function

Function MouseOut()
' FUNCTION MOUSEOUT
If Me.Controls(strButtonName).Visible = False Then
Me.Controls(strbuttonname).Visible = True
Me.Controls(strbuttonname & "a").Visible = False
End If
End Function


undrline via AccessMonster.com said:
New version below. Let's me do the mouseover, but not the mouseout. No
error prompt. I'm guessing that it's not saving "buttonname" for use in
mouseout(buttonname). If that is what I'm doing wrong, how do I get it to
remember "buttonname?" Otherwise, what am I doing wrong?

Thanks for continuing to help.



Private Sub Form_Open(Cancel As Integer)
' FORM OPENS
Dim buttonname As Control
End Sub

Function mouseover(buttonname)
' FUNCTION MOUSEOVER
If Me.Controls(buttonname).Visible = True Then
Me.Controls(buttonname).Visible = False
Me.Controls(buttonname & "a").Visible = True
End If
End Function

Function mouseout(buttonname)
' FUNCTION MOUSEOUT
If Me.Controls(buttonname).Visible = False Then
Me.Controls(buttonname).Visible = True
Me.Controls(buttonname & "a").Visible = False
End If
End Function

Private Sub CreateAudit_MouseMove(Button As Integer, Shift As Integer, X
As
Single, Y As Single)
' MOUSEMOVE BUTTON
buttonname = "button1"
mouseover (buttonname)
End Sub

Private Sub Image8_MouseMove(Button As Integer, Shift As Integer, X As
Single,
Y As Single)
' MOUSEMOVE IMAGE PLACEMAT
mouseout (buttonname)
End Sub



Yes, it doesn't like how I'm passing my variables . . .
Thanks for the definition. Thank you for the help. I'm worried about a
few
things related to the main variable; I feel like it should be declared
[quoted text clipped - 56 lines]
control
to true/false show/hide it?
 
I adapted your version, and it worked. Thank you!

Here was the final, with the adaptations:

Option Compare Database
Dim strButtonName As String
' GLOBAL DECLARATIONS (previously, I had tried this, As Control, and it
didn't work; Explicit is not necessary)

Private Sub Form_Open(Cancel As Integer)
' OPEN FORM (give it something to chew on, until strButtonName has an actual
value)
strButtonName = ""
End Sub

Private Sub Image8_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
' IMAGE PLACEMAT
If strButtonName <> "" Then
mouseOut (strButtonName)
End If
End Sub

Private Sub CreateAudit_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
' BUTTON
strButtonName = "button1"
mouseOver (strButtonName)
End Sub

Private Sub OpenCreateFile_MouseMove(Button As Integer, Shift As Integer, X
As Single, Y As Single)
' ANOTHER BUTTON
strButtonName = "button2"
mouseOver (strButtonName)
End Sub

Function mouseOver(strButtonName)
' MOUSEOVER
If Me.Controls(strButtonName).Visible = True Then
Me.Controls(strButtonName).Visible = False
Me.Controls(strButtonName & "a").Visible = True
End If
End Function

Function mouseOut(strButtonName)
' MOUSEOUT
If Me.Controls(strButtonName).Visible = False Then
Me.Controls(strButtonName).Visible = True
Me.Controls(strButtonName & "a").Visible = False
End If
End Function





John said:
Well, first of all don't dim ButtonName as a control. You should Dim it as
a string.
Second, dim it at the top of the class module, so it will be available
throughout the module and will retain its value.

Option Explicit
Dim strButtonName as String

Function MouseOver(ButtonName)
'Set strButtonName to the current button name for future use
strButtonName = ButtonName

If Me.Controls(buttonname).Visible = True Then
Me.Controls(buttonname).Visible = False
Me.Controls(buttonname & "a").Visible = True
End If

End Function

Function MouseOut()
' FUNCTION MOUSEOUT
If Me.Controls(strButtonName).Visible = False Then
Me.Controls(strbuttonname).Visible = True
Me.Controls(strbuttonname & "a").Visible = False
End If
End Function
New version below. Let's me do the mouseover, but not the mouseout. No
error prompt. I'm guessing that it's not saving "buttonname" for use in
[quoted text clipped - 47 lines]
 
Option Explicit is not necessary, BUT it is a very good idea to use it.

It keeps you from trying to use the wrong type of data in arguments and it
also allows you to find typing errors. I always use it and it catches a lot
of my typos when I enter things like stButtonName instead of strButtonName.


undrline via AccessMonster.com said:
I adapted your version, and it worked. Thank you!

Here was the final, with the adaptations:

Option Compare Database
Dim strButtonName As String
' GLOBAL DECLARATIONS (previously, I had tried this, As Control, and it
didn't work; Explicit is not necessary)

Private Sub Form_Open(Cancel As Integer)
' OPEN FORM (give it something to chew on, until strButtonName has an
actual
value)
strButtonName = ""
End Sub

Private Sub Image8_MouseMove(Button As Integer, Shift As Integer, X As
Single,
Y As Single)
' IMAGE PLACEMAT
If strButtonName <> "" Then
mouseOut (strButtonName)
End If
End Sub

Private Sub CreateAudit_MouseMove(Button As Integer, Shift As Integer, X
As
Single, Y As Single)
' BUTTON
strButtonName = "button1"
mouseOver (strButtonName)
End Sub

Private Sub OpenCreateFile_MouseMove(Button As Integer, Shift As Integer,
X
As Single, Y As Single)
' ANOTHER BUTTON
strButtonName = "button2"
mouseOver (strButtonName)
End Sub

Function mouseOver(strButtonName)
' MOUSEOVER
If Me.Controls(strButtonName).Visible = True Then
Me.Controls(strButtonName).Visible = False
Me.Controls(strButtonName & "a").Visible = True
End If
End Function

Function mouseOut(strButtonName)
' MOUSEOUT
If Me.Controls(strButtonName).Visible = False Then
Me.Controls(strButtonName).Visible = True
Me.Controls(strButtonName & "a").Visible = False
End If
End Function





John said:
Well, first of all don't dim ButtonName as a control. You should Dim it
as
a string.
Second, dim it at the top of the class module, so it will be available
throughout the module and will retain its value.

Option Explicit
Dim strButtonName as String

Function MouseOver(ButtonName)
'Set strButtonName to the current button name for future use
strButtonName = ButtonName

If Me.Controls(buttonname).Visible = True Then
Me.Controls(buttonname).Visible = False
Me.Controls(buttonname & "a").Visible = True
End If

End Function

Function MouseOut()
' FUNCTION MOUSEOUT
If Me.Controls(strButtonName).Visible = False Then
Me.Controls(strbuttonname).Visible = True
Me.Controls(strbuttonname & "a").Visible = False
End If
End Function
New version below. Let's me do the mouseover, but not the mouseout. No
error prompt. I'm guessing that it's not saving "buttonname" for use in
[quoted text clipped - 47 lines]
control
to true/false show/hide it?
 
I did have to look it up to find out what it does, and it does sound like a
good idea, you've helped to widen my scope.

Thank you, and everyone, for all your help.



John said:
Option Explicit is not necessary, BUT it is a very good idea to use it.

It keeps you from trying to use the wrong type of data in arguments and it
also allows you to find typing errors. I always use it and it catches a lot
of my typos when I enter things like stButtonName instead of strButtonName.
I adapted your version, and it worked. Thank you!
[quoted text clipped - 86 lines]
 
Back
Top