PDA

View Full Version : next button help



ashgull80
02-24-2008, 07:58 AM
hi
just a small question bout this code,
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

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

Norie
02-24-2008, 08:09 AM
What's in the textbox?

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

ashgull80
02-24-2008, 08:13 AM
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.

Norie
02-24-2008, 08:14 AM
Well it does for me.:confused:

ashgull80
02-24-2008, 08:20 AM
what year can you go back to?

ashgull80
02-24-2008, 08:24 AM
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?

Norie
02-24-2008, 08:24 AM
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?

ashgull80
02-24-2008, 08:26 AM
no how does that work?

Norie
02-24-2008, 08:29 AM
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.

ashgull80
02-24-2008, 08:36 AM
well from 2007 as that s far back as the data will go, but could be 20years forward depending on workbook life!

Norie
02-24-2008, 09:17 AM
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.

Bob Phillips
02-24-2008, 11:36 AM
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