changing date/month in vb6
hi all
im trying to get rid of a lot of usless code in this programe im redoing
what i have is two arrow keys a forward and backwards arrow
what they do is move the date or nonth foreward or backwards.
here is the code
code for the keys
VB Code:
Public Sub mnuNext_Click()
Change_Day_Month (True)
End Sub
Public Sub mnuPrevious_Click()
Change_Day_Month (False)
End Sub
main code
VB Code:
Public Sub Change_Day_Month(Forward As Boolean)
Dim leap As Integer
Dim lastday As Integer
Dim nmonth As Integer
Dim nday As Integer
Dim nyear As Integer
Dim cyear As Integer
Dim Add_day As Integer
numdays = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) 'number days in month
If Forward Then
Add_day = 1
Else
Add_day = -1
End If
cyear = iyear
leap = cyear - (cyear \ 4) * 4 'for leap year, leap=0
lastday = numdays(imonth) 'last day of month
If ((leap = 0) And (imonth = 2)) Then lastday = lastday + 1
'
If Not (cmdProgram.Value) Then
nday = iday + Add_day
If (nday > lastday) Or (nday < 1) Then
If (nday > lastday) Then
nday = 1
nmonth = imonth + Add_day
ElseIf (nday < 1) And (imonth = 1) Then
nmonth = 12
cyear = cyear + Add_day
nday = numdays(nmonth)
Else
nmonth = imonth + Add_day
nday = numdays(nmonth)
End If
Else
nmonth = imonth
End If
If (nmonth = 13) Or (nmonth = 0) Then
If (nmonth = 13) Then
nmonth = 1
Else
nmonth = 12
End If
nyear = cyear + Add_day
Else
nyear = cyear
End If
txtDay.Text = CStr(nday)
Else 'Light Mode
nmonth = imonth + Add_day
nyear = cyear
If (nmonth = 13) Or (nmonth = 0) Then
If (nmonth = 13) Then
nmonth = 1
Else
nmonth = 12
End If
nyear = cyear + Add_day
End If
End If
'
imonth = nmonth
iday = nday
iyear = nyear
txtMonth.Text = Convert_Date_Int2Str(CInt(nmonth))
txtyear.Text = CStr(nyear + 1900) 'add back 1900
Call run
End Sub
I have tride to use date picker but it does not fit or look goog in my form.
any help appriciated
ed
Re: changing date/month in vb6
:eek2:
to change a date use the DateAdd function, e.g.:
VB Code:
' a month ago
Debug.Print DateAdd("m", -1, Now)
' month from now
Debug.Print DateAdd("m", 1, Now)
Re: changing date/month in vb6
WOW ... so much code to adjust date ... :eek2: :)
Isn't it nice to have functions like below:
DateAdd
DatePart
DateDiff
and some others ... mighty handy. :thumb:
Cheers.