VB Code give unexplained answers.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I have attached below some VB code i am using on the on click property of a
button on one of my database forms.

When i run the code and compare it against the log in and log out times it
gives me the wrong answers. For example if the log in time was before 11:00am
and the log out was before 17:00 the field tmpadjusment should be 10 but the
code keeps skipping straight through to the else.

Dim tmpLog_In_Time, tmpLog_Out_Time As Variant
Dim tmpAdjustment As Integer
Dim tmpLogging_Out As Boolean

tmpLog_In_Time =
Forms!frmFactory_Menu!fsubFactory_Menu!txtLast_Live_Log_In
tmpLog_Out_Time = Now()

tmpLogging_Out = Forms!frmFactory_Menu!fsubFactory_Menu!txtLog_In_Status

DoCmd.Echo False
DoCmd.SetWarnings False

' If Forms!frmFactory_Menu!fsubFactory_Menu!txtLog_In_Status Then
If tmpLogging_Out Then

tmpAdjustment = 0

' The person is now logging out

If tmpLog_In_Time <= #11:00:00 AM# Then

If tmpLog_Out_Time > #5:00:00 PM# Then

tmpAdjustment = 20

Else

tmpAdjustment = 10

End If

Else

If tmpLog_Out_Time > #5:00:00 PM# Then

tmpAdjustment = 10

Else

tmpAdjustment = 0

End If
End If

I would appreciate any help.

Thanks
 
Here you are assigning tmpLog_In_Time a text value, not a time.
tmpLog_In_Time = _
Forms!frmFactory_Menu!fsubFactory_Menu!txtLast_Live_Log_In

You have defined all your variables where you are manipulating time as
Variant. They should be Date.
You are not getting errors because Variant data types will accept anything.
Your comparisons are not working because you are not always comparing dates.
 
Now() contains both date and time information. I could be wrong but I believe
you need to use the Format() function to get the compare to work properly.
ie:
If Format(tmpLog_Out_Time,'Short Time') > #5:00:00 PM# Then

Hi

I have attached below some VB code i am using on the on click property of a
button on one of my database forms.

When i run the code and compare it against the log in and log out times it
gives me the wrong answers. For example if the log in time was before 11:00am
and the log out was before 17:00 the field tmpadjusment should be 10 but the
code keeps skipping straight through to the else.

Dim tmpLog_In_Time, tmpLog_Out_Time As Variant
Dim tmpAdjustment As Integer
Dim tmpLogging_Out As Boolean

tmpLog_In_Time =
Forms!frmFactory_Menu!fsubFactory_Menu!txtLast_Live_Log_In
tmpLog_Out_Time = Now()

tmpLogging_Out = Forms!frmFactory_Menu!fsubFactory_Menu!txtLog_In_Status

DoCmd.Echo False
DoCmd.SetWarnings False

' If Forms!frmFactory_Menu!fsubFactory_Menu!txtLog_In_Status Then
If tmpLogging_Out Then

tmpAdjustment = 0

' The person is now logging out

If tmpLog_In_Time <= #11:00:00 AM# Then

If tmpLog_Out_Time > #5:00:00 PM# Then

tmpAdjustment = 20

Else

tmpAdjustment = 10

End If

Else

If tmpLog_Out_Time > #5:00:00 PM# Then

tmpAdjustment = 10

Else

tmpAdjustment = 0

End If
End If

I would appreciate any help.

Thanks

_______________________________________________
hth - RuralGuy (RG for short)
Please post to the NewsGroup so all may benefit.
 
Hi

Thanks very much for you help. We changed the data type to data but still
this code doesn't work.

I have run the code in debug mode and looked at each value held by the
current line of code. If the log in time equals 08/09/05 10:30:00 the first
test which is "Is this less than or equal to 11:00:00AM" fails and skips
straight to the else which is wrong.

Then at the next test comparing the log out which is now(), the data shows
as 15:00:00 which should clearly prove the stsament false and set the
adjustment to 0 but i always sets the adjustment to 10.

Can't see at all what is causing this.
 
Hi

Thanks very much for the help.

I tried the format thing but that don't appear to work.

I have devised a really ugly work around creating a field called tmpHour_in
and tmpHour_out since i am really only interested in the hour part of the log
in time.

I would still liike to find out why the original code doesn't work as it is
a much more elegant solution, albeit slightly wrong!
 
If the log in time equals 08/09/05 10:30:00 the first
test which is "Is this less than or equal to 11:00:00AM" fails and skips
straight to the else which is wrong.


Nope, that's actually correct. It just isn't what you want. :-)

Date values include Date (whole number) and Time (decimal) information. Your
current comparison can be restated as:
#08/09/05 10:30:00 AM# < #12/31/1899 11:00 AM#
which is false. (0 = 12/31/1899, 1 = 1/1/1900, etc.)

The TimeValue function returns just the time portion of an expression (well,
sort of, it reduces the date portion to zero).
The following returns True:
TimeValue(#08/09/05 10:30:00 AM#) < #12/31/1899 11:00 AM#

HTH,
 
I would still liike to find out why the original code doesn't work as it
is
a much more elegant solution, albeit slightly wrong!

Well, first, change those variants back to date defs.

And, several people pointed out:

If tmpLog_In_Time <= #11:00:00 AM# Then

You are comparing a date + time with a time only value...how can you expect
that to work?

You need
If timevalue( tmpLog_In_Time) <= #11:00:00 AM# Then
 
Back
Top