View Single Post
  #4  
Old March 23rd, 2010, 08:06 PM posted to microsoft.public.access.forms
John W. Vinson
external usenet poster
 
Posts: 18,261
Default Calculated a field in a subform.

On Tue, 23 Mar 2010 19:31:55 GMT, "JOSELUIS via AccessMonster.com"
u58123@uwe wrote:

Oh my God! Iīve tried everything but it doesnīt work.Firstly I put IIF in the
calculated control TotalDays but it didnīt calculate the total number of
days if DateOUT is null . I look for a syntax error but everything seems to
be allright so I tried the VB code but I dont know where I need to put the
code in TotalDays because if i only write code in the AfterUpdate event of
DateIn and DateOut it doesnīt work. Therefore i created a function but I
think I problably made a mistake . My function:
Function TotalDays() As Integer
Dim DateIn As Date
Dim DateOut As Date
If IsNull(DateOut) Then
TotalDays = DateDiff("d", [DateIn], [Date])
Else
TotalDays = DateDiff("d", [DateIn], [DatOut])
End If
End Function


I'm not sure why you're having all the problems, but if there are textboxes
named DateIn and DateOut (not DatOut!!!) on your form, you should be able to
just use a control source of

=DateDiff("d", [DateIn], NZ([DateOut], Date())

This assumes that DateIn will always have a date; if it might be null,

=IIF(IsNull([DateIn], Null, DateDiff("d", [DateIn], NZ([DateOut], Date()))

Note Date() - the function - rather than [Date], which would refer to a field
or control.
--

John W. Vinson [MVP]