Code Problem

D

DS

I have this Calendar that I set-up. It works except I can't get the
Month and Year to appear in the Titlebar. It says sub or function not
defined.
Thanks
DS

Option Compare Database
Function SetCalendar(ByVal Flag As Integer) As Integer

Dim MyForm As Form, MyControl As Control
Dim MaxDays As Integer, Offset As Integer, X As Integer
Static M As Double

SetCalendar = True

Set MyForm = Forms![Calendar]

Select Case Flag
Case -2, 2
M = DateAdd("yyyy", Flag - (Flag * 0.5), M)
Case -1, 1
M = DateAdd("m", Flag, M)
Case 0
M = DateValue(DatePart("m", Date$) & "/1/" & DatePart("yyyy",
Date$))
End Select

MaxDays = Val(Mid$("312831303130313130313031", DatePart("m", M) * 2 - 1, 2))
If DatePart("m", M) = 2 Then
If DatePart("yyyy", M) Mod 4 = 0 And DatePart("yyyy", M) Mod 400 <>
0 Then
MaxDays = 29
End If
End If

Offset = DatePart("w", M)
For X = 1 To 42
Set MyControl = MyForm("Day" & Str$(X))
If X < Offset Or X > Offset + MaxDays - 1 Then
MyControl = ""
Else
MyControl = X - Offset + 1
End If
Next X

X = SetTitleBar("Calendar", Format$(M, "mmm yyyy"))

End Function
 
D

Douglas J. Steele

There's no SetTitleBar function native to Access: it must be a user-defined
function that's missing from your application.

BTW, you're doing far more work than is required to determine the last day
of the month. A simple one-liner is:

MaxDays = Day(DateSerial(Year(M), Month(M) + 1, 0))
 
D

DS

Douglas said:
There's no SetTitleBar function native to Access: it must be a user-defined
function that's missing from your application.

BTW, you're doing far more work than is required to determine the last day
of the month. A simple one-liner is:

MaxDays = Day(DateSerial(Year(M), Month(M) + 1, 0))
Thanks Doug,
I went another way....
Made a form with 42 Textboxes, Named them D0, D1....to D40
Made a textbox called [FirstDate], format is: mmmm yyyy
Made a backbutton and forward button, cancel button.

Heres the code.

On the Forward Button:
Private Sub Command50_Click()
Me.FirstDate = DateAdd("m", 1, FirstDate) 'increase by 1 month
'Private Function filldates()
Dim curday As Variant, curbox As Integer
curday = DateSerial(Year(Forms!Calendar![FirstDate]),
Month(Forms!Calendar![FirstDate]), 1) 'first day of month
curday = DateAdd("d", 1 - Weekday(curday), curday) 'back to sunday
For curbox = 0 To 41 'need to loop thru 42 textboxes
Forms!Calendar("D" & curbox) = Day(curday)
Forms!Calendar("D" & curbox).Visible = False
If Month(curday) = Month(Forms!Calendar!FirstDate) Then
Forms!Calendar("D" & curbox).Visible = True
curday = curday + 1 'nextday
Next curbox
End Sub

On theBack Button:
Private Sub Command51_Click()
Me.FirstDate = DateAdd("m", -1, FirstDate) 'decrease by 1 month
Dim curday As Variant, curbox As Integer
curday = DateSerial(Year(Forms!Calendar![FirstDate]),
Month(Forms!Calendar![FirstDate]), 1) 'first day of month
curday = DateAdd("d", 1 - Weekday(curday), curday) 'back to sunday
For curbox = 0 To 41 'need to loop thru 42 textboxes
Forms!Calendar("D" & curbox) = Day(curday)
Forms!Calendar("D" & curbox).Visible = False
If Month(curday) = Month(Forms!Calendar!FirstDate) Then
Forms!Calendar("D" & curbox).Visible = True
curday = curday + 1 'nextday
Next curbox
End Sub

On Open Form:
Private Sub form_open(cancel As Integer)
Forms!Calendar![FirstDate] = Date 'set starting date to current date
Dim curday As Variant, curbox As Integer
curday = DateSerial(Year(Forms!Calendar![FirstDate]),
Month(Forms!Calendar![FirstDate]), 1) 'first day of month
curday = DateAdd("d", 1 - Weekday(curday), curday) 'back to sunday
For curbox = 0 To 41 'need to loop thru 42 textboxes
Forms!Calendar("D" & curbox) = Day(curday)
Forms!Calendar("D" & curbox).Visible = False
If Month(curday) = Month(Forms!Calendar!FirstDate) Then
Forms!Calendar("D" & curbox).Visible = True
curday = curday + 1 'nextday
Next curbox
End Sub

On the Cancel Button:
Private Sub Command52_Click()
On Error GoTo Err_Command52_Click
DoCmd.Close
Exit_Command52_Click:
Exit Sub

Err_Command52_Click:
MsgBox Err.Description
Resume Exit_Command52_Click

End Sub

This works really!
Check it out.
Thanks all!
DS
 
D

Douglas J. Steele

If it works for you, great. My advice, though, would be to use the one from
Stephen Lebans that I mentioned earlier. I've always believed it's better to
present the users with a control that they've seen before whenever possible,
and Stephen's is the standard Windows interface that they'll have seen.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



DS said:
Douglas said:
There's no SetTitleBar function native to Access: it must be a
user-defined function that's missing from your application.

BTW, you're doing far more work than is required to determine the last
day of the month. A simple one-liner is:

MaxDays = Day(DateSerial(Year(M), Month(M) + 1, 0))
Thanks Doug,
I went another way....
Made a form with 42 Textboxes, Named them D0, D1....to D40
Made a textbox called [FirstDate], format is: mmmm yyyy
Made a backbutton and forward button, cancel button.

Heres the code.

On the Forward Button:
Private Sub Command50_Click()
Me.FirstDate = DateAdd("m", 1, FirstDate) 'increase by 1 month
'Private Function filldates()
Dim curday As Variant, curbox As Integer
curday = DateSerial(Year(Forms!Calendar![FirstDate]),
Month(Forms!Calendar![FirstDate]), 1) 'first day of month
curday = DateAdd("d", 1 - Weekday(curday), curday) 'back to sunday
For curbox = 0 To 41 'need to loop thru 42 textboxes
Forms!Calendar("D" & curbox) = Day(curday)
Forms!Calendar("D" & curbox).Visible = False
If Month(curday) = Month(Forms!Calendar!FirstDate) Then Forms!Calendar("D"
& curbox).Visible = True
curday = curday + 1 'nextday
Next curbox
End Sub

On theBack Button:
Private Sub Command51_Click()
Me.FirstDate = DateAdd("m", -1, FirstDate) 'decrease by 1 month
Dim curday As Variant, curbox As Integer
curday = DateSerial(Year(Forms!Calendar![FirstDate]),
Month(Forms!Calendar![FirstDate]), 1) 'first day of month
curday = DateAdd("d", 1 - Weekday(curday), curday) 'back to sunday
For curbox = 0 To 41 'need to loop thru 42 textboxes
Forms!Calendar("D" & curbox) = Day(curday)
Forms!Calendar("D" & curbox).Visible = False
If Month(curday) = Month(Forms!Calendar!FirstDate) Then Forms!Calendar("D"
& curbox).Visible = True
curday = curday + 1 'nextday
Next curbox
End Sub

On Open Form:
Private Sub form_open(cancel As Integer)
Forms!Calendar![FirstDate] = Date 'set starting date to current date
Dim curday As Variant, curbox As Integer
curday = DateSerial(Year(Forms!Calendar![FirstDate]),
Month(Forms!Calendar![FirstDate]), 1) 'first day of month
curday = DateAdd("d", 1 - Weekday(curday), curday) 'back to sunday
For curbox = 0 To 41 'need to loop thru 42 textboxes
Forms!Calendar("D" & curbox) = Day(curday)
Forms!Calendar("D" & curbox).Visible = False
If Month(curday) = Month(Forms!Calendar!FirstDate) Then Forms!Calendar("D"
& curbox).Visible = True
curday = curday + 1 'nextday
Next curbox
End Sub

On the Cancel Button:
Private Sub Command52_Click()
On Error GoTo Err_Command52_Click
DoCmd.Close
Exit_Command52_Click:
Exit Sub

Err_Command52_Click:
MsgBox Err.Description
Resume Exit_Command52_Click

End Sub

This works really!
Check it out.
Thanks all!
DS
 
D

DS

Douglas said:
If it works for you, great. My advice, though, would be to use the one from
Stephen Lebans that I mentioned earlier. I've always believed it's better to
present the users with a control that they've seen before whenever possible,
and Stephen's is the standard Windows interface that it with a sey'll have seen.
Thanks, Douglas
Sincerely,
DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top