assignin time values to a variable

R

rocco

Hello,
I have to assign time values to few variables.
How should I do?
Let's say I have this variable:

dim myTime as Date

would it be:
(a) mytime=#08:30 Am#
or
(b) mytime=(8/24) + (30/1440)

Since the values will be retrieved from buttons the user will click, I could
I assign values through rule (a):
Should I write:
mytime="#" & buttonclicked & "#"
wouldn't this make a string and then raise an error when assigned to a
variable declared as Date?
Boh...

Obviously I will experiment by myself, but I really prefer some advice.
Thanks all,
Rocco
 
J

John W. Vinson

Hello,
I have to assign time values to few variables.
How should I do?
Let's say I have this variable:

dim myTime as Date

would it be:
(a) mytime=#08:30 Am#
or
(b) mytime=(8/24) + (30/1440)

Since the values will be retrieved from buttons the user will click, I could
I assign values through rule (a):
Should I write:
mytime="#" & buttonclicked & "#"
wouldn't this make a string and then raise an error when assigned to a
variable declared as Date?
Boh...

Obviously I will experiment by myself, but I really prefer some advice.
Thanks all,
Rocco

Either will work, but (b) is an awfully convoluted way to get there! Just use
the # delimited date literal.

Access will do reasonable string conversions for you, so don't worry about it.
HOWEVER... what's with the buttons? Might not a Listbox or an Option Group
meet the need?
 
M

Marshall Barton

rocco said:
I have to assign time values to few variables.
How should I do?
Let's say I have this variable:

dim myTime as Date

would it be:
(a) mytime=#08:30 Am#

That's ok, but be sure that you do not need a date part.
or
(b) mytime=(8/24) + (30/1440)

Even if that works, it's too obscure to be used. The
literal in your previous example is much clearer. If you
are calculating the hous and/or minutes, the TimeSerial
function is the way to go.
Since the values will be retrieved from buttons the user will click, I could
I assign values through rule (a):
Should I write:
mytime="#" & buttonclicked & "#"
wouldn't this make a string and then raise an error when assigned to a
variable declared as Date?

Yes, that will make a string. Use TimeValue (or CDate) if
all you have is a string that can be converted to a time
value.
 
R

rocco

Hi John and Marshall,
thank you both for your good advices.

Me too would prefer the solution [mytime=#08:30#], but since I will get the
time values from action on a form (John, it is a really good point to use an
option group for this, thanks), it would mean having to go with
[mytime=CDate("#" & value & "#")], as Marshall suggested, which may well look
clearer, but involve using a function to get the job done. And CDate is
really sensible to the user regional settings.
Plus it sounds awkward to me having to build a string and then convert it
into a date/time values.
I will give a shoot at solution (b). It use a little math but looks like I'm
assigning the value in one row only, instead on relying on another function
which rely on user pc general settings.

But I may well be wrong. I'm 99% of time, so...

Thank you both!
roccop
 
M

Marshall Barton

rocco said:
Me too would prefer the solution [mytime=#08:30#], but since I will get the
time values from action on a form (John, it is a really good point to use an
option group for this, thanks), it would mean having to go with
[mytime=CDate("#" & value & "#")], as Marshall suggested, which may well look
clearer, but involve using a function to get the job done. And CDate is
really sensible to the user regional settings.
Plus it sounds awkward to me having to build a string and then convert it
into a date/time values.
I will give a shoot at solution (b). It use a little math but looks like I'm
assigning the value in one row only, instead on relying on another function
which rely on user pc general settings.


Where is this "value" coming from and how did it get set?

If it is in a date/time variable or argument, then just use
it instead of the mytime variable.
 
R

rocco

Hi Marshall,
"value" comes from user choice. The user will click option buttons in an
option group. The value of the option group will set "value".

rocco

Marshall Barton said:
rocco said:
Me too would prefer the solution [mytime=#08:30#], but since I will get the
time values from action on a form (John, it is a really good point to use an
option group for this, thanks), it would mean having to go with
[mytime=CDate("#" & value & "#")], as Marshall suggested, which may well look
clearer, but involve using a function to get the job done. And CDate is
really sensible to the user regional settings.
Plus it sounds awkward to me having to build a string and then convert it
into a date/time values.
I will give a shoot at solution (b). It use a little math but looks like I'm
assigning the value in one row only, instead on relying on another function
which rely on user pc general settings.


Where is this "value" coming from and how did it get set?

If it is in a date/time variable or argument, then just use
it instead of the mytime variable.
 
M

Marshall Barton

So use the option group's AfterUpdate event and check the
group's OptionValue property:

Select Case Me.theoptiongroup
Case 1
mytime=#08:30#
Case 2
mytime=#12:00#
Case 3
. . .
End Select

No need to mess with string conversion.

Note: If those are times of day, then you may need to
include the date part. If they are durations, then you
should use an integer number of minutes(?), not a time
value.
--
Marsh
MVP [MS Access]

"value" comes from user choice. The user will click option buttons in an
option group. The value of the option group will set "value".

Marshall Barton said:
rocco said:
Me too would prefer the solution [mytime=#08:30#], but since I will get the
time values from action on a form (John, it is a really good point to use an
option group for this, thanks), it would mean having to go with
[mytime=CDate("#" & value & "#")], as Marshall suggested, which may well look
clearer, but involve using a function to get the job done. And CDate is
really sensible to the user regional settings.
Plus it sounds awkward to me having to build a string and then convert it
into a date/time values.
I will give a shoot at solution (b). It use a little math but looks like I'm
assigning the value in one row only, instead on relying on another function
which rely on user pc general settings.


Where is this "value" coming from and how did it get set?

If it is in a date/time variable or argument, then just use
it instead of the mytime variable.
 
C

ChrisO

Just as an alternative: -

If we go for the option group method we can store the number of seconds in
each option and that value is a Long. The Date/Time field/data type is not a
Date/Time at all; it’s a Double which represents a number of days. So .5 is
half a day and 1.5 is one and a half days.

So, for example, 08:30 AM can be stored as 30600 and 6:00:00 PM can be
stored as 64800 in the appropriate options. If we then divide the option
value by 86400, the number of seconds in a day, we get the correct value
despite Access’ exuberance to try and convert things using regional settings.
The data goes into the Field/Variable correctly but it is still displayed
correctly as per regional settings.

If we go that way the code breaks down to: -

Private Sub grpSelectTime_AfterUpdate()
Dim myTime As Date

myTime = Me.grpSelectTime / 86400

End Sub

--
A nod is as good as a wink to a blind horse.


rocco said:
Hi Marshall,
"value" comes from user choice. The user will click option buttons in an
option group. The value of the option group will set "value".

rocco

Marshall Barton said:
rocco said:
Me too would prefer the solution [mytime=#08:30#], but since I will get the
time values from action on a form (John, it is a really good point to use an
option group for this, thanks), it would mean having to go with
[mytime=CDate("#" & value & "#")], as Marshall suggested, which may well look
clearer, but involve using a function to get the job done. And CDate is
really sensible to the user regional settings.
Plus it sounds awkward to me having to build a string and then convert it
into a date/time values.
I will give a shoot at solution (b). It use a little math but looks like I'm
assigning the value in one row only, instead on relying on another function
which rely on user pc general settings.


Where is this "value" coming from and how did it get set?

If it is in a date/time variable or argument, then just use
it instead of the mytime variable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top