473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to calculate datediff for a field based on two other fields?

sueb
379 256MB
I inherited a database that contains three fields:

- OR Time (a number)
- OR Time In Room (Date/Time)
- OR Time Out Room (Date/Time)

On the form is an unbounded field that correctly calculates and displays the minutes between the patient's entering the OR room and leaving the room (using DateDiff with the minutes option, 'n'). The "OR Time" field in every record in the database is blank.

I want to calculate that time difference based on any change to either of the Date/Time fields and store it in the field "OR Time". The department now wants a report that sums "OR Time"s on a monthly basis, and I don't want to waste time re-calculating this field everytime the report is run. I have set the field "OR Time" to Locked in preparation for making this work.

I haven't been able to figure out the syntax, however, to make this happen. I created the following public function to call "After Update" for either of the "room" fields:

Expand|Select|Wrap|Line Numbers
  1. Public Function Calculate_Minutes(Date1 As Date, Date2 As Date) As Number
  2.     Calculate_Minutes = DateDiff("n", Date1, Date2)
  3. End Function
  4.  
but the error I get is "Compile error. User-defined type not defined." pointing to the function's declaration statement.

The call looks like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub OR_Time_Out_Room_AfterUpdate()
  2.     Me.[OR Time] = Calculate_Minutes(Me.OR_Time_In_Room, Me.OR_Time_Out_Room)
  3. End Sub
  4.  
and generates no error of its own.

What am I doing wrong?
Feb 16 '11 #1

✓ answered by ADezii

The problem is with the Return Type of the Function, namely Number. There is no Data Type named Number. To play it safe, redefine your Data Type:
Expand|Select|Wrap|Line Numbers
  1. Public Function Calculate_Minutes(Date1 As Date, Date2 As Date) As Long

8 2488
ADezii
8,834 Expert 8TB
The problem is with the Return Type of the Function, namely Number. There is no Data Type named Number. To play it safe, redefine your Data Type:
Expand|Select|Wrap|Line Numbers
  1. Public Function Calculate_Minutes(Date1 As Date, Date2 As Date) As Long
Feb 16 '11 #2
sueb
379 256MB
Okay, that makes sense. I thought "Number" was odd for a datatype, but I didn't get an error within the code window itself, so I thought it was okay.

So I fixed up all that (and the calls were a little wonky, too, it turns out), but I'm getting this from the public function:

"Reserved error (-1517); there is no message from this error"

which leaves me a little stumped as to where to go from here. Here's the function as it stands:

Expand|Select|Wrap|Line Numbers
  1. Public Function Calculate_Minutes(Date1 As Date, Date2 As Date) As Long
  2. On Error GoTo Err_Calculate_Minutes
  3.  
  4.     Calculate_Minutes = DateDiff("n", Date1, Date2)
  5.  
  6. Exit_Calculate_Minutes:
  7.     Exit Function
  8.  
  9. Err_Calculate_Minutes:
  10.     MsgBox Err.Description
  11.     Resume Exit_Calculate_Minutes
  12.  
  13. End Function
  14.  
and the call from one of the fields:

Expand|Select|Wrap|Line Numbers
  1. Private Sub OR_Time_In_Room_AfterUpdate()
  2. On Error GoTo Err_OR_Time_In_Room_AfterUpdate
  3.  
  4.     Me.[OR Time] = Calculate_Minutes(Me.OR_Time_In_Room, Me.OR_Time_Out_Room)
  5.  
  6. Exit_OR_Time_In_Room_AfterUpdate:
  7.     Exit Sub
  8.  
  9. Err_OR_Time_In_Room_AfterUpdate:
  10.     MsgBox Err.Description
  11.     Resume Exit_OR_Time_In_Room_AfterUpdate
  12.  
  13. End Sub
Feb 16 '11 #3
ADezii
8,834 Expert 8TB
It appears that it should work now, sueb, and I'm not really sure why it doesn't. If you wish to Attach the Front End, I'll be happy to look at it. I still have an Archived copy of the Back End, and I'm assuming that it is still applicable. If you do Attach the FE, please indicate what Form is the one in question.
Feb 16 '11 #4
sueb
379 256MB
I came in this morning and tried it again, right before getting ready to upload a copy, and it does work now.

It's not the first time that I've experienced that: I work on a bit of code for several hours into the evening; it looks like it should work but doesn't; I go home, come back, and just fire it off again in the morning, and it works.

Mystifying.

So, thanks again for steering me onto the path of righteousness, ADezii!
Feb 16 '11 #5
sueb
379 256MB
Here's another thread that addresses this -1517 error, and some of what it says sounds like it might be applicable to my situation. I'm going to investigate further.

http://bytes.com/topic/access/answer...-message-error
Feb 16 '11 #6
NeoPa
32,556 Expert Mod 16PB
Sue, what you are suggesting (and now have working it appears) is exactly the sort of thing that we try to warn developers away from. This is de-normalising your data and is generally considered a very bad idea. The concepts behind normalisation, and why it is so important, are covered in Database Normalisation and Table structures. I highly recommend reading it, and reverting your database back to a more normalised form.
Feb 17 '11 #7
sueb
379 256MB
NeoPa, I realize that this is a little unorthodox, and at some point I'll probably re-work it. For now, I'm just going to leave it as is. I'm not even sure that the user's requirements around this issue are settled yet.

But thanks for the link--it's always good to have references handy!
Feb 23 '11 #8
NeoPa
32,556 Expert Mod 16PB
Not my recomendation clearly, but as long as you're making an informed choice on the matter I can relax knowing I've done what I need to. I also understand, of course, the restrictions imposed when working on inherited databases. Always more tricky than ones you've designed yourself.

Good luck with your project :-)
Feb 23 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: jochen scheire | last post by:
Is there a way I can calculate a field in a form based on another field in the same form. When clicking submit, both values should be posted to the next page. I want to be able to type in a value...
3
by: carla | last post by:
I am using Access 2000 and have a table, tblCurrent. That table contains several fields - three of those fields are , and . What I want to accomplish in my data entry form, is first entering the...
1
by: Shawn Yates | last post by:
I have query which shows when employees clock in (long time) and clock out(long time). To find the total hours worked in hours and minutes I have another field on my query with the following: ...
14
by: SpyderSL | last post by:
Hey, I have created an access form, in which I have a drop down with employee names. These are the steps I would like to happen: 1. A user will enter a number in FIELD A 2. The user will...
3
by: Michelle Anderson | last post by:
I have an access form. In the form, it co ntains 3 fields : Type, hours, and Amount. Field "Type" is a combo box and it contains 2 values: Standard and Rush Field " Hours" is just a numeric...
5
by: CSmith | last post by:
I have a calculated field in a query, it calculates the # of days an employee has been employed using the following statement =datediff("d", , NOW()) by default. When an employee is terminated, I...
1
by: compl | last post by:
I have a database that I need to restrict the ability to edit one field based on another. The two fields are DOS (date) and Amount (currency). I only want the users to be able to edit the Amount...
4
by: whamo | last post by:
I have the need to populate a field based on the selection in a combo box. Starting out simple. (2) tables tbl_OSE_Info and tbl_Input; tbl_OSE_Info has three fields: Key, OSE_Name and OSE_Wt...
5
by: Shortstuff12345 | last post by:
I'm trying to use VBA code to disable a field based on the value of another field in a form. The code I have properly updates the enabled property of the field when it changes; however, it changes...
17
by: MNNovice | last post by:
Calculate net amount based on conditions ________________________________________ I need to calculate a net amount that is based on FedRatio. Normally the FedRatio is set to be 80% but there are a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.