We help IT Professionals succeed at work.

ShowWeekNumbers in a DTPicker?

hkp asked

I need to Show Week Numbers. I haven't been able to  show week numbers in a DTPicker, so I decided to do my own control.

I use a Command Control and a Text Control to emulate the text part and the button part of the DTPicker. When the Command Control is activated, I activate a Form (MonthViewForm) which is modeless.

The MonthViewForm is a form which has a MonthView Control on it's canvas.

The MonthViewForm is checking for the chosen date, using MonthView1_DateClicked event. When this happens, the MonthViewForm is unloaded by "Unload Me". I communicate the event to the caller of the MonthViewForm, by raising a "Done(dtDate As Date)" event.

However, my problem is to unload the MonthViewForm when hitting anything but MonthViewForm. How do I accomplish  this?

I have tried to use the MonthViewForm_Deactivate(), MonthViewForm_LostFocus(), but with no good results. My current plan is to be able to send a WM_KILLFOCUS to the MonthViewForm when the form with my UserControl (MMDatePicker), gains control.

Anyone has an idea?

Watch Question

I have written a function for this, I hope it works for you. It i acctualy written for a the calander we use in Sweden.

You can call the function like this:
MsgBox getSwedishWeekNumber(Now, vbMonday, CDate("2001-01-01"))

' Author: dannic
' Name: getSwedishWeekNumber
' Required Parameters:
' dtmWeekDate = the date we want to get the weeknumber for. eg. 2001-12-24
' firstDateOfWeek as VbDayOfWeek, eg. vbMonday
' dtmFirstWeeksStartDate The date for the first day
' of week number one the specified year eg. 2001-01-01
Function getSwedishWeekNumber(ByVal dtmWeekDate As Date, _
                        ByRef firstDayofweek As VbDayOfWeek, _
                        ByRef dtmFirstWeeksStartDate As Date) As Integer

    Dim intDays As Integer
    Dim intFirstWeek As Integer
    On Error GoTo getSwedishWeekNumberErr
    'how many days does the first week contain?
    Select Case Weekday(dtmFirstWeeksStartDate)
        Case vbSunday
            intFirstWeek = 0
        Case vbMonday
            intFirstWeek = 6
        Case vbTuesday
            intFirstWeek = 5
        Case vbWednesday
            intFirstWeek = 4
        Case vbThursday
            intFirstWeek = 3
        Case vbFriday
            intFirstWeek = 2
        Case vbSaturday
            intFirstWeek = 1
    End Select
    'Get number of days into this year
    intDays = dtmWeekDate - dtmFirstWeeksStartDate
    'Check if it is week number one!
    If intDays <= intFirstWeek Then
        getSwedishWeekNumber = 1
    Else 'Calculate the week number!
        ' if mod 7 returns 0 then the date is the last day of the week!
        ' Then we just add one (1) instead of two (2) to getWeekNumber to avoid that wrong weeknumber is returned
        If (intDays - intFirstWeek) Mod 7 = 0 Then
            'Calculate the week number!
            getSwedishWeekNumber = ((intDays - intFirstWeek) \ 7) + 1
            'Calculate the week number!
            getSwedishWeekNumber = ((intDays - intFirstWeek) \ 7) + 2
            'Weeknumber 53 does not exist in sweden!
            If getSwedishWeekNumber > 52 Then
                getSwedishWeekNumber = 1
            End If
        End If
    End If
 Exit Function
    Call App.LogEvent(vbCrLf & "Error in getSwedishWeekNumber!" & vbCrLf & _
                        "Errornumber: " & Err.Number & vbCrLf & _
                        "Errordescription: " & Err.Description, vbLogEventTypeError)

    Err.Raise Err.Number, Err.Source, Err.Description

End Function


hello hkp !

1. However, my problem is to unload the MonthViewForm when hitting anything but MonthViewForm. How do I
accomplish  this?

a. The Deactivate-Event isn't fired when the form doesn't belong to your process.

b. you can trap that event if you subclass the form
   see vbaccelerator.com or www.thescarms.com
   in the wnd_proc do something like this
    Select Case Msg
        Case WM_ACTIVATE
            If wParam = WA_INACTIVE Then
              SendMessageLong tmpHwnd, WM_NCACTIVATE, 0, 1
              Call ClosePopUp
c. You have to create a Window via API (CreateWindowEx)when you click on the command button (in procedure ShowPopup for example). You trap INACTIVE  for this window

d. You have to set the parent of your monthview to the window you created with CreateWindowEx




First of all, the week numbers in Denmark include week 53, hence, I have no use of the Swedish week number system. I actually have a Danish way of doing it, but this is not the issue here.

I want the behavior of the DTPicker, I just want to replace it with a DTPicker Control (which also include the week numbers, just like the MonthCalendar Control does)

I can't get the proposed subclassed form to work. It actually, send VB to the ground.
VK, could you specify a working sample which actually closes the popped up window (the one with the MonthCalendar Control) when the parent window (the one with the written date and the Popup Window Activation Buton) gets activated?

I am willing to give better credits for a valid working answer.

Author of the Year 2009

Hi hkp,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Refund points and save as a 0-pt PAQ.

hkp, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
DanRollins -- EE database cleanup volunteer


I would like to give the points to V.K. but he didn't finish the solution, hence I raise the points instead. Actually, I still need an answer. It seems, I need a better understanding of the way the events flow and how. In my opinion, it is the most badly documented part of the Windows API.

Author of the Year 2009

You are unlikely to get new responses to this ONE YEAR OLD QUESTION.  It is best to finalize this and ask a new question so it will appear in the question list at a place where the experts can see it.  What do you want to do to finalize this?
-- Dan
Per recommendation, points refunded and question closed.

CS Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.