Results 1 to 5 of 5

Thread: [RESOLVED] use combobox as Dtpicker

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Resolved [RESOLVED] use combobox as Dtpicker

    Hello
    I want to use a combo to show only months and years
    I found this sub but I was unable to make it in use.
    I need an expert to tell me how to use it
    thank you

    Code:
    Private Sub FillMonths(ByVal MFStart As Date, ByVal MFEnd As Date) 
        Dim Tdate As Date
        cmbMonth.Clear
        Tdate = MFStart
        Do
            cmbMonth.AddItem Format(Tdate, "mmm-yyyy")
            Tdate = DateAdd("m", 1, Tdate)
        Loop Until Tdate > MFEnd
    End Sub

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: use combobox as Dtpicker

    hi
    here a few samples, add the Controls you see in the Code

    Code:
    Option Explicit
     
     Dim ActiveMonth As Long
     Dim ActiveYear As Long
    
    
    
    
    Private Sub cboMonth_Click()
    
    Static IniDone
          If Not IniDone Then
             IniDone = True
             Exit Sub
          End If
    
    txtStartdate = FirstDayOfMonth(cboMonth.ListIndex + 1, cboYear)
    txtEndDate = LastDayOfMonth(cboMonth.ListIndex + 1, cboYear)
    
    
      Dim i As Long
      Dim mDays As Long
      Dim mFrom As Date
      Dim mTo As Date
      Dim mDay As Date
       
          mFrom = txtStartdate
          mTo = txtEndDate
          
          List1.Clear
       
          mDays = DateDiff("d", mFrom, mTo) + 1
         
          
            For i = 1 To mDays
                mDay = mFrom + i - 1
                List1.AddItem mDay
            Next
    
    End Sub
    
    Private Sub Form_Load()
    ActiveMonth = IIf(ActiveMonth = 0, Month(Now), ActiveMonth)
    ActiveYear = IIf(ActiveYear = 0, Year(Now), ActiveYear)
       
       iniCombos
    txtStartdate = FirstDayOfMonth(cboMonth.ListIndex + 1, cboYear)
    txtEndDate = LastDayOfMonth(cboMonth.ListIndex + 1, cboYear)
    
    End Sub
    
    Private Function iniCombos()
       Dim i As Long
          For i = 1 To 12
             cboMonth.AddItem MonthName(i)
          Next
          For i = 2015 To Year(Now) + 2
             cboYear.AddItem i
          Next
          cboMonth.ListIndex = ActiveMonth - 1
          cboYear.ListIndex = ActiveYear - 2015
    
    End Function
    
     Function FirstDayOfMonth(ActiveMonth As Long, ActiveYear As Long) As Date
        FirstDayOfMonth = DateSerial(ActiveYear, ActiveMonth, 1)
    End Function
     
    Function LastDayOfMonth(ActiveMonth As Long, ActiveYear As Long) As Date
        LastDayOfMonth = DateSerial(ActiveYear, ActiveMonth + 1, 0)
    End Function
    hth
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: use combobox as Dtpicker

    thank you sir
    so not possble to use only one combo?
    if so I'm satisfied with using two combos.
    thanks again

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: use combobox as Dtpicker

    Quote Originally Posted by newbie2 View Post
    Code:
    Private Sub FillMonths(ByVal MFStart As Date, ByVal MFEnd As Date) 
        Dim Tdate As Date
        cmbMonth.Clear
        Tdate = MFStart
        Do
            cmbMonth.AddItem Format(Tdate, "mmm-yyyy")
            Tdate = DateAdd("m", 1, Tdate)
        Loop Until Tdate > MFEnd
    End Sub
    What is the problem with the above? Just add a combobox named cmbMonth to your form. Add a button and in the click event, call the function to tell it the Start date and End date
    Code:
     FillMonths Date, DateAdd("yyyy", 1, Date)
    Tip: Change the combobox style to 2 (dropdown list) so users must select one from the list vs entering their own.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: use combobox as Dtpicker

    LaVolpe
    thank you a lot
    it is working fine

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width