Displaying count in a text box.

  • Thread starter Thread starter mdub
  • Start date Start date
M

mdub

Hi,

I have a text box (txtCount) and an Add button. I am trying to write code
that increases the count by 1 in the text box, every time the button is
clicked. The code I have so far only produces "1" in the text box:

Private Sub cmdAdd_Click()

Dim Count As Integer

Count = 0
Count = Count + 1
txtCount.Value = Count

End Sub

Thanks for the help.

Michael
 
Michael,
sounds like you are using an unbound text box.
Each time you set the count value, it is not stored anywhere.
Try storing the value of count in the tag property of the text box.
in the after update event of the text box put
Me.txtCount.Tag = Me.txtCount

In the cmdAdd click event put
Me.txtCount = Nz(Me.txtCount.Tag) + 1

Jeanette Cunningham
 
Im not sure the exact coding but you need to create a Static variable I
think. Each time you press the button it Sets the Count to 0 then adds 1 to
it... so it will come up as 1 every time.

I think if you put it on top of the module such as

Static Count As Integer
Count = 0

Private Sub cmdAdd_Click()
Count = Count + 1
txtCount.Value = Count
End Sub
 
Hi,

I have a text box (txtCount) and an Add button. I am trying to write code
that increases the count by 1 in the text box, every time the button is
clicked. The code I have so far only produces "1" in the text box:

Private Sub cmdAdd_Click()

Dim Count As Integer

Count = 0
Count = Count + 1
txtCount.Value = Count

End Sub

Thanks for the help.

Michael

Your code resets Count to 0 each time it's run.
Then you add 1 to the 0 and it now equals 1.
Then you set the control txtCount to 1.
That's why it alwayw will =1.

Try it this way:

Private Sub cmdAdd_Click()
txtCount = Nx(txtCount,0)+1
End Sub
 
Thanks for your help, I figured it out.

Michael

Jeanette Cunningham said:
Michael,
sounds like you are using an unbound text box.
Each time you set the count value, it is not stored anywhere.
Try storing the value of count in the tag property of the text box.
in the after update event of the text box put
Me.txtCount.Tag = Me.txtCount

In the cmdAdd click event put
Me.txtCount = Nz(Me.txtCount.Tag) + 1

Jeanette Cunningham
 
Back
Top