Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

I have 3 combo listboxes to input date a transaction took place.  How do I write an array for years with array of 97 items

Posted on 2003-03-30
9
Medium Priority
?
152 Views
Last Modified: 2012-05-04
I am working on a an ATM program, but I am really struggling.  Any help would be appreciated.

Problem:
Need to know how to fill the date information in 3 combo listbox that are designed for Month, Day, and Years.  The years will run from 2002 to 2099.  Also, if I use the list property to show the days in the months, how do I show that February cannot use the 30th and 31st day repectively, and also it can use the 29th day only if the year is a leapyear?

Thank you all kindly
0
Comment
Question by:zee07836
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 14

Accepted Solution

by:
aelatik earned 620 total points
ID: 8234735
'Hi zee07836,

'Put 3 combo's on your form and paste the following code...

Private Sub Combo2_Click()
    Combo3.Clear
For i = 1 To 31
    If IsDate(i & "/" & Combo2.ListIndex + 1 & "/" & Combo1) Then Combo3.AddItem i
Next
    End Sub
Private Sub Combo1_Click()
    Combo3.Clear
For i = 1 To 31
    If IsDate(i & "/" & Combo2.ListIndex + 1 & "/" & Combo1) Then Combo3.AddItem i
Next
End Sub

Private Sub Form_Load()
For y = 2000 To 2099
    Combo1.AddItem y
    Next y
Combo1.ListIndex = 0
For m = 1 To 12
    Combo2.AddItem MonthName(m)
Next m
    Combo2.ListIndex = 0
End Sub
0
 
LVL 2

Expert Comment

by:Jzaltheral
ID: 8234842
Will the user be selecting the year first or the month?  Or do you want it to matter?  That determines how best to code to form to do exactly what you need.

I can at least get you started with loading the date information -

For the months combo box, set the names in the list property - Just click the drop down arrow on the list property and type them in, hitting CTRL-ENTER after each one to move to a new line.  Since these don't change, there's no need to load them at run time - we can do it at design time.

To load the years you can put the following code in your form's Load event

'Load the values into the years combo box.
Dim Year As Long
Year = 2002

For Year = Year To 2099
    cmbYears.AddItem CStr(Year)
Next Year
   
End Sub

Once I get the answers to the questions I asked earlier, I should be able to help you out with the rest.

Jzal
0
 
LVL 2

Expert Comment

by:Jzaltheral
ID: 8234848
Oops.  Sorry aleatik - I was busy writing my own answer, and missed you posting yours.

Jzal
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:avya2k
ID: 8236570
Why don't you use Date picker control instead
To insert DatePicker control
open Project\Components
and select "Microsoft windows common controls-2.6.0(MSCOMCT2.ocx)

and insert DTPicker control
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8237425
I would agree with avya2k that you should use the Date picker control. However, if for some reason, you're reluctant to use that one, then you can have the 3 combo-boxes (say, 'dt', 'mn' and 'yr', with 'dt' holding 1 through 31 in string-form, 'mn' holding "January", "February", ...., "December", and 'yr' holding 2002 through 2009 in string-form), then you ca apply the following validation:

Dim tempDate As String
Dim myDate As Date

tempDate = mn & " " & dt & ", " & yr

If Not IsDate(tempDate) Then
  MsgBox "Invalid date. Please re-enter. "
Else
  myDate = CDate (tempDate) ' convert the string to date-type
  .. ' do your thing
End If


Hope that helps!

Mayank.
0
 
LVL 1

Expert Comment

by:hazgod
ID: 8242022
dim index as integer
dim leapYear as boolean

select case comboMonth.text

'months with 30 days
  case "September":
  case "April":
  case "June":
  case "November":
    comboDay.clear
    for index = 1 to 30
      comboDay.additem index
    next

  case "February":
    'need code in here to detect leap year
    'if year is a leap year, leapYear = true
    'else leapYear = false
    comboDay.clear
    if leapYear = true then
      for index = 1 to 29
        comboDay.additem index
      next
    else
      for index = 1 to 28
        comboDay.additem index
      next
    end if

  case else:
    comboDay.clear
    for index = 1 to 31
      comboDay.additem index
    next

end select
0
 

Author Comment

by:zee07836
ID: 8267050
Thank you to all of you who so graciously answered my question.

Much Thanks
Zee
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8267219
So accept one of the answers and rate it :-)
0
 

Author Comment

by:zee07836
ID: 8275278
Aelatik et All, I built projects with the respective codes that each of you posted; although they all were intellectually stimulating :), I found the one from Alelatik was the easiest to apply.

Aelatik, as shown below, I slightly modified the code you sent me (changed the control names)  and placed the codes on the form and in 3 combo boxes as shown and it worked perfectly.  

Private Sub Form_Load()

Dim y As Integer
Dim m As Integer


For y = 2000 To 2099
   CboYear.AddItem y  
   Next y
   CboYear.ListIndex = 0


For m = 1 To 12
   CboMonth.AddItem MonthName(m)
Next m
  CboMonth.ListIndex = 0

End Sub
'__________________________________________________________
Private Sub CboMonth_Click()
   CboDay.Clear
Dim i As Integer
For i = 1 To 31
   If IsDate(i & "/" & CboMonth.ListIndex + 1 & "/" & CboYear) Then CboDay.AddItem i
Next
   End Sub
'__________________________________________________________

Private Sub CboYear_Click()
Dim i As Integer
   CboDay.Clear
For i = 1 To 31
   If IsDate(i & "/" & CboMonth.ListIndex + 1 & "/" & CboYear) Then CboDay.AddItem i
Next
End Sub
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question