473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Populate combo box with next 3 SUNDAYS

105 100+
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...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.   Dim intSunday As Integer
  3.  
  4.   'set the rowsource type
  5.   Me.cmbDate.RowSourceType = "Value List"
  6.  
  7.   'clear the current list and set the column count to 1
  8.   Me.cmbDate.RowSource = vbNullString
  9.   Me.cmbDate.ColumnCount = 1
  10.  
  11.   'populate the list
  12.   For intMonth = 1 To 12
  13.     Me.cmbDate.AddItem Format(DateSerial(Year(Now()), intMonth, 1), "mmmm")
  14.   Next intMonth
  15.  
  16. End Sub
Mar 21 '08 #1
18 2539
missinglinq
3,532 Expert 2GB
This does the job:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Me.cmbDate.RowSourceType = "Value List"
  3. For i = 1 To 3
  4.   Me.cmbDate.AddItem DateAdd("ww", i, Date - (Weekday(Date) - 1))
  5. Next i
  6. End Sub
Linq ;0)>
Mar 21 '08 #2
jmarcrum
105 100+
This does the job:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Me.cmbDate.RowSourceType = "Value List"
  3. For i = 1 To 3
  4.   Me.cmbDate.AddItem DateAdd("ww", i, Date - (Weekday(Date) - 1))
  5. Next i
  6. 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?
Mar 22 '08 #3
jmarcrum
105 100+
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!
Mar 22 '08 #4
missinglinq
3,532 Expert 2GB
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)>
Mar 22 '08 #5
jmarcrum
105 100+
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!!
Mar 22 '08 #6
missinglinq
3,532 Expert 2GB
This code is based on three comboboxes named


  • cmbYears
  • cmbMonths
  • cmbSundayDates
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. 'Set up Years Combobox
  3. Me.cmbYears.RowSourceType = "Value List"
  4. 'Populate the list with Years (4 years back/4 years forward)
  5. FirstYear = DateAdd("yyyy", -4, Date)
  6. For intYear = 0 To 8
  7.  Me.cmbYears.AddItem Year(FirstYear) + intYear
  8. Next intYear
  9.  
  10. 'Set up Months Combobox
  11. Me.cmbMonths.RowSourceType = "Value List"
  12. 'populate the list
  13. For intMonth = 1 To 12
  14.  Me.cmbMonths.AddItem Format(DateSerial(Year(Date), intMonth, 1), "mmmm")
  15. Next intMonth
  16.  
  17. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmbYears_AfterUpdate()
  2.  
  3. If Not IsNull(cmbMonths) Then
  4. 'Set up Sundays Combobox
  5.  
  6. TargetDate = CDate(Me.cmbMonths & "/1/" & Me.cmbYears)
  7.  
  8. Me.cmbSundayDates.RowSourceType = "Value List"
  9. 'clear the current list and set the column count to 1
  10. Me.cmbSundayDates.RowSource = vbNullString
  11. Me.cmbSundayDates.ColumnCount = 1
  12.  
  13.  If Weekday(TargetDate) = 1 Then
  14.   Me.cmbSundayDates.AddItem TargetDate
  15.   For i = 1 To 4
  16.     If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1)), 2) Then
  17.       Me.cmbSundayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1))
  18.     End If
  19.   Next i
  20.  Else
  21.  
  22.   For i = 1 To 5
  23.    If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1)), 2) Then
  24.     Me.cmbSundayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1))
  25.    End If
  26.   Next i
  27.  End If
  28. End If
  29. End Sub
  30.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmbMonths_AfterUpdate()
  2. If Not IsNull(cmbYears) Then
  3. 'Set up Sundays Combobox
  4.  
  5. TargetDate = CDate(Me.cmbMonths & "/1/" & Me.cmbYears)
  6.  
  7. Me.cmbSundayDates.RowSourceType = "Value List"
  8. 'clear the current list and set the column count to 1
  9. Me.cmbSundayDates.RowSource = vbNullString
  10. Me.cmbSundayDates.ColumnCount = 1
  11.  
  12.  If Weekday(TargetDate) = 1 Then
  13.   Me.cmbSundayDates.AddItem TargetDate
  14.   For i = 1 To 4
  15.     If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1)), 2) Then
  16.       Me.cmbSundayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1))
  17.     End If
  18.   Next i
  19.  Else
  20.  
  21.   For i = 1 To 5
  22.    If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1)), 2) Then
  23.     Me.cmbSundayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 1))
  24.    End If
  25.   Next i
  26.  End If
  27. End If
  28. End Sub
  29.  
  30.  

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)>
Mar 23 '08 #7
jmarcrum
105 100+
[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
Mar 23 '08 #8
jmarcrum
105 100+
nevermind i figured it out, man that works fantastically! What a great solution! Thanks Missinglinq!

-Joseph
Mar 23 '08 #9
jmarcrum
105 100+
well...no actually...I still have that Type mismatch...what am I doing wrong? Something to do with Year(FirstYear)
Mar 23 '08 #10
jmarcrum
105 100+
is it trying to treat Year as a field?
Mar 23 '08 #11
jmarcrum
105 100+
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)
Mar 23 '08 #12
missinglinq
3,532 Expert 2GB
Glad you got it working!

Linq ;0)>
Mar 23 '08 #13
jmarcrum
105 100+
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?
Mar 23 '08 #14
missinglinq
3,532 Expert 2GB
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)>

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmbYears_AfterUpdate()
  2.  
  3. If Not IsNull(cmbMonths) Then
  4. 'Set up Wednesdays Combobox
  5.  
  6. TargetDate = CDate(Me.cmbMonths & "/1/" & Me.cmbYears)
  7.  
  8. Me.cmbWednesdayDates.RowSourceType = "Value List"
  9. 'clear the current list and set the column count to 1
  10. Me.cmbWednesdayDates.RowSource = vbNullString
  11. Me.cmbWednesdayDates.ColumnCount = 1
  12.  
  13.  If Weekday(TargetDate) <= 4 Then
  14.   Me.cmbWednesdayDates.AddItem TargetDate + (4 - Weekday(TargetDate))
  15.   For i = 1 To 4
  16.     If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4)), 2) Then
  17.       Me.cmbWednesdayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4))
  18.     End If
  19.   Next i
  20.  Else
  21.  
  22.   For i = 1 To 5
  23.    If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4)), 2) Then
  24.     Me.cmbWednesdayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4))
  25.    End If
  26.   Next i
  27.  End If
  28. End If
  29. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmbMonths_AfterUpdate()
  2. If Not IsNull(cmbYears) Then
  3. 'Set up Wednesdays Combobox
  4.  
  5. TargetDate = CDate(Me.cmbMonths & "/1/" & Me.cmbYears)
  6.  
  7. Me.cmbWednesdayDates.RowSourceType = "Value List"
  8. 'clear the current list and set the column count to 1
  9. Me.cmbWednesdayDates.RowSource = vbNullString
  10. Me.cmbWednesdayDates.ColumnCount = 1
  11.  
  12.  If Weekday(TargetDate) <= 4 Then
  13.   Me.cmbWednesdayDates.AddItem TargetDate + (4 - Weekday(TargetDate))
  14.   For i = 1 To 4
  15.     If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4)), 2) Then
  16.       Me.cmbWednesdayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4))
  17.     End If
  18.   Next i
  19.  Else
  20.  
  21.   For i = 1 To 5
  22.    If Left(TargetDate, 2) = Left(DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4)), 2) Then
  23.     Me.cmbWednesdayDates.AddItem DateAdd("ww", i, TargetDate - (Weekday(TargetDate) - 4))
  24.    End If
  25.   Next i
  26.  End If
  27. End If
  28. End Sub
Linq ;0)>
Mar 23 '08 #15
jmarcrum
105 100+
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?
Mar 23 '08 #16
missinglinq
3,532 Expert 2GB
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)>
Mar 23 '08 #17
jmarcrum
105 100+
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

Expand|Select|Wrap|Line Numbers
  1. Function SetDates()
  2. Dim TargetDate As Date
  3. 'Set up cmbSundayDates
  4.     TargetDate = CDate(Forms!frmEnterNewPrayer!cmbMonths & "/1/" & Forms!frmEnterNewPrayer!cmbYears)
  5.     Forms!frmEnterNewPrayer!cmbSundayDates.RowSourceType = "Value List"
  6.  
  7.     'clear the current list and set the column count to 1
  8.     Forms!frmEnterNewPrayer!cmbSundayDates.RowSource = vbNullString
  9.     Forms!frmEnterNewPrayer!cmbSundayDates.ColumnCount = 1
  10.  
  11.  
  12.     For i = 1 To GetMonthDays
  13.          If Weekday(TargetDate) = Forms!frmEnterNewPrayer!FrameService.Value Then _
  14.             Forms!frmEnterNewPrayer!cmbSundayDates.AddItem TargetDate
  15.  
  16.         TargetDate = DateAdd("d", 1, TargetDate)
  17.     Next i
  18.  
  19. End Function
  20.  
  21. Public Function GetMonthDays()
  22.     TargetDate = CDate(Forms!frmEnterNewPrayer!cmbMonths & "/1/" & Forms!frmEnterNewPrayer!cmbYears)
  23.     TargetDate = DateAdd("m", 1, TargetDate)
  24.     TargetDate = DateAdd("d", -1, TargetDate)
  25.  
  26.     GetMonthDays = Day(TargetDate)
  27. End Function
Mar 24 '08 #18
missinglinq
3,532 Expert 2GB
Glad you got it working for you!

Linq ;0)>
Mar 24 '08 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

10
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 =...
5
by: Filips Benoit | last post by:
Dear all, How can i populate a combo with the field-caption-names of 1 table? Thanks Filip
4
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...
1
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...
7
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...
4
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...
1
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...
5
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...
3
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
jinu1996
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.