Solved

Do I need to use DateDiff to change times?

Posted on 2009-05-13
11
185 Views
Last Modified: 2012-05-06
I've got a webcontrol that is supposed to enter in a start time and and end time based on information loaded into the MSFlexGrid. The only problem is, I cannot seem to get the end time to give the correct time base on a few factors:

'If the start hour = 11AM, then end hour = 2PM
'If the start hour = 11PM, then end hour = 2AM of next day
'If the start hour = 9AM, then end hour = 12PM
Private Sub MSFlexGrid_EnterCell()

On Error Resume Next
 
 

Dim strMonth As String

Dim strDay As String

Dim strYear As String

Dim strHour As String

Dim strMin As String

Dim strAMPM As String
 

'START TIME

'month

strMonth = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 0)

strMonth = Split(strMonth, "/")(0)

strMonth = Replace(strMonth, "0", "")

wb.Document.All("start_time_month").Value = strMonth
 

'day

strDay = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 0)

strDay = Split(strDay, "/")(1)

If Left(strDay, 1) = 0 Then strDay = Replace(strDay, "0", "")

wb.Document.All("start_time_day").Value = strDay
 

'year

strYear = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 0)

strYear = Split(strYear, "/")(2)

wb.Document.All("start_time_year").Value = strYear
 

'hour

strHour = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 1)

strHour = Split(strHour, ":")(0)

wb.Document.All("start_time_hour").Value = strHour
 

'min

strMin = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 1)

strMin = Split(strMin, ":")(1)

strMin = Left(strMin, 2)

wb.Document.All("start_time_min").Value = strMin
 

'AM_PM

strAMPM = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 1)

strAMPM = Right(strAMPM, 2)

wb.Document.All("start_time_ampm").Value = LCase(strAMPM)

'END START TIME
 

'END TIME

'month

strMonth = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 0)

strMonth = Split(strMonth, "/")(0)

strMonth = Replace(strMonth, "0", "")

wb.Document.All("end_time_month").Value = strMonth
 

'day

strDay = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 0)

strDay = Split(strDay, "/")(1)

If Left(strDay, 1) = 0 Then strDay = Replace(strDay, "0", "")

wb.Document.All("end_time_day").Value = strDay
 

'year

strYear = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 0)

strYear = Split(strYear, "/")(2)

wb.Document.All("end_time_year").Value = strYear
 

'hour

strHour = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 1)

strHour = Split(strHour, ":")(0)

strHour = strHour + 3

If strHour > 12 Then strHour = 12

'wb.Document.All("end_time_hour").Value = strHour
 

'min

strMin = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 1)

strMin = Split(strMin, ":")(1)

strMin = Left(strMin, 2)

wb.Document.All("end_time_min").Value = strMin
 

'AM_PM

strAMPM = MSFlexGrid.TextMatrix(MSFlexGrid.Row, 1)

strAMPM = Right(strAMPM, 2)

wb.Document.All("end_time_ampm").Value = LCase(strAMPM)

'END END TIME
 
 
 
 
 

End Sub

Open in new window

0
Comment
Question by:Taylor814
  • 7
  • 3
11 Comments
 

Author Comment

by:Taylor814
ID: 24378793
Here's a picture of the MSFlexGrid
Clipboard01.jpg
0
 
LVL 16

Accepted Solution

by:
JohnBPrice earned 500 total points
ID: 24379004
You just want to add 3 hours to the date time, running into next day?  Yes, DateAdd would be far easier.


dim dtStart as date

dim dtEnd as date

dtStart = CDate(MSFlexGrid.TextMatrix(MSFlexGrid.Row, 0) & " " & MSFlexGrid.TextMatrix(MSFlexGrid.Row, 1)

dtEnd = DateAdd("h",3,dtStart)

Open in new window

0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 24379019
missing a paren in there

dtStart = CDate(MSFlexGrid.TextMatrix(MSFlexGrid.Row, 0) & " " & MSFlexGrid.TextMatrix(MSFlexGrid.Row, 1))

Open in new window

0
 

Author Closing Comment

by:Taylor814
ID: 31581188
Thank you so much. It was exactly what I was looking for. Didn't realize it was THAT simple. Thank you again!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24379082
You need to work with date variables, e.g.

Dim iMonth as Integer
iMonth = Month(MSFlexGrid.TextMatrix(MSFlexGrid.Row, 1))

Dim iHour as Integer
iHour = Hour(MSFlexGrid.TextMatrix(MSFlexGrid.Row, 2))

Also arithmetic is easier if the 24-hour clock is used,

so if iHour = 23 means, that means 11 o'clock in the evening.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Taylor814
ID: 24379098
Quick question though, if the time starts at 9PM, why doesn't dtEND show the next day with "12:00AM". It seems to leave that part off.

If it starts at 9:30PM, then dtEND is nextday+ 12:30AM
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 24379289
If the date is EXACTLY 12:00:00 AM, the default date display format is to leave off the time and just show the date.  You can force it with the format function, e.g.

dim dtEndDate as date

dim strEndDate as string

strEndDate = format(dtEndDate,"mm/dd/yyyy Hh:Nn AM/PM")

Open in new window

0
 

Author Comment

by:Taylor814
ID: 24381587
Hmm, when using that code, I got:

?dtstart
5/9/2009 9:00:00 PM
?dtend
5/10/2009
?strenddate
12/30/1899 12:00 AM
0
 

Author Comment

by:Taylor814
ID: 24381593
Ah, nevermind. Changed dtEndDate to dtEnd...works...thanks
0
 

Author Comment

by:Taylor814
ID: 24401378
Here's what I don't understand. In the immediate window I get:

?Format(dtEnd, "mm/dd/yyyy hh:nn AM/PM")
05/09/2009 12:00 AM
?strenddate
5/9/2009

strEndDate doesn't show 12:00AM even though the format says it should.
0
 

Author Comment

by:Taylor814
ID: 24401384
Nevermind. strEndDate was Dim Date instead of string.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Access 2016 VB code 9 86
Vb.net 2008 2 51
Updates not working for MS Windows 7 12 126
VB6 Compile Compatibility Issue 4 38
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now