Calling a Function in a Query and Form

J

JudyB

I am at the end of my rope and need help desperately. I have a form and a
query where I would like to use a public function to calculate weeks. This
is my function:

Public Function CalcWeeks(DateStart As Variant, DateEnd As Variant) As Variant
Dim int_Weeks As Integer

If IsNull(DateStart) Then
CalcWeeks = Null
Exit Function
End If

int_Weeks = DateDiff("w", DateStart, IIf(IsNull(DateEnd), Now(), DateEnd))
End Function

I am lost as to what code needs to be put in the control source of the field
where I want to calculate the weeks. I can type in the DateDiff("w",
DateStart, IIf(IsNull(DateEnd), Now(), DateEnd)) portion of the function and
it works, so I am left wondering what the purpose of the function is. Can
anyone set me straight?
 
T

Tom Lake

JudyB said:
I am at the end of my rope and need help desperately. I have a form and a
query where I would like to use a public function to calculate weeks.
This
is my function:

Public Function CalcWeeks(DateStart As Variant, DateEnd As Variant) As
Variant
Dim int_Weeks As Integer
I am lost as to what code needs to be put in the control source of the
field
where I want to calculate the weeks. I can type in the DateDiff("w",
DateStart, IIf(IsNull(DateEnd), Now(), DateEnd)) portion of the function
and
it works, so I am left wondering what the purpose of the function is. Can
anyone set me straight?

In the control source you'd put

=CalcWeeks([DateStart], [DateEnd])

The purpose of defining a function is so you can use it in multiple places
without having to type in the entire calculation each time.

Tom Lake
 
S

strive4peace

Hi Judy,

adding on to what Tom wrote...

.... and in a query, you would do this:

field --> NumWeeks: CalcWeeks([DateStart], [DateEnd])

~~~

"I can type in the DateDiff("w",..."

actually, you would use "ww" for weeks ... "w" is weekdays...

~~~

instead of this:
IIf(IsNull(DateEnd), Now(), DateEnd))

you can do this:

nz(DateEnd, Date())

from Help

'~~~~~~~~~~~~~~~~~~~~~~~~~~``
Nz Function

You can use the Nz function to return zero, a zero-length string ("
"), or another specified value when a Variant is Null. For example, you
can use this function to convert a Null value to another value and
prevent it from propagating through an expression.

Syntax

Nz(variant[, valueifnull])

The Nz function has the following arguments.

Argument

variant
A variable of data type Variant.

valueifnull
Optional (unless used in a query).
A Variant that supplies a value to be returned if the variant
argument is Null. This argument enables you to return a value other than
zero or a zero-length string.

Note If you use the Nz function in an expression in a query without
using the valueifnull argument, the results will be a zero-length string
in the fields that contain null values.


'~~~~~~~~~~~~~~~~~~~~~~~~~~`

~~~

the difference between NOW and DATE is that Now also gives you the
current time ... and if all you want is a date, you do not want to use Now

~~~
Access stores date/times in a numeric format where the integer portion
of the number represents the date and the decimal portion of the number
represents time:

1/1/100 --> -657,434
1/2/100 --> -657,433
12/30/1899 --> 0
1/1/1950 --> 18,264
1/1/2005 --> 38,353
12/31/2007 --> 39,447
1/1/9999 --> 2,958,101
12/31/9999 --> 2,958,465

the time is a fraction of the day

12 noon is 0.5
6pm is 0.75

1/1/2005, 12 noon --> 38353.5

if you have a control with just a date and you want to make sure it
converts to a whole number (or it is stored in text format), use

DateValue([control_or_fieldname])
or
cLng([control_or_fieldname])

likewise, if you have a time, you can force it to the fractional part by

TimeValue([control_or_fieldname])

since dates are whole numbers and times are the fractions, you can also
do arithmetic operations on them

that is why you can subtract one date from another and get the number of
days between the two.

Because dates can also have a time component, it is handy to use
DateDiff and DateAdd, which let you specify the time increment (year,
month, day, hour, etc) to calculate new dates or get a difference
between dates.

The DateDiff function can be used to specify what time increment you
want returned when you subtract dates

Likewise, there is a DateAdd function to add specific time increments to
a date



Warm Regards,
Crystal

Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace

*
:) have an awesome day :)
*




Tom said:
JudyB said:
I am at the end of my rope and need help desperately. I have a form
and a
query where I would like to use a public function to calculate weeks.
This
is my function:

Public Function CalcWeeks(DateStart As Variant, DateEnd As Variant) As
Variant
Dim int_Weeks As Integer
I am lost as to what code needs to be put in the control source of the
field
where I want to calculate the weeks. I can type in the DateDiff("w",
DateStart, IIf(IsNull(DateEnd), Now(), DateEnd)) portion of the
function and
it works, so I am left wondering what the purpose of the function is.
Can
anyone set me straight?

In the control source you'd put

=CalcWeeks([DateStart], [DateEnd])

The purpose of defining a function is so you can use it in multiple places
without having to type in the entire calculation each time.

Tom Lake
 
J

JudyB

Hi Crystal,
Thanks for the help. I made your suggested changes, but I am still not
getting something right. This is what I have for the function:

Public Function CalcWeeks(DateStart As Variant, DateEnd As Variant) As Variant
Dim int_Weeks As Integer

If IsNull(DateStart) Then
CalcWeeks = Null
Exit Function
End If

int_Weeks = DateDiff("ww", DateStart, Nz(DateEnd, Date())

End Function

The line "int_Weeks = DateDiff("ww",..." above is showing in red. Don't
understand what I am doing wrong there. Error message says Syntax error.

In the Control Source of the Form I have the following:

=CalcWeeks([DateStart],[DateEnd])

In the field of the query I have this:

WeeksService: CalcWeeks([DateStart],[DateEnd])

Can you identify my problem, aside from the fact that I am so green at this?
Thanks so much for any help.




--
JudyB


strive4peace said:
Hi Judy,

adding on to what Tom wrote...

.... and in a query, you would do this:

field --> NumWeeks: CalcWeeks([DateStart], [DateEnd])

~~~

"I can type in the DateDiff("w",..."

actually, you would use "ww" for weeks ... "w" is weekdays...

~~~

instead of this:
IIf(IsNull(DateEnd), Now(), DateEnd))

you can do this:

nz(DateEnd, Date())

from Help

'~~~~~~~~~~~~~~~~~~~~~~~~~~``
Nz Function

You can use the Nz function to return zero, a zero-length string ("
"), or another specified value when a Variant is Null. For example, you
can use this function to convert a Null value to another value and
prevent it from propagating through an expression.

Syntax

Nz(variant[, valueifnull])

The Nz function has the following arguments.

Argument

variant
A variable of data type Variant.

valueifnull
Optional (unless used in a query).
A Variant that supplies a value to be returned if the variant
argument is Null. This argument enables you to return a value other than
zero or a zero-length string.

Note If you use the Nz function in an expression in a query without
using the valueifnull argument, the results will be a zero-length string
in the fields that contain null values.


'~~~~~~~~~~~~~~~~~~~~~~~~~~`

~~~

the difference between NOW and DATE is that Now also gives you the
current time ... and if all you want is a date, you do not want to use Now

~~~
Access stores date/times in a numeric format where the integer portion
of the number represents the date and the decimal portion of the number
represents time:

1/1/100 --> -657,434
1/2/100 --> -657,433
12/30/1899 --> 0
1/1/1950 --> 18,264
1/1/2005 --> 38,353
12/31/2007 --> 39,447
1/1/9999 --> 2,958,101
12/31/9999 --> 2,958,465

the time is a fraction of the day

12 noon is 0.5
6pm is 0.75

1/1/2005, 12 noon --> 38353.5

if you have a control with just a date and you want to make sure it
converts to a whole number (or it is stored in text format), use

DateValue([control_or_fieldname])
or
cLng([control_or_fieldname])

likewise, if you have a time, you can force it to the fractional part by

TimeValue([control_or_fieldname])

since dates are whole numbers and times are the fractions, you can also
do arithmetic operations on them

that is why you can subtract one date from another and get the number of
days between the two.

Because dates can also have a time component, it is handy to use
DateDiff and DateAdd, which let you specify the time increment (year,
month, day, hour, etc) to calculate new dates or get a difference
between dates.

The DateDiff function can be used to specify what time increment you
want returned when you subtract dates

Likewise, there is a DateAdd function to add specific time increments to
a date



Warm Regards,
Crystal

Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace

*
:) have an awesome day :)
*




Tom said:
JudyB said:
I am at the end of my rope and need help desperately. I have a form
and a
query where I would like to use a public function to calculate weeks.
This
is my function:

Public Function CalcWeeks(DateStart As Variant, DateEnd As Variant) As
Variant
Dim int_Weeks As Integer
I am lost as to what code needs to be put in the control source of the
field
where I want to calculate the weeks. I can type in the DateDiff("w",
DateStart, IIf(IsNull(DateEnd), Now(), DateEnd)) portion of the
function and
it works, so I am left wondering what the purpose of the function is.
Can
anyone set me straight?

In the control source you'd put

=CalcWeeks([DateStart], [DateEnd])

The purpose of defining a function is so you can use it in multiple places
without having to type in the entire calculation each time.

Tom Lake
 
D

Douglas J. Steele

You're missing a closing parenthesis.

int_Weeks = DateDiff("ww", DateStart, Nz(DateEnd, Date()))

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


JudyB said:
Hi Crystal,
Thanks for the help. I made your suggested changes, but I am still not
getting something right. This is what I have for the function:

Public Function CalcWeeks(DateStart As Variant, DateEnd As Variant) As
Variant
Dim int_Weeks As Integer

If IsNull(DateStart) Then
CalcWeeks = Null
Exit Function
End If

int_Weeks = DateDiff("ww", DateStart, Nz(DateEnd, Date())

End Function

The line "int_Weeks = DateDiff("ww",..." above is showing in red. Don't
understand what I am doing wrong there. Error message says Syntax error.

In the Control Source of the Form I have the following:

=CalcWeeks([DateStart],[DateEnd])

In the field of the query I have this:

WeeksService: CalcWeeks([DateStart],[DateEnd])

Can you identify my problem, aside from the fact that I am so green at
this?
Thanks so much for any help.




--
JudyB


strive4peace said:
Hi Judy,

adding on to what Tom wrote...

.... and in a query, you would do this:

field --> NumWeeks: CalcWeeks([DateStart], [DateEnd])

~~~

"I can type in the DateDiff("w",..."

actually, you would use "ww" for weeks ... "w" is weekdays...

~~~

instead of this:
IIf(IsNull(DateEnd), Now(), DateEnd))

you can do this:

nz(DateEnd, Date())

from Help

'~~~~~~~~~~~~~~~~~~~~~~~~~~``
Nz Function

You can use the Nz function to return zero, a zero-length string ("
"), or another specified value when a Variant is Null. For example, you
can use this function to convert a Null value to another value and
prevent it from propagating through an expression.

Syntax

Nz(variant[, valueifnull])

The Nz function has the following arguments.

Argument

variant
A variable of data type Variant.

valueifnull
Optional (unless used in a query).
A Variant that supplies a value to be returned if the variant
argument is Null. This argument enables you to return a value other than
zero or a zero-length string.

Note If you use the Nz function in an expression in a query without
using the valueifnull argument, the results will be a zero-length string
in the fields that contain null values.


'~~~~~~~~~~~~~~~~~~~~~~~~~~`

~~~

the difference between NOW and DATE is that Now also gives you the
current time ... and if all you want is a date, you do not want to use
Now

~~~
Access stores date/times in a numeric format where the integer portion
of the number represents the date and the decimal portion of the number
represents time:

1/1/100 --> -657,434
1/2/100 --> -657,433
12/30/1899 --> 0
1/1/1950 --> 18,264
1/1/2005 --> 38,353
12/31/2007 --> 39,447
1/1/9999 --> 2,958,101
12/31/9999 --> 2,958,465

the time is a fraction of the day

12 noon is 0.5
6pm is 0.75

1/1/2005, 12 noon --> 38353.5

if you have a control with just a date and you want to make sure it
converts to a whole number (or it is stored in text format), use

DateValue([control_or_fieldname])
or
cLng([control_or_fieldname])

likewise, if you have a time, you can force it to the fractional part by

TimeValue([control_or_fieldname])

since dates are whole numbers and times are the fractions, you can also
do arithmetic operations on them

that is why you can subtract one date from another and get the number of
days between the two.

Because dates can also have a time component, it is handy to use
DateDiff and DateAdd, which let you specify the time increment (year,
month, day, hour, etc) to calculate new dates or get a difference
between dates.

The DateDiff function can be used to specify what time increment you
want returned when you subtract dates

Likewise, there is a DateAdd function to add specific time increments to
a date



Warm Regards,
Crystal

Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace

*
:) have an awesome day :)
*




Tom said:
I am at the end of my rope and need help desperately. I have a form
and a
query where I would like to use a public function to calculate weeks.
This
is my function:

Public Function CalcWeeks(DateStart As Variant, DateEnd As Variant) As
Variant
Dim int_Weeks As Integer

I am lost as to what code needs to be put in the control source of the
field
where I want to calculate the weeks. I can type in the DateDiff("w",
DateStart, IIf(IsNull(DateEnd), Now(), DateEnd)) portion of the
function and
it works, so I am left wondering what the purpose of the function is.
Can
anyone set me straight?

In the control source you'd put

=CalcWeeks([DateStart], [DateEnd])

The purpose of defining a function is so you can use it in multiple
places
without having to type in the entire calculation each time.

Tom Lake
 
J

John Spencer

One other thing, you are not assigning the value to the function to be
returned. You can add a line at the end.

CalcWeeks = int_Weeks

or simply replace int_Weeks with CalcWeeks

CalcWeeks = DateDiff("ww", DateStart, Nz(DateEnd, Date()))

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================
 
J

JudyB

I have tried to add the parenthesis as you suggested but the system
automatically changes my code to: CalcWeeks = DateDiff("ww", DateStart,
Nz(DateEnd, Date)) Do not understand why it is doing that. Is there
somethingelse that I am suppose to do to prevent that? Thanks for the help!
 
J

JudyB

Update: I continue to try to get this function to work. The system still
will not let me add the parenthesis to the end of the following code -
CalcWeeks = DateDiff("ww", DateStart, Nz(DateEnd, Date))
But, my query is working correctly and calculating the weeks. The subform,
however, does not work. Gives me the #Name? error. Any suggestions on how
to get the system to accept the end of code as ...Date()))?
 
J

John Spencer

Don't worry about the missing SET of parentheses.

Access VBA often removes them after the Date function, but the Date function
still works.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 
J

JudyB

Thanks! Any thoughts as to why the code =CalcWeeks([DateStart],[DateEnd])
placed in the control source of the WeeksService field on the subform is not
working? I am getting a zero in all of the rows where I have start dates and
end dates.
 
J

John Spencer

Does the function work at all?

If you type
?CalcWeek(#1/1/2008#,#12/31/2008#)
in the immediate window of VBA does it return a value that looks correct?

If not, you might consider posting the exact code you have at this point?

If it does, then I would check the values of DateStart and DateEnd. Are
they both date fields?

Other than that, I have no idea.

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================
 

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