Solved

VB6 - Convert HH:MM into Decimal

Posted on 2016-11-18
8
76 Views
Last Modified: 2016-11-19
Hi,

I need your help to convert a time in a textbox1 to be converted into Decimal value.

Normally, in Excel, if i have 00:10 (10 minutes) the decimal value would be 0.17.
The excel formula would be:
=INT(A1)*24+ROUND(MINUTE(A1)/60,2)

Open in new window


But now, i would like to do the same thing but in VB6 and the time would be entered in the TextBox1.

I would like to get the Decimal result in Textbox2.

How can i do that?

Thanks for your help
0
Comment
Question by:Wilder1626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41893438
Will the TextBox1 value always be hours and minutes?
0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 400 total points
ID: 41893444
Change the textbox names to suit.

Dim intMin As Integer
Dim strParts() As String
strParts = Split(Text1.Text, ":")
intMin = strParts(1) + 60 * strParts(0)
Text2.Text = Round(intMin / 60, 2)

Open in new window

0
 
LVL 4

Assisted Solution

by:Chris Watson
Chris Watson earned 100 total points
ID: 41893447
Dim hours As Long
hours = Left(Text1.Text, 2)
    
Dim hoursAsDays As Long
hoursAsDays = hours * 24
   
Dim minutes As Byte
minutes = Right(Text1.Text, 2)
    
Dim minutesAsDays As Currency
minutesAsDays = minutes / 60
minutesAsDays = Round(minutesAsDays, 2)
    
Dim days As Currency
days = hoursAsDays + minutesAsDays

Text2.Text = days

Open in new window

0
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!

 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 41893457
Thank you so much. This is exactly what i was looking for.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41893464
I don't know how the value gets into Textbox1 but if it's user entered then you might want to add lines 3 to 6.
Dim intMin As Integer
Dim strParts() As String
If Len(Text1.Text) - 1 <> Len(Replace(Text1.Text, ":", "")) Then
    MsgBox "A string in the form hh:mm or h:mm is expected, please try again"
    Exit Sub
End If

strParts = Split(Text1.Text, ":")
intMin = strParts(1) + 60 * strParts(0)
Text2.Text = Round(intMin / 60, 2)

Open in new window

In any case you're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
1
 
LVL 11

Author Comment

by:Wilder1626
ID: 41893808
Thanks Martin, I will definitely add lines 3 to 6. Good idea as the vale is added manually.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41893819
Since a user can put anything in a textbox you probably should use a MaskedEdit control instead as in the attached.
MaskedEdit.zip
1
 
LVL 11

Author Comment

by:Wilder1626
ID: 41894164
wow, i've to go with your MaskedEdit control. That a very good idea. Thanks again
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

759 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