-
Jun 21st, 2019, 06:40 AM
#1
Thread Starter
Fanatic Member
[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
-
Jun 21st, 2019, 07:26 AM
#2
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.
-
Jun 21st, 2019, 06:13 PM
#3
Thread Starter
Fanatic Member
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
-
Jun 21st, 2019, 06:31 PM
#4
Re: use combobox as Dtpicker
Originally Posted by newbie2
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.
-
Jun 21st, 2019, 07:07 PM
#5
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|