Dear programmers;
I want to shorten my codes in adding a time on a combo box with 30 mins time interval
My sample code - Combo1.Additem "7:30 AM"
-
Combo1.Additem "8:00 AM"
-
Combo1.Additem "8:30 AM"
-
Combo1.Additem "9:00 AM"
-
Combo1.Additem "9:30 AM"
-
Combo1.Additem "9:00 AM"
-
Combo1.Additem "9:30 AM"
-
Combo1.Additem "10:00 AM"
-
Combo1.Additem "10:30 AM"
-
Combo1.Additem "11:00 AM"
-
Combo1.Additem "11:30 AM"
-
Combo1.Additem "12:00 PM"
-
Combo1.Additem "12:30 PM"
-
Combo1.Additem "1:00 PM"
16 1508
I'd suggest you create a variable of type Date. Set it to the start time (in this case, 07:30 I suppose). Then do a loop using the DateAdd() function to add 30 minutes, until it reaches the finish time. Each time around the loop, use the Format() function to get the string you want, and add it to the list.
Note, this applies to VB6 and may need modification if using a later version.
I did not try it but I just want to say thank you for your reply. If you won't mind, can you please send me the codes :)
I did not try it but I just want to say thank you for your reply. If you won't mind, can you please send me the codes :)
Further don't ask for code before trying yourself what was suggested to you.
Sorry guys... I tried and I've spend so much time but still i can't make it...
Can you please tell me what's wrong with these codes? - Dim d As Date
-
d="#7:00 AM#"
-
Do Until d = #8:00 PM#
-
Combo1.AddItem DateAdd("n", 30, d)
-
Loop
-
Sorry guys... I tried and I've spend so much time but still i can't make it...
Can you please tell me what's wrong with these codes?
Sure. You've got unnecessary quotes on line 2, and you're not incrementing the value of d as you go around the loop (which means it will go forever).
Also, I suspect you will want to use the Format() function to format the value that you place in the combobox. But that can wait until you get the loop working.
hello killer42. This is my modification. - Dim i As Long
-
Dim d As Date
-
Dim e As Date
-
-
d=#7:00:00 AM#
-
-
Do
-
e=DateAdd("n", i, d)
-
Combo1.AddItem e
-
i=i+30 'add 30 mins interval
-
Loop Until i=810 '8:00:00 PM
These codes work but I can't really make it how to add 30 mins interval on variable "e". It goes looping and looping as you've said. By the way, regarding about the Format function I can't add AM/PM at the end and also the time format is 24-hour. How to change it to 12-hour format?
hello killer42. Is this one correct?
It's looking pretty good. But why not just reuse d instead of creating another variable e? And I wouldn't increment i; you're going to be incrementing your time by half an hour longer each time. Just leave i as it is, and loop until d hits the finishing time.
As for Format(), hit the books. :) The documentation describes it pretty thoroughly.
Thanks for your immediate reply, killer42 :)
if it help i came up with this... - Public Class Form1
-
Dim start1 As String = "00:00"
-
Dim end1 As String = "12:00"
-
Dim i As Integer
-
Dim a As String
-
Dim start2 As String
-
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
-
box.Items.Clear()
-
-
box.Items.Add(start1)
-
'For i = 1 To Len(start1)
-
' a = Mid(start1, i, 1)
-
' If a >= "0" Or a <= "9" Then
-
' start2 = start2 + a
-
' End If
-
'Next
-
'start1 = start2
-
-
'take out :
-
-
While start1 < end1
-
start2 = ""
-
For i = 1 To Len(start1)
-
a = Mid(start1, i, 1)
-
If a = "0" Or a = "1" Or a = "2" Or a = "3" Or a = "4" Or a = "5" Or a = "6" Or a = "7" Or a = "8" Or a = "9" Then
-
start2 = start2 + a
-
End If
-
Next
-
start1 = start2
-
-
'Dim convert As String
-
a = Mid(start1, 3, 2)
-
If a = "30" Then
-
start1 = start1 - 30
-
start1 = start1 + 100
-
Else
-
start1 = start1 + 30
-
End If
-
-
'convert here
-
If Len(start1) = 3 Then
-
start1 = "0" + Mid(start1, 1)
-
End If
-
If Len(start1) = 2 Then
-
start1 = "00" + Mid(start1, 1)
-
End If
-
-
start1 = Mid(start1, 1, 2) & ":" & Mid(start1, 3, 2)
-
box.Items.Add(start1)
-
-
End While
-
-
End Sub
-
End Class
-
if it help i came up with this...
Thanks for that, James. Does it work? I have to admit, it seems like a lot of extra coding for quite a simple task.
Hello James
I'm looking for a vb 6.0 code, but thanks for your reply. :)
Thanks for that, James. Does it work? I have to admit, it seems like a lot of extra coding for quite a simple task.
haha killer you should know by now... i don't write 3 lines of code when i can write 53 lines ;)
haha killer you should know by now... i don't write 3 lines of code when i can write 53 lines ;)
I think in solving a problem like this we should apply the KISS principle.
hello killer42. This is my modification. - Dim i As Long
-
Dim d As Date
-
Dim e As Date
-
-
d=#7:00:00 AM#
-
-
Do
-
e=DateAdd("n", i, d)
-
Combo1.AddItem e
-
i=i+30 'add 30 mins interval
-
Loop Until i=810 '8:00:00 PM
These codes work but I can't really make it how to add 30 mins interval on variable "e". It goes looping and looping as you've said. By the way, regarding about the Format function I can't add AM/PM at the end and also the time format is 24-hour. How to change it to 12-hour format?
Hi,
Try this : -
Combo1.Clear
-
Dim t As Date
-
Dim i As Integer
-
t = CDate("07:00:00")
-
Do
-
t = DateAdd("n", 30, t)
-
Combo1.AddItem Format(t, "hh:mm AM/PM")
-
i = i + 1
-
Loop Until i >= 26
-
-
Regards
Veena
Hi,
Try this : -
Combo1.Clear
-
Dim t As Date
-
Dim i As Integer
-
t = CDate("07:00:00")
-
Do
-
t = DateAdd("n", 30, t)
-
Combo1.AddItem Format(t, "hh:mm AM/PM")
-
i = i + 1
-
Loop Until i >= 26
-
-
Regards
Veena
Hi Veena
I just want to say THANK YOU VERY MUCH. Atlast! I've got what I wanted to know ;)
ye this just demonstrates how much i have to learn :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: kaptain kernel |
last post by:
i've got a while loop thats iterating through a text file and pumping the
contents into a database. the file is quite large (over 150mb).
the looping causes my CPU load to race up to 100 per...
|
by: Stegga |
last post by:
Hi there,
I'm trying to add 1 second incrementally to a timestamp while looping
through it and I am pulling my hair out.
Please can someone help me?
example of code:
$calc_timestamp =...
|
by: Joseph Garvin |
last post by:
When I first came to Python I did a lot of C style loops like this:
for i in range(len(myarray)):
print myarray
Obviously the more pythonic way is:
for i in my array:
print i
|
by: Luke - eat.lemons |
last post by:
Sorry for the post in this NG but im short on time to get this working
and i haven't seem to of got a response anywhere else.
Im pretty new to asp so all light on this question would be great.
...
|
by: Dököll |
last post by:
Hiya, Partners!
I have been into it for 12 hours straight this week-end, my son is very unhappy. Looks like I am getting pretty close but need your help, Again. I will post my first...
|
by: anthon |
last post by:
Hi all - first post!
anywho;
I need to create a function for speeding up and down a looping clip.
imagine a rotating object, triggered by an action, and slowly decreasing in speed, till it...
|
by: XpatienceX |
last post by:
Hi,
Can someone help me with this assignment, I am confused of what is needed to be done here.
I am suposed to design a program that models a worm's behavior in the following scenario:
A...
|
by: assgar |
last post by:
Hi
I am having problem with my loping.
I don't know if I have chosen the correct approach.
GOAL:
I need to insert into a table event types for a specific date range.
The calendar the event...
|
by: Davaa |
last post by:
Dear all,
I am a student making a MS Form application in C++. I would ask a question about "Timer". Sample code which I am developing is below.
private: System::Void...
|
by: tddeffner |
last post by:
I have a form i need to create that involves looping dropdowns in asp.net. The form sets a time in a open time and a close time dropdownlist for each weekday. So I need it to loop seven times. I...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |