variables do not accept values

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

Guest

I recently starting creating MS Acess projects and have run in to a problem.
I create the project and then import my forms and code from an Access .mdb.
When I debug my code, I noticed that my variable does not accept the result
of an addition of fields. I am adding the values of fields on my form and
using a variable to store the value for future use in the code. After I run
the line of code, the variable is equal to "null". The fields have the
correct value, the added value is not being accepted by the variable. What
am I missing? Is it because of importing from a .mdb or something else?
 
The code is simple

dim tothours

tothours = Mon + Tue + Wed + Thur + Fri

The days of the week are the data entry fields. When I run the code, the
tothours is null.
 
Null propagates. Use the Nz function to replace null values with zero:

tothours = Nz(Mon, 0) + NZ(Tue, 0) + Nz(Wed, 0) + Nz(Thur, 0) + Nz(Fri, 0)

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Bob said:
The code is simple

dim tothours

tothours = Mon + Tue + Wed + Thur + Fri

The days of the week are the data entry fields. When I run the code, the
tothours is null.
else?
 
It works fine. Thanks for the tip.

Douglas J Steele said:
Null propagates. Use the Nz function to replace null values with zero:

tothours = Nz(Mon, 0) + NZ(Tue, 0) + Nz(Wed, 0) + Nz(Thur, 0) + Nz(Fri, 0)

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



else?
 
seems simple enough. I am guessing tothours, mon, tue, wed, thur, fri are
controls on a form. Could be that tothours is not, but the basic problem
here is at least one of these variables have not been initialized.
Let's say someone put 8 in mon, 8 in tue, 8 in wed, and 8 in fri. That
means that thru will result in a Null vaule. If you have a Null value in a
calculation, you get back Null. Also, if these are controls on a form, then
the result would be 8888, not what I expect you want. So, we need to do two
things. convert them to numbers and convert nulls to zeros. Here is how we
do that:
tothrs = CLng(Nz(mon, 0)) + CLng(Nz(tue, 0)) + CLng(Nz(wed, 0)) +
CLng(Nz(thur, 0)) + CLng(Nz(fri, 0))
 
Back
Top