Hi everyone!
I have a form that when the user opens it, the Date combo box on the form is populated with every month of the year. But I don't want it to do that! I want it to populate with the next 3 Sundays chronologically for the user to select from. How do I need to change my code? Here's my OnLoad event for the form... - Private Sub Form_Load()
-
Dim intSunday As Integer
-
-
'set the rowsource type
-
Me.cmbDate.RowSourceType = "Value List"
-
-
'clear the current list and set the column count to 1
-
Me.cmbDate.RowSource = vbNullString
-
Me.cmbDate.ColumnCount = 1
-
-
'populate the list
-
For intMonth = 1 To 12
-
Me.cmbDate.AddItem Format(DateSerial(Year(Now()), intMonth, 1), "mmmm")
-
Next intMonth
-
-
End Sub
18 2539
This does the job: - Private Sub Form_Load()
-
Me.cmbDate.RowSourceType = "Value List"
-
For i = 1 To 3
-
Me.cmbDate.AddItem DateAdd("ww", i, Date - (Weekday(Date) - 1))
-
Next i
-
End Sub
Linq ;0)>
This does the job: - Private Sub Form_Load()
-
Me.cmbDate.RowSourceType = "Value List"
-
For i = 1 To 3
-
Me.cmbDate.AddItem DateAdd("ww", i, Date - (Weekday(Date) - 1))
-
Next i
-
End Sub
Linq ;0)>
Thanks man! It works great!
But hey, what if....I had a combo box that listed each month of the year for the user to select...and then beside that...i had a combo box with the last 4 years up to the present day + the next 4 years....
I want the user to be able to select a year first, then select a month, and then the Sunday combo box populates with all of the Sundays for that particular month. Is there a way to do this with the code we have so far?
Like, for example...
I have a year combo box (2007, 2008, and 2009). I also have a month combo box (January, February, etc.). If the user selects 2008 and then selects July, I want the Sunday combo box to populate with ONLY the Sundays of that month, according to that selected year.
Thanks!
We can do that with a combination of your code and my code and some additional code. I've actually got it working except for one little snag; I need to figure out which months have four Sundays and which have five. As it is now, using five for all months, it lists the first Sunday of the following month if the month in question only has four.
I've got some things I have to get done today, but I'll work on it later today/tomorrow and post the final solution back here.
Linq ;0)>
We can do that with a combination of your code and my code and some additional code. I've actually got it working except for one little snag; I need to figure out which months have four Sundays and which have five. As it is now, using five for all months, it lists the first Sunday of the following month if the month in question only has four.
I've got some things I have to get done today, but I'll work on it later today/tomorrow and post the final solution back here.
Linq ;0)>
Wow thanks so much missinglinq!!
This code is based on three comboboxes named - cmbYears
- cmbMonths
- cmbSundayDates
- Private Sub Form_Load()
-
'Set up Years Combobox
-
Me.cmbYears.RowSourceType = "Value List"
-
'Populate the list with Years (4 years back/4 years forward)
-
FirstYear = DateAdd("yyyy", -4, Date)
-
For intYear = 0 To 8
-
Me.cmbYears.AddItem Year(FirstYear) + intYear
-
Next intYear
-
-
'Set up Months Combobox
-
Me.cmbMonths.RowSourceType = "Value List"
-
'populate the list
-
For intMonth = 1 To 12
-
Me.cmbMonths.AddItem Format(DateSerial(Year(Date), intMonth, 1), "mmmm")
-
Next intMonth
-
-
End Sub
- Private Sub cmbYears_AfterUpdate()
-
-
If Not IsNull(cmbMonths) Then
-
'Set up Sundays Combobox
-
-
TargetDate = CDate(Me.cmbMonths & "/1/" & Me.cmbYears)
-
-
Me.cmbSundayDates.RowSourceType = "Value List"
-
'clear the current list and set the column count to 1
-
Me.cmbSundayDates.RowSource = vbNullString
-
Me.cmbSundayDates.ColumnCount = 1
-
-
If Weekday(TargetDate) = 1 Then
-
Me.cmbSundayDates.AddItem TargetDate
-
For i = 1 To 4
-
If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1)), 2) Then
-
Me.cmbSundayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1))
-
End If
-
Next i
-
Else
-
-
For i = 1 To 5
-
If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1)), 2) Then
-
Me.cmbSundayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1))
-
End If
-
Next i
-
End If
-
End If
-
End Sub
-
- Private Sub cmbMonths_AfterUpdate()
-
If Not IsNull(cmbYears) Then
-
'Set up Sundays Combobox
-
-
TargetDate = CDate(Me.cmbMonths & "/1/" & Me.cmbYears)
-
-
Me.cmbSundayDates.RowSourceType = "Value List"
-
'clear the current list and set the column count to 1
-
Me.cmbSundayDates.RowSource = vbNullString
-
Me.cmbSundayDates.ColumnCount = 1
-
-
If Weekday(TargetDate) = 1 Then
-
Me.cmbSundayDates.AddItem TargetDate
-
For i = 1 To 4
-
If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1)), 2) Then
-
Me.cmbSundayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1))
-
End If
-
Next i
-
Else
-
-
For i = 1 To 5
-
If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1)), 2) Then
-
Me.cmbSundayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1))
-
End If
-
Next i
-
End If
-
End If
-
End Sub
-
-
Obviously, this is a rather complicated bit of code, so be very careful if you decide to substitute your own names for the combobox names in the code. It would probably be safer to simply use the names as given.
Anytime you have complicated code, and you're thinking about modifying it, it's always a good idea to make a copy of the working code for safekeeping. Either copy and paste the code into a Word document, or simply copy the form, giving it an appropriate name.
I've tested this fairly extensively, using 12 months from three separate years, one of which (2008) is a Leap Year, and checking the results against actual calendars. In all cases the results were accurate, but if you find any problems, post the year/month you were inputting as well as the final results.
Good luck!
Linq ;0)>
[code]Private Sub Form_Load()
'Set up Years Combobox
Me.cmbYears.RowSourceType = "Value List"
'Populate the list with Years (4 years back/4 years forward)
FirstYear = DateAdd("yyyy", -4, Date)
For intYear = 0 To 8
Me.cmbYears.AddItem Year(FirstYear) + intYear
Next intYear
Linq ;0)>
Hey missinglinq,
I have a Type mismatch on this line....
Me.cmbYears.AddItem Year(FirstYear) + intYear
nevermind i figured it out, man that works fantastically! What a great solution! Thanks Missinglinq!
-Joseph
well...no actually...I still have that Type mismatch...what am I doing wrong? Something to do with Year(FirstYear)
is it trying to treat Year as a field?
THAT.....now THAT is EXACTLY what it was doing....YAY!!! it works again!
Here's how I fixed it....
To avoid error when a field/control is named Year:
MyYear = VBA.Year(Now)
Glad you got it working!
Linq ;0)>
Glad you got it working!
Linq ;0)>
Hey Missinglinq,
What about Wednesdays? Is it done the same way, but instead of TargetDate = 1 is it 4?
You didn't really think it'd be that simple, did you? If you're going to try to place all of this on one form, be very careful integrating the code to populate both comboboxes (for Sundays and Wedsnesdays) as it will be very easy to scrooch the pooch!! Back up what you already have working and keep it in a safe place!
Note that I've named the Wednesday combobox cmbWednesdayDates.
Good luck!
Linq ;0)> - Private Sub cmbYears_AfterUpdate()
-
-
If Not IsNull(cmbMonths) Then
-
'Set up Wednesdays Combobox
-
-
TargetDate = CDate(Me.cmbMonths & "/1/" & Me.cmbYears)
-
-
Me.cmbWednesdayDates.RowSourceType = "Value List"
-
'clear the current list and set the column count to 1
-
Me.cmbWednesdayDates.RowSource = vbNullString
-
Me.cmbWednesdayDates.ColumnCount = 1
-
-
If Weekday(TargetDate) <= 4 Then
-
Me.cmbWednesdayDates.AddItem TargetDate + (4 - Weekday(TargetDate))
-
For i = 1 To 4
-
If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4)), 2) Then
-
Me.cmbWednesdayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4))
-
End If
-
Next i
-
Else
-
-
For i = 1 To 5
-
If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4)), 2) Then
-
Me.cmbWednesdayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4))
-
End If
-
Next i
-
End If
-
End If
-
End Sub
- Private Sub cmbMonths_AfterUpdate()
-
If Not IsNull(cmbYears) Then
-
'Set up Wednesdays Combobox
-
-
TargetDate = CDate(Me.cmbMonths & "/1/" & Me.cmbYears)
-
-
Me.cmbWednesdayDates.RowSourceType = "Value List"
-
'clear the current list and set the column count to 1
-
Me.cmbWednesdayDates.RowSource = vbNullString
-
Me.cmbWednesdayDates.ColumnCount = 1
-
-
If Weekday(TargetDate) <= 4 Then
-
Me.cmbWednesdayDates.AddItem TargetDate + (4 - Weekday(TargetDate))
-
For i = 1 To 4
-
If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4)), 2) Then
-
Me.cmbWednesdayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4))
-
End If
-
Next i
-
Else
-
-
For i = 1 To 5
-
If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4)), 2) Then
-
Me.cmbWednesdayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4))
-
End If
-
Next i
-
End If
-
End If
-
End Sub
Linq ;0)>
Hey Missinglinq!
That seems to be working really nicely! However, you remember earlier when you were saying that the combo box was spilling over into the next month? I keep getting one extra Wednesday like that. Is that an easy fix?
In converting it from Sundays to Wednesdays I've probably missed something. I'll look at it as soon as I can in the morning.
Linq ;0)>
Hey Missinglinq,
I took that code and ran with it a bit...This function will do what we are trying to do in a shorter amount of lines - Function SetDates()
-
Dim TargetDate As Date
-
'Set up cmbSundayDates
-
TargetDate = CDate(Forms!frmEnterNewPrayer!cmbMonths & "/1/" & Forms!frmEnterNewPrayer!cmbYears)
-
Forms!frmEnterNewPrayer!cmbSundayDates.RowSourceType = "Value List"
-
-
'clear the current list and set the column count to 1
-
Forms!frmEnterNewPrayer!cmbSundayDates.RowSource = vbNullString
-
Forms!frmEnterNewPrayer!cmbSundayDates.ColumnCount = 1
-
-
-
For i = 1 To GetMonthDays
-
If Weekday(TargetDate) = Forms!frmEnterNewPrayer!FrameService.Value Then _
-
Forms!frmEnterNewPrayer!cmbSundayDates.AddItem TargetDate
-
-
TargetDate = DateAdd("d", 1, TargetDate)
-
Next i
-
-
End Function
-
-
Public Function GetMonthDays()
-
TargetDate = CDate(Forms!frmEnterNewPrayer!cmbMonths & "/1/" & Forms!frmEnterNewPrayer!cmbYears)
-
TargetDate = DateAdd("m", 1, TargetDate)
-
TargetDate = DateAdd("d", -1, TargetDate)
-
-
GetMonthDays = Day(TargetDate)
-
End Function
Glad you got it working for you!
Linq ;0)> Sign in to post your reply or Sign up for a free account.
Similar topics
by: Lukelrc |
last post by:
Hi,
I have an array - ListOfFiles - that i want use to populate an combo.
I've attempted to do it like this
<select name="txtAvailable" rows="4" id="txtAvailable">
<Script Language =...
|
by: Filips Benoit |
last post by:
Dear all,
How can i populate a combo with the field-caption-names of 1 table?
Thanks
Filip
|
by: Mike L |
last post by:
I'm open for any suggestions on how to better program this. I want the user
to select a license from a combo box, cboPrivilege and then the user will
click the add button, then a record will be...
|
by: freekedoutfish |
last post by:
Hi. New member here
Im sat at work, pounding my head off the desk because this tiny bit of simple code refuses to work.
The sub is intended to pull data from the "companyname" column in the...
|
by: nareshpulipati |
last post by:
Hi all,
I am new to VB .net.
Iam trying to populate the database item into combo box.
Database Type:SQL(ODBC)
My code retuns no value in combo box
Public Class Form1
Private Sub...
|
by: whamo |
last post by:
I have the need to populate a field based on the selection in a combo box.
Starting out simple. (2) tables tbl_OSE_Info and tbl_Input;
tbl_OSE_Info has three fields: Key, OSE_Name and OSE_Wt...
|
by: jmarcrum |
last post by:
Hey everyone!!
I have a form with a year combo box, a month combo box, a Day combo box, and an Option Group with 2 choices (Sunday and Wednesday). If the user selects a year, then selects a...
|
by: giandeo |
last post by:
Hello Experts.
Could you find a solution for this problem please!
I have the following tables in Access Database
Table Name: origin
Fields Names: country, countrycode
Table Name: make...
|
by: PGM |
last post by:
I am trying to populate a combo box using a simple SQL query to a database
but all I get in the combo box is "System._ComObject" . Any suggestions are
greatly appreciated
On Error Resume Next
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |