Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / February 2005

Tip: Looking for answers? Try searching our database.

Dtermine the Period

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eric - 04 Feb 2005 16:37 GMT
Hi,
I have 1 Table "Periods" with 3 fields:
From date     -     To date      -    Period
01/01/2004-  02/02/2004 -       01
03/02/2004-  02/03/2004 -       02 ..........
I have 13 periods.

In my Form I have a field "Date" and a field "Period"
I would like when user type the "Date", it giving automatically the exact
period from the Table.
I did it with this formula but doesn't work perfectly, is the a simply way
to do it ?

(If Me![Date] >= "2004/04/01" And Me![Date] <= "2004/05/01" Then
Me![Periode] = 1
End If )

Thanks
SHIPP - 04 Feb 2005 17:45 GMT
Here's a function that will calculate the period for you. Simply call it from
anywhere and supply the date.

Option Compare Database
Option Explicit

Public Function intPeriod(dtmCurrent As Date) As Integer
 
 
 
 On Error GoTo HandleErr
 Const cstrProcName As String = "stdCalcPeriod - intPeriod"
 
 Dim intJDay      As Integer
 Dim dblPeriod    As Double
 Dim intPer       As Integer
 Dim Jan31WkDay   As Integer
 Dim intYear      As Integer
 Dim Jan31TheYear As Date
 Dim intPerStart  As Integer

 intJDay = DatePart("Y", dtmCurrent)
 intYear = DatePart("YYYY", dtmCurrent)
 Jan31TheYear = DateSerial(intYear, 1, 31)
 Jan31WkDay = DatePart("W", Jan31TheYear)
 Select Case Jan31WkDay
   Case 1
     intPerStart = 31
   Case 2
     intPerStart = 30
   Case 3
     intPerStart = 29
   Case 4
     intPerStart = 28
   Case 5
     intPerStart = 27
   Case 6
     intPerStart = 26
   Case 7
     intPerStart = 32
 End Select
 dblPeriod = ((((intPerStart - 28) * -1) + intJDay) / 28) + 1
 intPer = Fix(dblPeriod)
 Select Case intPer
   Case Is < 1
     intPeriod = 13
   Case Is > 13
     intPeriod = 13
   Case Else
     intPeriod = intPer
 End Select

ExitHere:
 Exit Function

HandleErr:
 intPeriod = 0
 Select Case Err.Number
   Case Else
     MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical,
cstrProcName
 End Select
 Resume ExitHere

End Function

> Hi,
> I have 1 Table "Periods" with 3 fields:
[quoted text clipped - 14 lines]
>
> Thanks
Tim Ferguson - 04 Feb 2005 17:54 GMT
> Hi,
> I have 1 Table "Periods" with 3 fields:
> From date     -     To date      -    Period
> 01/01/2004-  02/02/2004 -       01
> 03/02/2004-  02/03/2004 -       02 ..........
> I have 13 periods.

 '
 ' Create the criterion carefully: note proper formats
 ' By the way, using names like "Date" is a Really Bad Idea
 ' So is using table names and field names with spaces in them
 '
 strWhere = _
   "FromDate <= " & format(Me!DateControl,"\#yyyy\-mm\-dd\#") & _
   "  AND ToDate >= " & format(Me!DateControl,"\#yyyy\-mm\-dd\#")

 '
 ' check it looks okay; remove this line once it's debugged
 '
 msgbox strWhere

 '
 ' look up result
 '
 wPeriod = DLookup("Period", "Schedules", strWhere)

There is probably a neater way using Max(), but this is easy to maintain
and to understand.

Hope it helps

Tim F
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.