Results 1 to 6 of 6

Thread: incrementing a #

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Posts
    5

    Question

    i feel like an idiot posting this, but i want a label to increase by one. I used this and for some reason it doesn't work.

    Code:
    Private Sub Form_Load()
    
    Dim k As Integer
    
    End Sub
    
    Private Sub tmr_Timer()
    
    k = k + 1
    
    lbl.Caption = k
    
    End Sub
    it only goes up to one and stops


  2. #2
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    Talking

    Try this instead:
    --------------------------------
    Private Sub tmr_Timer()
    Static K as Integer

    k = k + 1

    lbl.Caption = k

    End Sub
    --------------------------------

    Static will make it keep its value. The way you had it tells the timer that k is 0 therefore you have:
    0 = 0 + 1
    This will work. Trust me.

  3. #3
    Guest
    Hello there,

    You need to either use the static keyword ok declare a variable that can be seen either globaly (not the best) or in a form level.

    In the form declaration section, so it can be used by the all subs and function in the form:
    Code:
    Option Explicit
    
    ' form vars
    Dim iCounter As Integer
    or in a bas file
    Code:
    Option Explicit
    
    ' Public var's
    Dim piCounter As Integer
    or with the static
    Code:
    Private Sub tmr_Timer()
    
    Static k
    
    k = k + 1
    
    lbl.Caption = k
    
    End Sub

    Hope this helps,

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2000
    Posts
    5

    Thumbs up It worked.

    tnx. i used the static one from RealisticGraphics, the other one was too long

  5. #5
    Hyperactive Member
    Join Date
    Sep 1999
    Posts
    305
    Why bother with static? you can just as easily put

    Code:
    Dim k As Integer
    at the top of the code, outside of any subs. That's where you can define integers or API or constants for the form.

    bob

  6. #6
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    Using static is better for one good reason. You can type everything in the same spot. Plus if you want to use the var again later you can. Static is a very usful property that you can use rather than going back and forth to declare vars elsewhere. But it's more of a preference than anything else.

Posting Permissions

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



Click Here to Expand Forum to Full Width