Spin button questions

N

Norm

I am making a simple user form for an Excel macro to use
instead of asking a bunch of questions to the user. (This
is from Office XP and Excel 2002.) The form has a text
box with a spin button beside it. Here's a
couple "beginner's" questions about it:

1) Since I need to keep the two controls syncronized, I
thought I would use the 'Change' event on each control to
update the other one. But wouldn't that lead to an
infinite loop? Example: when the text box changes, it
would trigger the 'Change' event to update the spin
button. But the spin button's 'Change' event would try to
update the text box. Would it ever end?

2) I wrote some simple code to try to update the spin
button from the text box and I run into an error that says
the property can't be updated. I am trying to set
the 'Value' property on the spin button. Can't that be
updated?

Thanks for any and all help!
 
D

Dave Peterson

Instead of keeping them syncronized, why not just have the textbox keep track of
the value and the spinner either increase or decrease that value in the textbox?

Option Explicit
Private Sub SpinButton1_SpinDown()
With Me.TextBox1
.Value = Val(.Value) - 1
End With
End Sub
Private Sub SpinButton1_SpinUp()
With Me.TextBox1
.Value = Val(.Value) + 1
End With
End Sub

Then if you need the current value, just look at val(me.textbox1.value)
 
T

Thomas Bartkus

Norm said:
I am making a simple user form for an Excel macro to use
instead of asking a bunch of questions to the user. (This
is from Office XP and Excel 2002.) The form has a text
box with a spin button beside it. Here's a
couple "beginner's" questions about it:

1) Since I need to keep the two controls syncronized, I
thought I would use the 'Change' event on each control to
update the other one. But wouldn't that lead to an
infinite loop? Example: when the text box changes, it
would trigger the 'Change' event to update the spin
button. But the spin button's 'Change' event would try to
update the text box. Would it ever end?

The question never occurs in the most typical situation where you simply
update a textbox from the SpinButton. You would leave the TextBox change
event empty and you would also disable manual entry to the TextBox by
setting the [Locked] property to true. You control the TextBox from the
SpinButton.

Did you mean you want to go *both* ways?
Updating the SpinBox *also* when the user types a number into the
TextBox?

It still doesn't go into an infinite loop. The number reflected back to the
TextBox from the SpinButton alteration will be the *same* number you already
typed in. Since the number is the same, it won't trigger a new change event
and the back and forth ends right there.
2) I wrote some simple code to try to update the spin
button from the text box and I run into an error that says
the property can't be updated. I am trying to set
the 'Value' property on the spin button. Can't that be
updated?

Of course it can!
I'm guessing your 'Value' falls outside the .Max and .Min properties of the
SpinButton. The 'Value' will be refused if it falls ouside this range.
Of course, you also need Locked = False and Enabled = True.

Thomas Bartkus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top