Solved

SQL to Code: How to write it.

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
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…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

758 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

21 Experts available now in Live!

Get 1:1 Help Now