Solved

extract times in ms access

Posted on 2014-12-16
11
139 Views
Last Modified: 2014-12-17
I have textboxes that has times entered in this format 18 Hours 10 Minutes
i would like to extract the hours and minutes then have the sum of text boes displayed in another box.
thanks
0
Comment
Question by:Svgmassive
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40503085
Please provide a few more examples of the time format.
Could it also be 234 hours 5 minutes (>24 hours, hours or minutes without the leading zero)?
could it also be 0 hours 0 minutes?
could it also be 15 hours (whole number of hours, so no minutes specified)
Is there any validation on the text box or could the values also include typos?
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 40503099
try this sample db

click Process button

you can also change the values the is on display on the three  top textboxes then click process button

result is displayed on the 4th textbox
Database1--Q-28581983-.accdb
0
 
LVL 18

Expert Comment

by:Simon
ID: 40503119
Nice one Rey! I didn't know val() could be used like that (to get a numeric value from a string that starts with a number).
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 35

Expert Comment

by:PatHartman
ID: 40503294
Val() only pulls out the first numeric string so it won't help you in this case since you have two embedded strings.  Of course, you could use two fields that are defined as numeric rather than a mushed text string and not have this problem at all.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40503314
<Val() only pulls out the first numeric string so it won't help you in this case since you have two embedded strings. >

it will help, if you know how to use them.
0
 
LVL 18

Expert Comment

by:Simon
ID: 40503647
Yes, I tried Rey's solution and it works - otherwise I wouldn't have congratulated him. Good use of both Val() and Split().

The only fail I got was if I entered 1 hour 5 minutes (without the 's' on the hours). I agree with Pat that a better way to enter the data would be as separate numeric fields for hours and minutes, or a single hh:mm format datetime field with validation, but given the question as stated I think Rey's solution is ace.
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 40503719
As long as you're happy.  I would have simply split the field and not used Val at all.  I've been burned by it on more than one occasion.  This one seems safe enough although you should be aware that certain very strange expressesions are considered to be numeric by Val() such as

print val("1e3.2")
 1000

Looks like val is interpreting this as scientific notation.  I discovered the issue in an application that managed drawings for a building company and then again in an application that worked with part numbers.
0
 
LVL 18

Expert Comment

by:Simon
ID: 40503730
Thanks for the salutory warning on Val(). I don't think I've ever used it myself, but I was interested to see it in Rey's code.
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 250 total points
ID: 40504243
> The only fail I got was if I entered 1 hour 5 minutes (without the 's' on the hours).
And so will 1 minute fail.

You'll have to validate the content of the textboxes. Then it is a bit simpler to convert to time values and sum these. Finally, apply a generice function to format the sum like the one below; you can, of course, modify it to return any format.
Private Sub Command8_Click()

    Me.Text4 = FormatHourMinute(fComputeTime())

End Sub

Private Sub Form_Load()

Me.Text1 = "18 hours 10 minutes"
Me.Text2 = "1 hour 10 minutes"
Me.Text3 = "18 hours 1 minute"

End Sub

Private Function fComputeTime() As Date

    Dim ctl     As Control
    Dim datTime As Date

    For Each ctl In Me.Controls
        If ctl.ControlType = acTextBox Then
            If InStr(ctl.Value, "hour") > 0 And InStr(ctl.Value, "minute") > 0 Then
                datTime = datTime + TimeSerial(CLng(Split(ctl.Value, " ")(0)), CLng(Split(ctl.Value, " ")(2)), 0)
            End If
        End If
    Next

    fComputeTime = datTime

End Function

Public Function FormatHourMinute( _
  ByVal datTime As Date, _
  Optional ByVal strSeparator As String = ":") _
  As String
  
' Returns count of days, hours and minutes of datTime
' converted to hours and minutes as a formatted string
' with an optional choice of time separator.
'
' Example:
'   datTime: #10:03# + #20:01#
'   returns: 30:04
'
' 2005-02-05. Cactus Data ApS, CPH.

  Dim strHour       As String
  Dim strMinute     As String
  Dim strHourMinute As String
  
  strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
  ' Add leading zero to minute count when needed.
  strMinute = Right("0" & CStr(Minute(datTime)), 2)
  strHourMinute = strHour & strSeparator & strMinute
  
  FormatHourMinute = strHourMinute
  
End Function

Open in new window

/gustav
0
 

Author Comment

by:Svgmassive
ID: 40504480
gustav you are correct thanks
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40504496
You are welcome!

/gustav
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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…

813 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

13 Experts available now in Live!

Get 1:1 Help Now