Visual Basic Helper Routines
Pure VB: Fill a Combo with Specified Time Intervals
     
Posted:   September 16, 2002
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   Newsgroup contributors
     

Related:  

Pure VB: Methods to Determine the Last Day of the Month
Pure VB: Methods to Determine Number of Days in a Month
Pure VB: Determining Leap Years
VarMonthName: Retrieve Localized Month Names
VarWeekdayName: Retrieve Localized Weekday Names
     
 Prerequisites
None.

A routine to populate a combo box with a full day of times at the specified time intervals. As coded, any interval between 1 and 60 minutes is accommodated - the illustration shows several popular ones. In addition, by specifying vbShortDate or vbLongDate, you can tailor the presentation of the dates.
 BAS Module Code
None.

 Form Code
Toss a command button six combos on a form (Combo1-Combo6), and add the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Form_Load()
   
   Command1.Caption = "Fill"
   
End Sub


Private Sub Command1_Click()

   FillComboTimes Combo1, 5, vbShortTime
   FillComboTimes Combo2, 10, vbShortTime
   FillComboTimes Combo3, 15, vbShortTime
   FillComboTimes Combo4, 20, vbShortTime
   FillComboTimes Combo5, 30, vbShortTime
   FillComboTimes Combo6, 60, vbShortTime

End Sub


Private Sub FillComboTimes(cbo As ComboBox, _
                           period As Long, _
                           fmt As VbDateTimeFormat)

   Dim cnt As Long
   Dim t As Date
   
   If period < 1 Or period > 60 Then Exit Sub
   
   With cbo
      
      .Clear
   
      For cnt = 1 To (24 * (60 \ period))
         t = t + DateAdd("n", period, 0)
         .AddItem FormatDateTime(t, fmt)
      Next
   
      .ListIndex = 0
   
   End With

End Sub
 Comments
VB versions not supporting the FormatDateTime method should use :

    cbo.AddItem Format$(t, fmt)

instead. In addition, change the definition of fmt from As Long to As String, and pass the appropriate format$-compatible time format, ie "short time" or "long time" (to get the am pm designations).


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter