Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Location
    South Houston Tx.
    Posts
    6

    Question Unanswered: Counter problems

    I have never had any problems making counters before so this has really pussled me. I have no idea what the problem is but everytime I set off the counter it counts to one and stops. I have the following code along with a much larger program. none of these words are used anywhere else in the program.

    Private Sub CmdCheckGuess_Click()
    Dim intcounter As Integer
    intcounter = intcounter + 1
    Lblcounter.caption = intcounter
    End Sub

    I am running VB6 student edition.

  2. #2
    Join Date
    Jan 2004
    Location
    Aberdeen, Scotland
    Posts
    1,067
    The problem may be that intcounter loses its value after you sub has finished running,
    3 ways around this that i can think of
    1 instead of using a variable you could do something like
    lblcounter.Caption = CInt(lblCounter.Caption) + 1
    i.e. using the labels value to increment on itself
    2 use a public variable rather than a Dim
    3 use a Static Variable rather than the Dim (Static Variables are retained even when the sub has stoped)

  3. #3
    Join Date
    Nov 2004
    Posts
    30
    you are declaring the variable just before you increment it. so even if you increment it to 1 on first click on the next click you declare it again setting the value to 0. You have to declare the variable as form level or u can simply do one thing

    Private Sub CmdCheckGuess_Click()
    Lblcounter.caption = val(lblcounter)+1
    End Sub

  4. #4
    Join Date
    May 2004
    Posts
    97
    Quote Originally Posted by Buford
    I have never had any problems making counters before so this has really pussled me. I have no idea what the problem is but everytime I set off the counter it counts to one and stops. I have the following code along with a much larger program. none of these words are used anywhere else in the program.

    Private Sub CmdCheckGuess_Click()
    Dim intcounter As Integer
    intcounter = intcounter + 1
    Lblcounter.caption = intcounter
    End Sub

    I am running VB6 student edition.
    David is right on. Read up on Scope of variables.

    Where you dim your variable makes all the difference in the world.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •