Adding Time

  • Thread starter Thread starter David Ehrenreich
  • Start date Start date
D

David Ehrenreich

Hello,

In my form I have a feild that I would like to calculate
the time. So, say it's currently 9am if a person enters 2
in the feild it will log 11am in the table, or if they
enter 8.5 it will log 5:30pm in the feild. I know I will
need an expression to do this, but i have very little
experience with Vb. If anyone could help me with this.

Thank you
David Ehrenreich
 
David Ehrenreich said:
Hello,

In my form I have a feild that I would like to calculate
the time. So, say it's currently 9am if a person enters 2
in the feild it will log 11am in the table, or if they
enter 8.5 it will log 5:30pm in the feild. I know I will
need an expression to do this, but i have very little
experience with Vb. If anyone could help me with this.

Thank you
David Ehrenreich

In VBA:

Dim TheUsersNumber As Double, TheDate As Date, TheLogDate As Date

TheUsersNumber = Me.TextField1
TheDate = Now
TheLogDate = TheDate + (TheUsersNumber / 24)
 
In VBA, you can use the following code:

Dim dteEAT As Date 'Estimated Arrival Time
Dim strEAT As String 'String format of Short Time
dteEAT = Now + CDbl(<ValueUserEnters>)/24

Then you can use the Format Function on this Date variable

strEAT = Format(dteEAT,"Short Time")

It would be more efficient for Access to store the value in the dteEAT
variable in the table behind the form, but the strEAT is in string format to
be displayed on the form.
 
David Ehrenreich said:
Hello,

In my form I have a feild that I would like to calculate
the time. So, say it's currently 9am if a person enters 2
in the feild it will log 11am in the table, or if they
enter 8.5 it will log 5:30pm in the feild. I know I will
need an expression to do this, but i have very little
experience with Vb. If anyone could help me with this.

Thank you
David Ehrenreich

Date/time data is stored internally as a count and fraction of days
relative to a specified "zero" date (midnight, December 30, 1899). The
time is represented as a fraction of a day, so 12:00 PM (noon) is
internally represented as 12/24 = 0.5.

Therefore, to increment a date/time field by a number of hours, you
would divided that number by 24 and add the result to the field; e.g.,
to add 8.5 hours to the date/time field "MyDateField" on your form, you
could use this code:

Me.MyDateField = Me.MyDateField + (8.5 / 24)

If the increment is coming from a control named HoursToAdd, you'd have

Me.MyDateField = Me.MyDateField + (Me.HoursToAdd / 24)
 
Back
Top