• MonthView Control (VB6)

    Author
    Topic
    #389691

    Hi all. I have a form with about 20 date fields on it. What I am trying to achieve is for the user to be able to click on the date field and a calender similiar to the MonthView Control will popup and the user will select a date from it. When the user tabs out of the field the date will display in the pre-determined format (YYYY-MM-DD). Currently all I have is the MonthView control on the form and it takes up alot of space.
    Is this possible to acheive?

    Thanks.

    Bill

    Viewing 1 reply thread
    Author
    Replies
    • #689341

      How ’bout the DTPicker, from “Microsoft Windows Common Controls-2 6.0” AKA MSCOMCT2.OCX? It pops down when the user clicks the drop-down arrow. (You will undoubtedly choose a font that actually fits inside the space you allot to it, unlike my example.)

      • #689342

        jscher2000 – Perfect! Thanks for your help.

        Bill

      • #689344

        Oops just one other thing. When I run the form with the DTPicker field on it, the field displays todays date. I want the field to remain blank until it is clicked on. When I go to the Value property and delete the displayed date with the intention of forcing the field to display as blank, I get an error message saying that the field cannot contain a Null value.
        Is there anyway to get the field to display as blank until a date is selected? Thanks for your help.

        Bill

        • #689355
          • #689359

            Thanks heaps. The following works which I got from your second URL:

            Private Sub dpkDepDate1_Change()

            If dpkDepDate1.Value “” Then
            dpkDepDate1.Format = dtpShortDate
            End If

            End Sub

            Private Sub Form_Load()
            ‘First, set date to today… then blankout dpkDepDate1
            dpkDepDate1.Format = dtpCustom
            dpkDepDate1.CustomFormat = ” ‘ ”

            End Sub

            Just one more thing. I have 10 fields which the code needs to apply named dpkDepDate1 through to dpkDepDate10. Is there anyway I can write one piece code that will apply to all 10 fields instead of writing the same code 10 times?
            Thanks as always.

            Bill

            • #689364

              Private Sub dpkDepDate1_Change()
              HandleDateChange 1
              End Sub

              Private Sub dpkDepDate2_Change()
              HandleDateChange 2
              End Sub

              ‘ etc. for dkpDepDate3_Change through dkpDepDate10)Change

              Private Sub HandleDateChange(i As Integer)
              If Me.Controls(“dpkDepDate” & i).Value “” Then
              Me.Controls(“dpkDepDate” & i).Format = dtpShortDate
              End If
              End Sub

              Private Sub Form_Load()
              Dim i As Integer
              For i = 1 To 10
              ‘First, set date to today… then blankout dpkDepDate #i
              Me.Controls(“dpkDepDate” & i ).Format = dtpCustom
              Me,Controls(“dpkDepDate” & i).CustomFormat = ” ‘ ”
              Next i
              End Sub

            • #689368

              Hans,
              For some reason the following lets the date picker come up but when you click/tab out of it, no date is displayed:

              Private Sub Form_Load()
              Dim i As Integer
              For i = 1 To 10
              ‘First, set date to today… then blankout dpkDepDate #i
              Me.Controls(“dpkDepDate” & i ).Format = dtpCustom
              Me,Controls(“dpkDepDate” & i).CustomFormat = ” ‘ ”
              Next i
              End Sub

              Any idea why? I have had a little play with it and can’t work out why.
              Thanks for your help.

              Bill

            • #689372

              There is a typo in my reply that the Visual Basic Editor should catch: “Me,” instead of “Me.” I just typed the code into the reply, I didn’t copy it from actual code.

              The Form_Load part only blanks the Date/Time pickers. Displaying the dates is taken care of in the …_Change events, In my example, they all call HandleDateChange. I just now tried it in an actual form and it works OK.

            • #689378

              Hans,
              Got it. Works fine. Thanks heaps for your help.

              Bill

            • #689648

              Hans,
              I actually have two sets of 10 date fields that use the MonthView Control on the same form. They are dtpDepDate1 through 10 and dtpArrDate1 through 10 respectively. I got the code to work fine when changing dtpDepDate1 – 10 but I cannot change both sets. When I run the form with the code changed I get an Ambigous Name Detected error on the second Form Load Sub.
              Any idea where I have gone wrong?
              Thanks heaps.

              Bill

            • #689748

              There cannot be two procedures of the same name in a module. In particular, there can only be one Form_Load procedure for a given form – otherwise, which one should be executed when the form opens?

              You can, however, initialize both sets of date fields in one procedure:

              Private Sub Form_Load()
              Dim i As Integer
              For i = 1 To 10
              Me.Controls(“dpkArrDate” & i ).Format = dtpCustom
              Me,Controls(“dpkArrDate” & i).CustomFormat = ” ‘ ”
              Me.Controls(“dpkDepDate” & i ).Format = dtpCustom
              Me,Controls(“dpkDepDate” & i).CustomFormat = ” ‘ ”
              Next i
              End Sub

            • #691132

              Hans,
              Sorry for the late reply but I have been traveling. Since your last post I have had to start again with my app, as I have established that I didn’t plan well enough initially – lesson learnt! Despite having to start again, my original problem remains, however the fields have changed. In review my problem is this.
              I have a form with quite a few sets of fields. They are:

              dtpArrDate1 through to dtpArrDate10
              dtpDepDate1 through to dtpDepDate10
              dtpCheckInDate1 through to dtpCheckInDate5
              dtpCheckOutDate1 through to dtpCheckOutDate5
              dtpCarPickUp1 through to dtpCarPickUp5
              dtpCarDropOff1 through to dtpCarDropOff5

              The fields are all date picker controls. Currently when the form is loaded the fields display today’s date which is not what I want. I want the fields to display as blank until the user actually selects a date. I have had a few goes at writing code to make this happen but never successfully. I also tried to manipulate the code you supplied so generously previously but the changes I have had to make to the app have made it redundant and my skills are not up to scratch programming wise.
              Any ideas for code which will achieve my aim?

              Thanks in advance.

            • #691136

              Are you sure that having 40 Date/Time pickers on a form is a good idea?

              I would think that adapting the code would be straightforward:

              Private Sub Form_Load()
              Dim i As Integer
              For i = 1 To 10
              Me.Controls(“dpkArrDate” & i ).Format = dtpCustom
              Me,Controls(“dpkArrDate” & i).CustomFormat = ” ‘ ”
              Me.Controls(“dpkDepDate” & i ).Format = dtpCustom
              Me,Controls(“dpkDepDate” & i).CustomFormat = ” ‘ ”
              Next i
              For i = 1 To 5
              Me.Controls(“dtpCheckInDate” & i ).Format = dtpCustom
              Me.Controls(“dtpCheckInDate” & i ).CustomFormat = ” ‘ ”
              Me,Controls(“dtpCheckOutDate” & i).Format = dtpCustom
              Me,Controls(“dtpCheckOutDate” & i).CustomFormat = ” ‘ ”
              Me.Controls(“dtpCarPickUp” & i ).Format = dtpCustom
              Me.Controls(“dtpCarPickUp” & i ).CustomFormat = ” ‘ ”
              Me,Controls(“dtpCarDropOff” & i).Format = dtpCustom
              Me,Controls(“dtpCarDropOff” & i).CustomFormat = ” ‘ ”
              Next i
              End Sub

              If this doesn’t work, an you explain where you have problems?

            • #691327

              Hans,
              I was wondering why you suggest that having 40 date picker fields may not be a good idea? It was the only way I could think of to solve my problem. I short, the aim of my application is for users to be able to send the information of their requested travel bookings to their travel agent. There is a need for at least 10 flight requests and 5 hotel and car bookings.
              If there is an easier way I would be greatful of any advice.
              I had previously tried to do a similiar thing using a Custom Form in Outlook but users found using Outlook a little difficult (believe it or not!).
              Thanks heaps for your time.

              Bill

            • #691338

              Bill,

              It seems to me that one form with space for 10 flight requests, 5 hotel bookings and 5 car bookings with details for each will be rather cluttered. I haven’t thought deeply about it, but I would probably create a form that presents a non-editable overview of the customer’s bookings, with command buttons that show a popup form for editing an existing booking or adding a new one.

            • #691349

              Hans,
              The code you kindly provided works up to a point. It blanks out all the fields as required and allows the date picker to be selected, however when I select the date, the field goes back to being blank. I had a go at solving the problem myself with some limited success.
              The code at the end of this message, when added to the code you provided, enables the field to retain the date when it is selected from the date picker field. However it only works for one field (dtpDepDate1).
              I don’t really want to write the same code 40 times for the 40 fields that will have to be changed. Is there an easier way of doing this?
              On the other issue of why so many fields, I have attached a screen shot which may offer a better explanation than I was able to offer.
              Thanks as always.

              Bill

              Private Sub dtpDepDate1_CloseUp()
              FormatDTPicker
              End Sub

              Private Sub dtpDepDate1_Format(ByVal CallbackField As String, _
              FormattedString As String)
              If CallbackField = “X” Then
              FormattedString = “”
              End If
              End Sub

              Private Sub FormatDTPicker()
              With dtpDepDate1
              If .Value = vbNull Then
              .Format = dtpCustom
              .CustomFormat = “X”
              Else
              .Format = dtpShortDate
              End If
              End With
              End Sub

              Private Sub dtpDepDate1_MouseDown(Button As Integer, _
              Shift As Integer, X As Single, Y As Single)
              With dtpDepDate1
              If .Value = vbNull Then
              .Value = Now
              End If
              End With

            • #691350

              Oops, sorry the attachment didn’t make it last time. Another try.

            • #691363

              Hi Bill,

              I didn’t know that you had organized your form with a tab control; it looks good, I admit.

              About the code: the Date/Time picker is not installed on the PC I’m using at the moment (neither is VB6), so I can’t test code. You should be able to reduce the code for each of the events to one line, calling a generalized routine:

              Private Sub dtpDepDate1_CloseUp()
              FormatDTPicker 1
              End Sub

              Private Sub FormatDTPicker(i As Integer)
              With Me.Controls(“dtpDepDate”& i)
              If .Value = vbNull Then
              .Format = dtpCustom
              .CustomFormat = “X”
              Else
              .Format = dtpShortDate
              End If
              End With
              End Sub

              Private Sub dtpDepDate1_Format(ByVal CallbackField As String, FormattedString As String)
              HandleFormat
              End Sub

              Private Sub HandleFormat(ByVal CallBackField As String, FormattedString As String)
              If CallbackField = “X” Then
              FormattedString = “”
              End If
              End Sub

              Private Sub dtpDepDate1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
              HandleMouseDown 1
              End Sub

              Private Sub HandleMouseDown(i As Integer)
              With Me.Controls(“dtpDepDate” & i)
              If .Value = vbNull Then
              .Value = Now
              End If
              End With
              End Sub

              Another remark: have you tried to use control arrays?

            • #691366

              Hans,
              Thanks for that. I am away from my normal PC at the moment so I won’t be able to test your code until tomorrow. I will let you know how I go.
              As for control arrays, I must admit I have read a bit about them but have the confidence to use them. Is it in my interests to do so?
              Appreciate your help.

              Bill

            • #691370

              Bill,

              Instead of independent controls dtpDepDate1 to dtpDepDate10, you could have 10 controls all named dtpDepDate, with index 1 through 10. The controls in a control array share event procedures, so you don’t have to write 10 individual routines for each event.

            • #691439

              Hans – Your explanation is much clearer than the books I have been reading. I understand now and will look at it. Thanks heaps.

              Bill

            • #691458

              Hi Hans,
              I have installed the code and still no go:-( The date picker field works for field dtpDepDate1 as it is supposed to but for the remaining fields the field does not retain the value once selected.
              I have taken the liberty of attaching all of the code as it now exists to see if you can see where the problem is. I have looked without success.
              Thanks heaps for your time.

              Bill

              Option Explicit
              Private Sub Form_Load()
              Dim i As Integer
              For i = 1 To 10
              Me.Controls(“dtpArrDate” & i).Format = dtpCustom
              Me.Controls(“dtpArrDate” & i).CustomFormat = ” ‘ ”
              Me.Controls(“dtpDepDate” & i).Format = dtpCustom
              Me.Controls(“dtpDepDate” & i).CustomFormat = ” ‘ ”
              Next i
              For i = 1 To 5
              Me.Controls(“dtpCheckInDate” & i).Format = dtpCustom
              Me.Controls(“dtpCheckInDate” & i).CustomFormat = ” ‘ ”
              Me.Controls(“dtpCheckOutDate” & i).Format = dtpCustom
              Me.Controls(“dtpCheckOutDate” & i).CustomFormat = ” ‘ ”
              Me.Controls(“dtpCarPickUp” & i).Format = dtpCustom
              Me.Controls(“dtpCarPickUp” & i).CustomFormat = ” ‘ ”
              Me.Controls(“dtpCarDropOff” & i).Format = dtpCustom
              Me.Controls(“dtpCarDropOff” & i).CustomFormat = ” ‘ ”
              Next i
              End Sub
              Private Sub dtpDepDate1_CloseUp()
              FormatDTPicker 1
              End Sub

              Private Sub FormatDTPicker(i As Integer)
              With Me.Controls(“dtpDepDate” & i)
              If .Value = vbNull Then
              .Format = dtpCustom
              .CustomFormat = “X”
              Else
              .Format = dtpShortDate
              End If
              End With
              End Sub

              Private Sub dtpDepDate1_Format(ByVal CallBackField As String, FormattedString As String)
              HandleFormat
              End Sub

              Private Sub HandleFormat(ByVal CallBackField As String, FormattedString As String)
              If CallBackField = “X” Then
              FormattedString = “”
              End If
              End Sub

              Private Sub dtpDepDate1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
              HandleMouseDown 1
              End Sub

              Private Sub HandleMouseDown(i As Integer)
              With Me.Controls(“dtpDepDate” & i)
              If .Value = vbNull Then
              .Value = Now
              End If
              End With
              End Sub

            • #691470

              You will have to create on-line event procedures for the others too. You just have

              Private Sub dtpDepDate1_CloseUp()
              FormatDTPicker 1
              End Sub

              now, you should also create

              Private Sub dtpDepDate2_CloseUp()
              FormatDTPicker 2
              End Sub

              through

              Private Sub dtpDepDate10_CloseUp()
              FormatDTPicker 10
              End Sub

              Similarly

              Private Sub dtpDepDate2_Format(ByVal CallBackField As String, FormattedString As String)
              HandleFormat
              End Sub

              through

              Private Sub dtpDepDate10_Format(ByVal CallBackField As String, FormattedString As String)
              HandleFormat
              End Sub

              and

              Private Sub dtpDepDate2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
              HandleMouseDown 2
              End Sub

              through

              Private Sub dtpDepDate10_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
              HandleMouseDown 10
              End Sub

              You see that you still need to create event handlers for all controls, but you don’t have to copy multiple lines of code into each, you just call a common routine. If you were to use control arrays, this wouldn’t be necessary. As I wrote, the items in a control array use shared event handlers.

            • #691619

              Hans,
              So far, so good. The code works fine for the dtpDepDate fields. I tried to adapt the code for the remaining fields one by one and the code continually stops in the first line of the following code: I get an ambigous name detected message.

              Private Sub FormatDTPicker(i As Integer)
              With Me.Controls(“dtpArrDate” & i)
              If .Value = vbNull Then
              .Format = dtpCustom
              .CustomFormat = “X”
              Else
              .Format = dtpShortDate
              End If
              End With
              End Sub

              As this thread is getting a little long I have attached the whole code as it currently is as a .txt file to this message.
              Thanks heaps.

              Bill

            • #691631

              Bill,

              You asked the same question in post 269268, so you’ll get the same answer: post 269403.

            • #691632

              Hans – Arh, that makes sense to me now. Thanks.

              Bill

    • #689590

      I have a form which emulates the MonthView control. You’re welcome to use it. Instructions for calling its functions are at the top of the code. I usually put a small button next to the textbox which actually calls this form, but you can certainly use a textbox’s click or double-click event to call it. Feel free to try it and e-mail with any questions.

    Viewing 1 reply thread
    Reply To: MonthView Control (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: