Increment buttons

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

I have two text boxes on a form which contain amounts in
Currency format. I would like to make two buttons ("+"
and "-" button)near the second text box. They should work
as follows:
When one presses "+" button the value of the TextBox1
decreases by 1 and the value of TextBox2 increases by 1.
When one presses "-" button the value of the TextBox1
increases by 1 and the value of TextBox2 decreases by 1.
Is it possible to make?
 
You can use the Command Button Wizard to get the picture that you would
like.

The command for the + command button in VBA would be the following:

Me.TextBox1.Value = CStr(CLng(Me.TextBox1.Value) - 1)
Me.TextBox2.Value = CStr(CLng(Me.TextBox2.Value) + 1)

The command for the - command button in VBA would be the following:

Me.TextBox1.Value = CStr(CLng(Me.TextBox1.Value) + 1)
Me.TextBox2.Value = CStr(CLng(Me.TextBox2.Value) - 1)

The above commands uses conversion functions as text boxes stores values in
string format.
 
Ronald said:
You can use the Command Button Wizard to get the picture that you would
like.

The command for the + command button in VBA would be the following:

Me.TextBox1.Value = CStr(CLng(Me.TextBox1.Value) - 1)
Me.TextBox2.Value = CStr(CLng(Me.TextBox2.Value) + 1)

The command for the - command button in VBA would be the following:

Me.TextBox1.Value = CStr(CLng(Me.TextBox1.Value) + 1)
Me.TextBox2.Value = CStr(CLng(Me.TextBox2.Value) - 1)

The above commands uses conversion functions as text boxes stores values in
string format.


Ronald, I'm pretty sure that text boxes store their values
as a variant, so the conversion functions are not strictly
required. OTOH, other than the extra time it would take to
do the conversions when the value is already the right type,
it may be a good idea to take the guesswork out of Access's
hands.
 
You are partially correct in that it depends on what the developer has done
with the Format Property on the textbox or if it's bound to some field in a
table, but in it's absence, it is in String format by default.

One other thing that I found with either A97 or XL97 UserForms (not sure
which since it's been a long while ago), but not sure if it's an issue with
A02, if the textbox value is a "Null" value, it seems to error out when you
use something like Textbox.Value = "" as the criteria of an If, Then, Else
Statement. For this reason, I use the following code for testing purposes:

If IsNull(Textbox.Value) Then
<Statement(s)>
Elseif Textbox = "" Then
<Statement(s)>
Else
<Statement(s)>
End If

I test for the Null value before I test for anything else to get around this
particular issue. One might say it would be better to use the Select Case
in place of the multi-level If Then Else statement above cause it only
evaluate the value 1 time and that's at the beginning of the series
structure.
 
Ronald said:
You are partially correct in that it depends on what the developer has done
with the Format Property on the textbox or if it's bound to some field in a
table, but in it's absence, it is in String format by default.

One other thing that I found with either A97 or XL97 UserForms (not sure
which since it's been a long while ago), but not sure if it's an issue with
A02, if the textbox value is a "Null" value, it seems to error out when you
use something like Textbox.Value = "" as the criteria of an If, Then, Else
Statement. For this reason, I use the following code for testing purposes:

I've never seen that cause an error. When the text box
contains Null, the construct;

If textbox = "" Then
'yes
Else
'no
End If

will always take the Else branch, but it shouldn't cause an
error unless the statements in the Else part can't deal with
Null.

If IsNull(Textbox.Value) Then
<Statement(s)>
Elseif Textbox = "" Then
<Statement(s)>
Else
<Statement(s)>
End If

I test for the Null value before I test for anything else to get around this
particular issue.

Sure, Null usually requires special handling.

One might say it would be better to use the Select Case
in place of the multi-level If Then Else statement above cause it only
evaluate the value 1 time and that's at the beginning of the series
structure.

Select Case doesn't deal with Null, so even if you do use
Select Case, you would still have to either preceed it with
an If IsNull(... or deal with it in the Case Else block
(which I think is a little convoluted. or at least obscure).
 
Back
Top