View Full Version : timespan in vb6
Zero Tolerance
06-03-2004, 09:14 AM
All,
I'm working on a form in VB6 and one part of it is to enter a start time in hours and minutes then a stop time in hours and minutes. There is a cmd button to display the span in hours and minutes in 2 labels.
I have gotten this to work somewhat with simple if statements and the mod operator. However the way I have done it still gives logic errors with certain times.
I know from reading there is a timespan function in vb that should be able to do this much simpler. Maybe I will have to reformat the form and use one textbox for hours and minutes instead of 2 (1 for hours, 1 for minutes)...as well as displaying the duration(span) in one label instead of two. That is no problem. I just need some help in how to do it.
See the screenie of the part of the form I am working on.
PS....this is the same project that I posted the Printer.Print question to. Thanks to Bond and Snake for the help. I did get the printer to work like I wanted it to....however I changed the program to send all information to Word (OLE) so there would be a "Soft" copy of the info stored on the drive/disc as well as a "Hard" copy for the file. This proved to be much better because if one of our Case Managers needed to append the notes section at a later date, then they can.
Again...Thanks!
Will the start and stop times always be during the same day? You could just use the DateDiff function:
Dim dtStart As Date, dtEnd As Date
dtStart = TimeValue(txtStart.Text)
dtEnd = TimeValue(txtEnd.Text)
MsgBox "Hours between = " & DateDiff("h", dtEnd, dtStart)
MsgBox "Minutes between = " & DateDiff("n", dtEnd, dtStart)
Obviously, you'll still need to use the Mod function:
Dim iHours As Integer, iMins As Integer
iMins = DateDiff("n", dtEnd, dtStart)
iHours = iMins / 60
iMins = iMins Mod 60
Zero Tolerance
06-04-2004, 10:32 AM
yes.....all during the same day...I would not expect the span to be more than 3 hours tops
I am really busy today....so I may not be able to try your suggestions....soon as I get time I will and report back.
right now I am doing it with just converting everything to minutes...then dividing by 60 for the hours...and mod (%) by 60 for the minutes display....works ok but id rather de-bloat the code with a working function for the purpose
thanks!
sssssssssssBANG!
Zero Tolerance
06-04-2004, 10:33 AM
lol..also i always forget that vb declares minutes with "n" cuz "m" is used for months
:D
Zero Tolerance
06-04-2004, 03:47 PM
Bond...I must be missing sumthin....I tried your code and it doesnt give correct logic
Any suggestions?
btw...here is how I incorped your code into the form.....let it be known that I done away with the seperate txtboxes for hours/mins...and had one txtbox for start hours/mins...one for stop hrs/mins....but it gave me a really strange result
'Dim dtStart As Date, dtEnd As Date
'dtStart = TimeValue(txtStart.Text)
'dtEnd = TimeValue(txtStop.Text)
'Dim iHours As Integer, iMins As Integer
'iMins = DateDiff("n", dtEnd, dtStart)
'iHours = iMins / 60
'iMins = iMins Mod 60
'lblDurationHrs.Caption = DateDiff("h", dtEnd, dtStart)
'lblDurationMins = DateDiff("n", dtEnd, dtStart)
'lblDurationHrs.Caption = iHours
'lblDurationMins.Caption = iMins
Here's the full code:
Private Sub cmdCalculate_Click()
' Make sure both start and end times were entered...
If Len(txtStart.Text) = 0 Then
MsgBox "You must enter a start time."
txtStart.SetFocus
Exit Sub
End If
If Len(txtEnd.Text) = 0 Then
MsgBox "You must enter an end time."
txtEnd.SetFocus
Exit Sub
End If
' Get the times entered...
Dim dtStart As Date
dtStart = TimeValue(txtStart.Text)
Dim dtEnd As Date
dtEnd = TimeValue(txtEnd.Text)
' Make sure these are valid times...
If Not IsDate(dtStart) Then
MsgBox "The start time entered was not valid."
txtStart.SetFocus
Exit Sub
End If
If Not IsDate(dtEnd) Then
MsgBox "The end time entered was not valid."
txtEnd.SetFocus
Exit Sub
End If
' Calculate the difference...
Dim iHours As Integer, iMins As Integer
iMins = DateDiff("n", dtStart, dtEnd)
iHours = iMins / 60
iMins = iMins Mod 60
' Display the result...
lblResult.Caption = iHours & " hours, " & iMins & " minutes"
End Sub
Just add two textboxes (txtStart, txtEnd), a Label (lblResult), and a button (cmdCalculate) and paste this code into the form.
You can use military format when entering the times, or you can add "AM" or "PM" to qualify your times.
Here's a screenshot:
Zero Tolerance
06-04-2004, 04:54 PM
thanks....I have only had time here and there today to play with it.....and now its 5 b4 5 so its TGIF
I will get back on this monday....and report
Thanks Again!
ssssssssssBANG!
Zero Tolerance
06-07-2004, 02:54 PM
havent had a great deal of time to mess with this today because of some CISCO router problems all morning.
however I did take a few minutes out just now to toy with it and found that there is still a logic error even with the full code.
I at first thought it was ok....and was testing with times like 12:00PM start and 3:50 PM stop.....which gave the correct span of 3hours and 50 mins....BUT if you enter 8:00AM as a start time and 8:50AM as stop.....it gives 1:50 mins as span...when it is only 50 mins.
Maybe I could figure out why it is adding an hour if I had more time.....maybe its something you will spot out in your code right away and can reply on it.
Either way...thanks for the help thus far....
Here is how I edited your code to work better with my form
Private Sub cmdProcess_Click()
' Make sure both start and end times were entered...
If Len(txtStartHrs.Text) = 0 Then
MsgBox "You must enter a start time."
txtStart.SetFocus
Exit Sub
End If
If Len(txtStopHrs.Text) = 0 Then
MsgBox "You must enter an end time."
txtEnd.SetFocus
Exit Sub
End If
' Get the times entered...
Dim dtStart As Date
dtStart = TimeValue(txtStartHrs.Text & ":" & txtStartMins.Text & cboAP1)
Dim dtEnd As Date
dtEnd = TimeValue(txtStopHrs.Text & ":" & txtStopMins.Text & cboAP2)
' Make sure these are valid times...
If Not IsDate(dtStart) Then
MsgBox "The start time entered was not valid."
txtStartHrs.SetFocus
Exit Sub
End If
If Not IsDate(dtEnd) Then
MsgBox "The end time entered was not valid."
txtStopHrs.SetFocus
Exit Sub
End If
' Calculate the difference...
Dim iHours As Integer, iMins As Integer
iMins = DateDiff("n", dtStart, dtEnd)
iHours = iMins / 60
iMins = iMins Mod 60
' Display the result...
lblDurationHrs.Caption = iHours & " : " & iMins
End Sub
also here is a screenie of the logic error:
Anaconda
06-07-2004, 04:16 PM
Here's a hint.. try the times 8:00 AM, and 8:29 AM, I'm willing to bet that you'll get a correct answer.
[do this before reading on]
after that try replacing
iHours = iMins / 60
with
iHours = iMins \ 60
[again, do this before reading on]
[the explanation]
This change will force the division to be integer division. Currently, you are performing normal division (decimal, not integer), and then casting it to an integer, which will be subject to rounding. (thus any spans that have remaining minutes below 30 min, should calculate correctly, while any spans with the remaining minutes over 30 will be off by one, as the hours were rounded up by one in the conversion after the division.
Zero Tolerance
06-07-2004, 04:19 PM
Snake.....damn your good!
I would of never figured that one out....I dont think I have ever read anywhere about that operand
thanks....works flawlessly now!
sssssssssBANG!~
Sorry, I didn't get a chance to check the forum yesterday, but it looks like Snake took care of ya. I actually thought I had chosen the correct slash for integer division (which was my intention), but it looks like I didn't! Sorry bout that -- whipped the program up in a minute or two.
Zero Tolerance
06-08-2004, 01:13 PM
Bond.....much appreciated!
All is good and it works fine for what I need it to do.
Now is there any way to create a "Read Only" document in word???
LOL
Just set the file attributes.
Or, when you do a File->Save As, you can use the Tools menu in the upper-right corner and select "Security Options." Within here, you can setup passwords to open and modify your Word file. If you just setup a password to modify, they can still open the file, but they won't be able to save (under the same name) unless they supply the proper password.
(This is also under Tools->Options->Security tab)
Zero Tolerance
06-08-2004, 03:04 PM
I should of been more clear....
I was wanting to know if there was a way to make my VB program, when creating the word doc, could make it "read-only"
Sorry for the confusion....cuz I do know how to make a doc when creating it in word myself...to be "read-only"
This is VBScript, but should be similar:
dim w
set w = createobject("word.application")
w.visible = true
dim doc
set doc = w.documents.add
' Here's the key line. You could also use ActiveDocument.WritePassword
doc.WritePassword = "mypassword"
set doc = nothing
set w = nothing
You will be able to save it the first time. Then, if you try to open it again, it will prompt you to enter a password for modify access, or click "Read Only" if you don't have the password.
Zero Tolerance
06-09-2004, 09:47 AM
thanks Bond....I have met with my supervisor (she is clueless about any programming...and im NOT a programmer here...nor anywhere really for that matter yet...
but since all of the new HIPPA regulations.....maybe I should not even let anyone save the file as any type....
is there a way to let it just print the word doc thru word....but without opening word? I find that its easier to format the form with word and the objWord commands than Printer.Print
so I guess my basic question is.......can it just use word to print without actually opening word?
if not...then I will go with the above code
Thanks,
sssssssssssBANG!
Yes, you can create an instance of Word (like in the example I posted above) and just don't set the Visible property to true. It will still launch Word and you can still do your formatting and all -- it just won't be visible onscreen.
Then, you can call the print function to print the document, then close the document and close Word without anyone even knowing that it was running.
Zero Tolerance
06-09-2004, 03:27 PM
so without me knowing what the h@#k I'm doing
would a replacement of this:
dim w
set w = createobject("word.application")
w.visible = true
dim doc
set doc = w.documents.add
with this:
dim w
set w = createobject("word.application")
w.visible = false
dim doc
set doc = w.documents.print
set doc = w.documents.close
will that work?
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.