Using a variable to provide a control name

  • Thread starter Thread starter Waterman
  • Start date Start date
W

Waterman

This one is probably really simple.

The form I have only has two command buttons on it. The
code below will allow me to change the fore color of
command1 by clicking command0.

Private Sub Command0_Click()
Dim btnname As CommandButton
Set btnname = Command1
btnname.ForeColor = vbRed
End Sub

What I am trying to accomplish is use a string variable to
allow me to state the name of the command button.

Private Sub Command0_Click()
Dim btnname As CommandButton
Dim strName as string
strName = "Command1"
Set btnname = strName
btnname.ForeColor = vbRed
End Sub

The error I'm getting is "type mismatch". It sure seems
like a simply enough thing to accomplish, but I have to
admit that I'm stumped. Can you help?
 
Waterman said:
This one is probably really simple.

The form I have only has two command buttons on it. The
code below will allow me to change the fore color of
command1 by clicking command0.

Private Sub Command0_Click()
Dim btnname As CommandButton
Set btnname = Command1
btnname.ForeColor = vbRed
End Sub

What I am trying to accomplish is use a string variable to
allow me to state the name of the command button.

Private Sub Command0_Click()
Dim btnname As CommandButton
Dim strName as string
strName = "Command1"
Set btnname = strName
btnname.ForeColor = vbRed
End Sub

The error I'm getting is "type mismatch". It sure seems
like a simply enough thing to accomplish, but I have to
admit that I'm stumped. Can you help?

Set btnname = Me.Controls(strName)

Or just

Private Sub Command0_Click()
Dim strName as String
strName = "Command1"
Me.Controls(strName).ForeColor = vbRed
End Sub
 
-----Original Message-----


Set btnname = Me.Controls(strName)

Or just

Private Sub Command0_Click()
Dim strName as String
strName = "Command1"
Me.Controls(strName).ForeColor = vbRed
End Sub

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


Wonderfully simple. Thanks, Dirk!
 
Waterman said:
The form I have only has two command buttons on it. The
code below will allow me to change the fore color of
command1 by clicking command0.

Private Sub Command0_Click()
Dim btnname As CommandButton
Set btnname = Command1
btnname.ForeColor = vbRed
End Sub

What I am trying to accomplish is use a string variable to
allow me to state the name of the command button.

Waterman,

here's some code:

Private Sub Command0_Click()
Dim btnname As CommandButton
Dim strName as string
strName = "Command1"

Me.Controls(strName).ForeColor = vbRed
End Sub

Best regards
Emilia

Emilia Maxim
PC-SoftwareService, Stuttgart
http://www.maxim-software-service.de
 
Back
Top