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
Solved

SQL to Code: How to write it.

Posted on 2006-06-12
4
228 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

839 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