• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

VBA Code to trigger event - Convert excel formula to expression - to VBA

Original issue was: In Access 2003/XP/2000/97, I'm trying to build an expression that
will do the same as the following Excel formula with the result formatted as

=IF(TYPE(CODE(B11))=1,IF(F11=0,(E11-$B$6)/7,(E11-F11)/7)," ")

I've tried this expression but it's not working??

to count the number of days between two
dates and divide the result by 7, in which case you need something
along the lines of;

I understand that Access cannot
ADD amd SUBTRACT directly (+/-) ...DateDiff , DateAdd

VBA code tha triggered by an event -

IIf(IsNull([1_ID_REFERENCE),Null,IIf([5_ACTUAL]=0,DateDiff("d", [4_Plan],
Date)/7,DateDiff("d", [4_PLAN], [5_ACTUAL])/7)))[/size]

See the attached screen shots for view of access form and origianl excel spreadsheet

I need help with the following VBA Function

Public Function fnTestThis(dat4Plan As Date, dat5Actual As Date) As Long

fnTestThis = CLng(DateDiff("d", dat4Plan, dat5Actual))

End Function

I need help to write this to determine what to pass to that function to execute on whatever event you need it to gve result as original excel formula:------------------------------------------
I need help to sort out the data validation and error handling.

dim dat_4Plan as date
dim dat_5Actual as date
dim lngDelta as long 'datediff number that will be retd from function

dat_4Plan = me.4_Plan

if me.5_Actual=0 then
dat_5Actual = date()

lngDelta = fnTestThis(dat_4Plan, dat_5Actual)

1 Solution
Jim P.Commented:
So basically you are trying to find the number of weeks between two dates?

>> I understand that Access cannot ADD and SUBTRACT
>> directly (+/-) ...DateDiff , DateAdd

It can but has a tendency to innacuracy depending on the input value. The DateAdd and DateDiff for you to specify the results you are looking at.

?fnTestThis(date()-75, date())
?fnTestThis(date()-81, date())

Converting this code:

=IF(TYPE(CODE(B11))=1,IF(F11=0,(E11-$B$6)/7,(E11-F11)/7)," ")
It would be something like

=Iif(Nz(MyRefField,0)=1, Iif(Nz(MyRefField1,0)=0, CLng(DateDiff("d",PlanDate,ActualDate)/7), CLng(DateDiff("d",PlanDate,FinishDate)/7))
Public Function fnTestThis(dat4Plan As Date, dat5Actual As Date) As Long
fnTestThis = CLng(DateDiff("d", dat4Plan, dat5Actual)/7)
End Function

Open in new window


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now