How to Use a function

J

James

Ok I've got a function that takes all the values in a table and calculates
the total time. The function is called AddTimes See below


Function AddTimes()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim time1 As Date
Dim time2 As Date
Dim TimeAccum As Integer

Set db = CurrentDb
Set rs = db.OpenRecordset("WebProxyLog")
rs.MoveNext
Do
rs.MovePrevious
time1 = rs!logTime
rs.MoveNext
time2 = rs!logTime
If DateDiff("n", time1, time2) < 5 Then
TimeAccum = TimeAccum + DateDiff("n", time1, time2)
End If
rs.MoveNext
Loop Until rs.EOF
AddTimes = TimeAccum

End Function


How do I make a report or form that will display this.
If I run the function with debug.print it displays the proper value so its
working, but how to i get it to work with a report or form?

Thanks
 
M

M.L. Sco Scofield

In the "Control Source" property of a text box, put:

=AddTimes()

Good luck.

Sco
 
J

James

Also before I run the report, it promts me to enter in a value for
AddTimes()
Why would it do this?
 
M

M.L. Sco Scofield

It works just fine here.

The only reason this would happen is because the report can't find your
function.

Where did you put your function?

I should be either in the module of the report you are calling it in or in a
standard module.

Sco
 
J

James

I just pressed the modules button and created a new one there? How should I
be doing it?
thanks
 
J

James

would you be willing to email it to be the way you set it up and got it
working?
jtucke AT creativedoor DOT com
 
M

M.L. Sco Scofield

A standalone module should be just fine.

Do you have "Public" in front of the Function line? Also, although it won't
affect the operation, good programming would call for an "As Integer" at the
end of the line. I.e.:

Public Function AddTimes() As Integer

Another possibility is a name conflict. What is the name of the text box the
function is in on your report? I'd call it "txtTotalTimes" unless you're
already using that name for another text box.

Are you sure there aren't any extra characters in the text box's control
source. (Try using the zoom box, <Shift>+<F2>, to look for an extra space or
carriage return or something that shouldn't be there.) It should be
=AddTimes() exactly with no extra characters of any kind.

Although it didn't seem to matter, (I put it in several different sections
and it worked just fine,) what section is your text box in?

Last, but certainly not least, is *all* your code compiling with *no*
errors?

Sco
 

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