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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy One? Dimming Controls 2

Status
Not open for further replies.

Aeneas

Programmer
Sep 18, 2003
25
CA
Dim txtFirstName as TextBox

Set txtFirstName = UserForm1.TextBox1



Why doesn't this work? I've also tried using some vbObject type of stuff here, but I am fairly unfamiliar - is there an easy syntax fix for this?

Thanks again!

Cheers, Tom
 
If UserForm1 is a declared form object then your syntax should work.

If UserForm1 is the form where the code is placed then use

Set txtFirstName = Me.TextBox1

If UserForm1 is another form then use

Set txtFirstName = Forms("UserForm1").TextBox1

In this last case, UserForm1 must be open.
 
Well, I am in VBA, in UserForm1's code window, and whether I just use TextBox1 or Me.TextBox1 or UserForm1.TextBox1 it gives me a type mismatch.

Exactly:

Private Sub CommandButton2_Click()

Dim txtAction As TextBox


Set txtAction = Me.TextBox1


End Sub
 
I can get the same error if TextBox1 is not a text box. Are you sure that TextBox1 is a text box?
 
Try this:

Code:
Private Sub CommandButton2_Click()
Dim txtAction As [COLOR=red yellow]Control[/color]
Set txtAction = Me.TextBox1
txtAction.SetFocus [COLOR=green]'just to test[/color]
End Sub



Peace!! [americanflag] [peace] [americanflag]

Mike

Didn't get the answers that you wanted? Take a look at FAQ219-2884
 
Although, I am still confused as to why you would want to do this . . . Please explain.

Why not just rename TextBox1 to txtAction at design time?


Peace!! [americanflag] [peace] [americanflag]

Mike

Didn't get the answers that you wanted? Take a look at FAQ219-2884
 
Hi Aeneas,

A TextBox on a UserForm is type MSForms.TextBox. A standalone Textbox on a sheet, taken from the Control Toolbox, is type Textbox, so try changing your code to ..

Code:
[blue]Dim txtFirstName as [red]MSForms.[/red]TextBox

Set txtFirstName = UserForm1.TextBox1[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Wow, this works. So simple. MS so butt-backwards sometime - TextBox object doesn't work, but Control object does.


Anyway, aside from going into the details of my design, I have come across many instances where it would be useful to loop through controls without using 'for each ctrl in UserForm1' due to the need to group certain controls together to test them specifically (if textbox(n+4) > 1; this would fail for textual textboxes).

Now I can have a function that sets the textbox numbers up:

Function(byval k as integer, txt<name1-4> as control)

if input1 = 1 then
set txt<name1> = textbox1
set txt<name2> = textbox2
set txt<name3> = textbox3
set txt<name4> = textbox4
elseif input1 = 5 then
set txt<name1> = textbox5
set txt<name2> = textbox6
set txt<name3> = textbox7
set txt<name3> = textbox8
...


Later, I can do a loop like:

loop for i = 1 to 4 step 3
call function(i,txt<name1-4>)
{work with txt<name1-4> without having to explicitly reference}
end loop




This seems to cut down on code for me. Thanks so much for your help!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top