Solved

SQL to Code: How to write it.

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

627 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