Consulting

Results 1 to 12 of 12

Thread: next button help

  1. #1

    next button help

    hi
    just a small question bout this code,
    [VBA]Private Sub cmdNext_Click()
    Dim iYear As Long

    iYear = CLng(txtYear.Text)
    If iYear < Year(Date) Then
    iYear = iYear + 1
    txtYear.Text = iYear
    End If
    End Sub

    Private Sub cmdPrev_Click()
    Dim iYear As Long

    iYear = CLng(txtYear.Text)
    iYear = iYear - 1
    txtYear.Text = iYear
    End Sub [/VBA]

    i need to be able to go back years as far as 2006 but only forward to the current year how is that possible? as at the moment it starts on 2007 will go forward to 2008 and no further but will not go back any years.
    also how can i set the text box so when the user form is opened it starts on the current year?
    thanks ash

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What's in the textbox?

    The code works fine for me if I just have the year in it.

  3. #3
    the next butoon works fine, will start on 2007 then will only go as far as 2008 perfect, but the previous button will not go back once the next button has been pressed.

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Well it does for me.

  5. #5
    what year can you go back to?

  6. #6
    sorry been a idiot works fine if i attach the code to the correct button, can i set it though so i cant go any further back than 2007?

  7. #7
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    As far as I want it would appear, though I obviously didn't test for every year.

    And I can go backwards and forwards no problem up to 2008.

    By the way have you considered using a spinner control instead of 2 command buttons?

  8. #8
    no how does that work?

  9. #9
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What years do you actually want to use?

    3 controls seems like overkill when you appear, though it's not clear, to only want to work with 2-3 years.

    You could easily populate a combobox with the required years.

  10. #10
    well from 2007 as that s far back as the data will go, but could be 20years forward depending on workbook life!

  11. #11
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Well there's nothing stopping you populating a combobox dynamically based on what data you have.

    Code to do it would depend on the data.

    Could be simple as finding the max and min years from it.

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub cmdPrev_Click()
    Dim iYear As Long

    iYear = CLng(txtYear.Text)
    If iYear > 2007 Then
    iYear = iYear - 1
    txtYear.Text = iYear
    End If
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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