Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing a CommandButton via code 1

Status
Not open for further replies.

BajanPOET

Programmer
Jul 12, 2002
194
BB
How can I access a CommandButton's properties etc via code? I want to disable or hide the command button based on a condition (tested by an IF-THEN-ELSE statement) but I don't know how to access the CommandButton1.enabled or .visible properties using code

What? Who? ME????
 



Hi,

What application?

If Excel, on a sheet or in a UserForm?

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
It's on a sheet in Excel... sorry about that...


What? Who? ME????
 



Code:
with YourSheet.Shapes("YourCommandObjectName").OleFormat.Object
  if [i]expression[/i] then
     .Visible = true
  else
     .Visible = false
  end if
end with


Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
How do I find out the name of YourCommandObjectName - would that be the CommandButton1 name? And YourSheet would be the mame of the sheet - LPO: so that I reference it by Sheets("LPO")?

What? Who? ME????
 


Select the Control Object and observe the name in the Name Box.

BTW, is this a Form control of a Control Toolbox control?

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
This is a Control Toolbox control. The name in the name box when I right click it and choose Properties is CommandButton1.

What? Who? ME????
 



For Control Toobox...
Code:
with CommandButton1
  if expression then
     .Visible = true
  else
     .Visible = false
  end if
end with


Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
While stepping through my code after adding the above code snippet, I realized when I hovered my mouse over the CommandButton1 after the focus had passed the "with CommandButton1" code that CommandButton1 as an object appeared empty! (CommandButton1 = empty)

I will try it and see if it works, but I think this means that the code doesn't recognize the commandbutton... let u guys know what happens in a bit.

What? Who? ME????
 
It's telling me "Object required" when it gets to the CommandButton's .Visible property. As I figured, it's not seeing the CommandButton.

What? Who? ME????
 



If the code is in a MODULE, then the command button needs a Sheet Object as the parent reference...
Code:
with YourSheetObject.CommandButton1
  if expression then
     .Visible = true
  else
     .Visible = false
  end if
end with


Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top