Solved

SQL to Code: How to write it.

Posted on 2006-06-12
4
229 Views
Last Modified: 2012-05-05
I have a form in which are input two dates which I would like to display in a text box by using the AfterUpdate Event of the second date field. The SQL for the query that subtracts the two dates is:

SELECT CInt(([EndDateTime]-[StartDateTime])*1440) Mod 60 AS Minutes, CInt(([EndDateTime]-[StartDateTime])*24) Mod 24 AS Hours, Format([Hours],"0") &  " hours, " & Format([Minutes],"0") & " minutes" AS Elapsed
FROM tblTrades;

How would I write that in code format?

Thanks.
RWW
0
Comment
Question by:PstWood
[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
  • 3
4 Comments
 
LVL 58

Expert Comment

by:harfang
ID: 16890682
Hello PstWood

A translation to VB could look like this:


Private Sub txtEndDateTime_Click()

    Dim lngMinutes As Long
    Dim lngHours As Long

    lngMinutes = CInt((txtEndDateTime - txtStartDateTime) * 1440) Mod 60
    lngHours = CInt((txtEndDateTime - StartDateTime) * 24) Mod 24
    txtElapsed = Format(lngHours, "0") & " hours, " & Format(lngMinutes, "0") & " minutes"

End Sub


You can also use the exact same syntax you had in the query for textboxes on your form. For example, start with a textbox txtMinutes, and use:

    = CInt(([EndDateTime]-[StartDateTime])*1440) Mod 60

And continue with txtHours and txtElapsed. Then hide those you don't want (or, of course, incorporate the definitions of Minutes and Hours into the last expression, making one very long but valid expression...


(°v°)
0
 
LVL 58

Accepted Solution

by:
harfang earned 125 total points
ID: 16890701
I just noticed the Mod 24. If you do want a wrap at 24 hours, you can use simply this:

    = Format(EndDateTime-StartDateTime, 'h" hours, "n" minutes"')

Or, in VB:

    txtElapsed = Format(EndDateTime - StartDateTime, "h"" hours, ""n"" minutes""")

I was under the impression you needed the function to *allow* results over 24 hours. Incidentally, I think I would do it like this, though:

Private Sub txtEndDateTime_Click()

    Dim lngMinutes As Long

    lngMinutes = CLng((txtEndDateTime - txtStartDateTime) * 1440)
    txtElapsed = (lngMinutes \ 60) & " hours, " & (lngMinutes Mod 60) & " minutes"

End Sub

Anyway, good luck!
(°v°)
0
 

Author Comment

by:PstWood
ID: 16894433
Yep. That worked. I made the following changes to include Me. for the text boxes being used.

Private Sub EndDateTime_AfterUpdate()

    Dim lngMinutes As Long

    lngMinutes = CLng((Me.EndDateTime - Me.StartDateTime) * 1440)
    Me.Duration = (lngMinutes \ 60) & " hours, " & (lngMinutes Mod 60) & " minutes"

End Sub

Thanks again.
RWW
0
 
LVL 58

Expert Comment

by:harfang
ID: 16894510
Glad it worked, and success with your application!
(°v°)
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

735 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