PC Review


Reply
Thread Tools Rate Thread

assignin time values to a variable

 
 
rocco
Guest
Posts: n/a
 
      19th Mar 2010
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
 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      19th Mar 2010
On Fri, 19 Mar 2010 07:30:01 -0700, rocco <(E-Mail Removed)>
wrote:

>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?
--

John W. Vinson [MVP]
 
Reply With Quote
 
rocco
Guest
Posts: n/a
 
      19th Mar 2010
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


"Marshall Barton" wrote:

> rocco wrote:
> >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.
>
> --
> Marsh
> MVP [MS Access]
> .
>

 
Reply With Quote
 
rocco
Guest
Posts: n/a
 
      20th Mar 2010
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" wrote:

> rocco wrote:
> >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.
>
> --
> Marsh
> MVP [MS Access]
> .
>

 
Reply With Quote
 
ChrisO
Guest
Posts: n/a
 
      25th Mar 2010
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" wrote:

> 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" wrote:
>
> > rocco wrote:
> > >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.
> >
> > --
> > Marsh
> > MVP [MS Access]
> > .
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assignin Taks to a Distro in outlook cyb3rwolf Microsoft Outlook Discussion 0 24th Sep 2008 10:59 PM
Assignin TASKs to associates within the office cannot be viewed =?Utf-8?B?ZmNyb2Vz?= Microsoft Outlook Discussion 1 15th May 2007 03:22 PM
Run-time error '91': Object variable or With block variable not se =?Utf-8?B?Q2hyaXNzeQ==?= Microsoft Excel Crashes 0 15th Jun 2005 12:58 AM
run time error 91 Object Variable or With Block Variable not set =?Utf-8?B?Y2hhbXBjZg==?= Microsoft Word Document Management 3 6th Jan 2005 01:17 PM
Run-time error '91': "Object variable or With block variable not set Mike Microsoft Excel Programming 2 30th Dec 2004 10:59 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:57 PM.