Solved

VB6 - How do I set DayBold property in the MonthView control dynamically?

Posted on 2008-10-29
11
1,932 Views
Last Modified: 2012-05-05
Hi Experts,

I have a month view control that I need to set its dayBold property at runtime for multiple days. I am using the below code, but it doesn't seem to work!

I have checked the code and the recordset has 3 records for different dates in the same month, however it doesn't show any bold day!
RS.MoveFirst
        While Not RS.EOF
        
                  With Me.MonthView1
                  
                    Dim d1 As Date
                    d1 = DateValue(RS("valueDate"))
                    .DayBold(d1) = True
                    
                  End With
                  
                  RS.MoveNext
        Wend

Open in new window

0
Comment
Question by:feesu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 4

Expert Comment

by:GO-87
ID: 22839280
Hi feesu
Is your code running inside the Form_Load ( ) event handler? If so, try adding these two lines before your code:
  Me.Show
  DoEvents
Cheers
G
0
 

Author Comment

by:feesu
ID: 22839299
G!

You are a life saver!!

What's wrong with the code? Why did I need to do this?
0
 
LVL 11

Expert Comment

by:Antagony1960
ID: 22839477
DayBold only works on dates which fall within the VisibleDays array, which spans from 1 to 42.
Unfortunately, the bolding information is not preserved as you move from month to month, so I suggest you put the code in the SelChange event. Assuming the RS recordset is defined elsewhere (if not you could refine the query to limit it to the visible dates), something like this should work:

Private Sub MonthView1_SelChange(ByVal StartDate As Date, _
                         ByVal EndDate As Date, Cancel As Boolean)
Dim d1 As Date
Static dFirst As Date
    With MonthView1
        If dFirst <> .VisibleDays(1) Then
            dFirst = .VisibleDays(1)
            While Not RS.EOF
                d1 = DateValue(RS("valueDate"))
                If d1 >= dFirst And d1 <= dFirst + 41 Then
                    .DayBold(d1) = True
                End If
                RS.MoveNext
            Wend
        End If
    End With
End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:feesu
ID: 22855841
Antagony1960,

Your point is valid. However, I have tried the code you sent, and called it as well on load. But it never bolded anything!

Note: I have defined the RS in the form declaration and opened it in the form load.
0
 
LVL 11

Expert Comment

by:Antagony1960
ID: 22856007
Your probably still at the end of the recordset when the event fires. Try sticking a movefirst in just before the wend loop:

            RS.MoveFirst
            While Not RS.EOF
            ...ect.
0
 

Author Comment

by:feesu
ID: 22856010
I did that already. After browsing to a different month, it doesn't bold anything!
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22856036
Don't forget to refresh the control :)
private Sub Form_Activate()
'...
        While Not RS.EOF       
                  With Me.MonthView1
                    Dim d1 As Date
                    d1 = DateValue(RS("valueDate"))
                    .DayBold(d1) = True                    
                  End With                 
                  RS.MoveNext
        Wend
Me.MonthView1.Refresh
End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:Antagony1960
ID: 22856089
Ah, it's a long time since I've played with this so I'd forgotten that Microsoft, in their wisdom, decided to make the control have different methods of bolding depending on whether the date is set externally or by clicking on the calendar itself. You need to add the GetDayBold event for calendar clicks:
Private Sub MonthView1_GetDayBold(ByVal StartDate As Date, ByVal Count As Integer, State() As Boolean)
Dim i As Integer
    For i = 0 To Count - 1
        State(i) = MonthView1.DayBold(StartDate + i)
    Next
End Sub

Open in new window

0
 

Author Comment

by:feesu
ID: 22860315
Hello again,

This is my code that I call on load and on sel change. In addition to the code you sent for GetDayBold.

Your code started bolding the days of each month! For example, I need to bolden only 24th of Oct. but now when I click on next month manually, the 24th of Nov. is also bolded!
Sub test()
        
    Me.Show
    DoEvents
 
    Dim D1 As Date
    Static dFirst As Date
    With MonthView1
        If Format(dFirst, "dd/mm/yyyy") <> Format(.VisibleDays(1), "dd/mm/yyyy") Then
            dFirst = Format(.VisibleDays(1), "dd/mm/yyyy")
            RS.MoveFirst
            While Not RS.EOF
                D1 = Format(DateValue(RS("valueDate")), "dd/mm/yyyy")
                If D1 >= Format(dFirst, "dd/mm/yyyy") And D1 <= Format(DateAdd("d", 41, dFirst), "dd/mm/yyyy") Then
                    .DayBold(Format(D1, "dd/mm/yyyy")) = True
                    'MsgBox "bolded: " & D1
                End If
                RS.MoveNext
            Wend
        End If
        .Refresh
    End With
 
    
    DoEvents
 
End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:Antagony1960
ID: 22860876
Right, sorry about that. I was just about to go out for the day so I didn't get chance to test it properly.

Yeah, I'd forgotten all about this MonthView control's annoying behaviour. You'll still need to use the GotDayBold event code I posted earlier, but you now also have to reset any existing bolding whenever the month changes. This should do it:
Private Sub MonthView1_SelChange(ByVal StartDate As Date, _
                         ByVal EndDate As Date, Cancel As Boolean)
Dim D1 As Date, i As integer
Static dFirst As Date
    With MonthView1
        If Format(dFirst, "dd/mm/yyyy") <> Format(.VisibleDays(1), "dd/mm/yyyy") Then
            dFirst = Format(.VisibleDays(1), "dd/mm/yyyy")
            For i = 0 To 41: .DayBold(dFirst + i) = False: Next
            RS.MoveFirst
            While Not RS.EOF
                D1 = Format(DateValue(RS("valueDate")), "dd/mm/yyyy")
                If D1 >= Format(dFirst, "dd/mm/yyyy") And D1 <= Format(DateAdd("d", 41, dFirst), "dd/mm/yyyy") Then
                    .DayBold(Format(D1, "dd/mm/yyyy")) = True
                End If
                RS.MoveNext
            Wend
        End If
        .Refresh
    End With 
End Sub

Open in new window

0
 
LVL 11

Accepted Solution

by:
Antagony1960 earned 500 total points
ID: 22860899
By the way, you shouldn't need to be formatting dFirst or D1 as they're already date variables. I'd just use DateValue on the RS and MonthView dates if I were you. Like this:
Private Sub MonthView1_SelChange(ByVal StartDate As Date, _
                         ByVal EndDate As Date, Cancel As Boolean)
Dim D1 As Date, i As Integer
Static dFirst As Date
    With MonthView1
        If dFirst <> DateValue(.VisibleDays(1)) Then
            dFirst = DateValue(.VisibleDays(1))
            For i = 0 To 41: .DayBold(dFirst + i) = False: Next
            RS.MoveFirst
            While Not RS.EOF
                D1 = DateValue(RS("valueDate"))
                If D1 >= dFirst And D1 <= dFirst + 41 Then
                    .DayBold(D1) = True
                End If
                RS.MoveNext
            Wend
        End If
        .Refresh
    End With
End Sub

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question