Planning

dummy1

Registered User.
Local time
Today, 12:19
Joined
Oct 3, 2008
Messages
46
Hello All,

I ve downloaded a database planboard what i integrated in my Database planning. Now i ve got a problem with tht form Hourgrid. I m not able to add timeschedules from 1 to 11 day of each month. I suppose it has to do something with dd,mm,yyyy or mm,dd,yyyy date format. After 3 days of search and changing stuff, i am realy stucked in to this problem.

Some one have maybe a solution?

I uploaded the mdb, maybe someone can find the problem

Greets

Dummy
 

Attachments

Hi,

A bit of a guess but i might help you...

I think that access uses the date format as per your computer's settings for the dates you see (e.g. if you are in the UK then a date on a form will be automatically dd/mm/yyyy) but no matter where you are access VBA uses the US format (mm/dd/yyyy).

When you are using your UK dates in code then you first need to convert them:

say for example the date is sorted in a variable called "YourDate", then use this code everytime you reference the date:

Format(YourDate, "mm\/dd\/yyyy")

That's change it in US date format.

Not sure if that's the answer for you (sorry I've not been through your file, I'm not that good at access), but I had a similar problem last week and that did the trick!

ALl the best,

Phil.
 
Thanks Phil,

I will try it out, I know what you mean, the only thing is i dont know excactly where to put it.

Regards,

Dummy
 
if you post the code here I'll have a look....
 
Hello,

I put the code inside .

Regards,

Dummy
 

Attachments

hi sorry work went nuts for a few days there, I'm back now.

Could you post the code snippet you are not sure of up here please? thanks. Sorry I know nothing about viruses so I don't download stuff much you see!

Thanks.
 
The code

Code:
Option Compare Database
Option Explicit
 
' data access objects needed
Dim mdbT As DAO.Database ' Target DB w/ unit & period tbls, default CurrentDb
Dim mrsU As DAO.Recordset ' Unit recordset
Dim mrsP As DAO.Recordset ' Period recordset
 
' Constants to identify the tables/queries
' Replace these with the names specific to
' your own application (see user manual).
' Note that the UnitRS can be either a table name,
' or saved query, or a SQL statement.
' Both recordsets must include the Primary Key field.
' The unit set must also include a display field, which
' may or may not be the same as the primary key.
' The period set must include the Primary Key field and
' both the FromDate field and the ThruDate field.
Const cUnitRS As String = "SELECT * FROM tblUnit ORDER BY Unit;"
' You must use a table or query name for the period,
' since a SQL statement that limits the period recordset
' to the currently displayed date range is created at run time.
Const cPeriodRS As String = "tblHourPeriod"
 
' constants for the names of the primary key fields
Const cUnitPK As String = "UnitID"
Const cPeriodPK As String = "PeriodID"
' constants for the field to display from the unit set
' note that this can be the same as the primary key
' but can be another field as well (as in the default)
Const cUnitDisplayFld As String = "Unit"
 
' constants for the data type of the PK fields
' DesGrid only supports single field primary keys
' of Number or Text data types. If your primary
' key fields are text fields, use the value "String",
' otherwise leave them as "Numeric".
Const cUnitPKDataType As String = "Numeric"
Const cPeriodPKDataType As String = "Numeric"
 
' constants for the names of the date fields used
' in the period table. These must be of data type
' Date/Time and there must be two of them:
' one for the FromDate (starting date) and
' one for the ThruDate (ending date)
Const cFromFld As String = "FromDate"
Const cThruFld As String = "ThruDate"
Const cColorKeyFld As String = "ColorKey"
 
' constant for the name of the color key table
Const cColorTbl As String = "trefColorKey"
 
' variables to hold selected dates and units
Dim mdSelFrom As Date
Dim mdSelThru As Date
Dim mcolSelUnits As New Collection
 
' array for currently displayed dates
Dim madDisplay(1 To 32) As Date
' constant for special effect for selected image boxes
Const dsRaised As Long = 1
Const dsSunken As Long = 2
Const dsEtched As Long = 3
 
' stores true if the grid is clear
' we check this before running code to
' clear the grid (speeds scrolling)
Dim mfGridClear As Boolean
 
' width (in twips) of a day toggle
Const cHourW As Integer = 288
' number of image boxes in a row
Const cNumImgPerRow As Integer = 32
 
' dummy value for date variables
Const cBlankDate As Date = #1/1/100#
 
' variable to store state of selected grid boxes
Dim mfSelectedGrid As Boolean
 
Private Sub Form_Load()
 
Dim dOpen As Date
 
' initialize
mdSelFrom = cBlankDate
mdSelThru = cBlankDate
mfSelectedGrid = False
 
' fill the recordsets
LoadData
 
' format calendar portion of the form
' if a date was passed in OpenArgs, use that, else use Date
If Not IsDate(OpenArgs) Then
dOpen = DateSerial(Year(Date), Month(Date), Day(Date)) + TimeSerial(Hour(Time), 0, 0)
SetupCal dOpen
Else
dOpen = CDate(OpenArgs)
dOpen = DateSerial(Year(dOpen), Month(dOpen), Day(dOpen)) + TimeSerial(Hour(dOpen), 0, 0)
SetupCal dOpen
End If
 
' format unit list portion of the form
FillUnits mrsU.Fields(cUnitPK)
 
' you might want to clear the grid instead
' depending on how you want the form to initialize
DrawGrid
 
' set up the color key from table data
GetColorKeyCaptions
 
End Sub
 
Public Function DrawGrid() As Boolean
' puts colored diamonds and arrows on the grid
' according to data in the period recordset
On Error GoTo ErrLine
 
Dim sqlU As String
Dim x As Integer, iCol As Integer
Dim Y As Integer, sColor As String
Dim iImgFirst As Integer, iImgLast As Integer
Dim sUnitID As String
Dim fFromOff As Boolean, fThruOff As Boolean
Dim iFirstInRow As Integer, iTimeUnit As Integer
Dim tgl As ToggleButton
 
' open a recordset of periods in the current date range
sqlU = "SELECT " & cUnitPK & ", " & cColorKeyFld & ", " & _
cFromFld & ", " & cThruFld & " FROM " & cPeriodRS & _
" WHERE (" & cFromFld & " <= #" & madDisplay(cNumImgPerRow) & "# " & _
"AND " & cThruFld & " >= #" & madDisplay(1) & "# " & _
"AND (" & EnumUnits & ")) " & _
"ORDER BY " & cUnitPK
Set mrsP = mdbT.OpenRecordset(sqlU)
 
With mrsP
' exit if no records
If (.BOF And .EOF) Then
On Error Resume Next
.Close
Set mrsP = Nothing
ClearGrid
DisplayGrid True
DisplaySelectedGrid
GoTo ExitLine
End If
' work through the records, drawing arrows/diamonds on the appropriate row
ClearGrid
.MoveFirst
Do Until .EOF ' if no period for this unit, on to next unit toggle
' find the row number that matches the current record
For x = 1 To 10
Set tgl = Me.Controls("tglUnit" & Format(x, "00"))
If tgl.Tag = .Fields(cUnitPK) Then
Exit For
End If
Next x
' check to see if the from/thru dates are off screen
fFromOff = (.Fields(cFromFld) < madDisplay(1))
fThruOff = (.Fields(cThruFld) > madDisplay(cNumImgPerRow) + 1)
' Determine which image box represents our first date
iFirstInRow = ((x - 1) * cNumImgPerRow) + 1
If fFromOff Then
iTimeUnit = 0
Else
Select Case grpHour
Case 1
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cFromFld)) / 60
Case 2
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cFromFld)) / 30
Case 3
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cFromFld)) / 15
End Select
End If
iImgFirst = iFirstInRow + iTimeUnit
' determine which image box represents our last date
If .Fields(cThruFld) > madDisplay(cNumImgPerRow) Then
iImgLast = x * cNumImgPerRow
Else
Select Case grpHour
Case 1
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cThruFld)) / 60
Case 2
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cThruFld)) / 30
Case 3
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cThruFld)) / 15
End Select
' remove the '- 1' from the following line if you want the arrow to
' go through the ending time field
iImgLast = iFirstInRow + (iTimeUnit - 1)
End If
' draw the appropriate symbol
sColor = .Fields(cColorKeyFld)
If iImgFirst >= iImgLast Then
' one-unit period, so use a diamond
SetPic iImgFirst, sColor, "D"
Else
' multi-day period, so line w/arrows
If fFromOff Then
SetPic iImgFirst, sColor, "B"
Else
SetPic iImgFirst, sColor, "L"
End If
If fThruOff Then
If (.Fields(cFromFld) = madDisplay(cNumImgPerRow)) Then
SetPic iImgLast, sColor, "L"
Else
SetPic iImgLast, sColor, "B"
End If
Else
SetPic iImgLast, sColor, "R"
End If
' now the bar in between the end points
For Y = (iImgFirst + 1) To (iImgLast - 1)
SetPic Y, sColor, "B"
Next Y
End If
.MoveNext
Loop
End With
 
DisplayGrid True
mfGridClear = False
DisplaySelectedGrid
DrawGrid = True
 
ExitLine:
Exit Function
ErrLine:
DrawGrid = False
Resume ExitLine
End Function
 
Private Function DisplaySelectedGrid() As Boolean
' Sets the special effect of selected grid image boxes
' to dsSunken. Note that this function is cosmetic, since
' there may be selected units not diplayed.
' Returns True if successful, false on error
On Error GoTo ErrLine
 
Dim iReturn As Integer
' loop counters
Dim iUnitTgl As Integer, x As Integer
' unit toggle, image box
Dim tglU As ToggleButton, imgX As Image
' image box numbers
Dim iFstImg As Integer, iLstImg As Integer
' booleans to determine displayability of arrows
Dim fFirstOff As Boolean, fLastOff As Boolean
Dim fBigWide As Boolean ' a range that spans beyond both sides of the grid
Dim iInterval As Integer
 
' make sure none are sunken
If mfSelectedGrid Then
For x = 1 To 320
Set imgX = Me.Controls("img" & Format(x, "000"))
imgX.SpecialEffect = dsEtched
Next x
End If
 
' if no date is selected or no units selected, exit
If ((mdSelFrom = cBlankDate) Or (mcolSelUnits.Count = 0)) Then
iReturn = 0
mfSelectedGrid = False
GoTo ExitLine
Else
mfSelectedGrid = True
End If
 
' check if the from/thru dates are off screen
' fFirstOff is True if the first selected date has been scrolled off screen
fFirstOff = ((mdSelFrom < madDisplay(1)) Or _
(mdSelFrom > madDisplay(cNumImgPerRow)))
' fLastOff is True if the last selected date has been scrolled off screen
fLastOff = ((mdSelThru > madDisplay(cNumImgPerRow) + 1) Or _
((mdSelThru <= madDisplay(1)) And _
(mdSelThru <> cBlankDate)))
' fBigWide is True if both the first and last dates are off screen (so just a big wide bar)
fBigWide = (mdSelFrom < madDisplay(1) And mdSelThru > madDisplay(cNumImgPerRow))
 
If (fFirstOff And fLastOff) And Not fBigWide Then
' don't draw an arrow
Else
' loop through the unit toggles, looking for selected ones
For iUnitTgl = 1 To 10
Set tglU = Me.Controls("tglUnit" & Format$(iUnitTgl, "00"))
If tglU Then
' here we figure out the first and last image boxes
Select Case grpHour
Case 1: iInterval = 60
Case 2: iInterval = 30
Case 3: iInterval = 15
End Select
' clean the date values
mdSelThru = DateSerial(Year(mdSelThru), Month(mdSelThru), Day(mdSelThru)) + _
TimeSerial(Hour(mdSelThru), Minute(mdSelThru), 0)
mdSelFrom = DateSerial(Year(mdSelFrom), Month(mdSelFrom), Day(mdSelFrom)) + _
TimeSerial(Hour(mdSelFrom), Minute(mdSelFrom), 0)
If mdSelThru <= DateAdd("n", iInterval, mdSelFrom) Then
' diamond
iFstImg = ((iUnitTgl - 1) * cNumImgPerRow) + ToggleNumFromDate(mdSelFrom)
Set imgX = Me.Controls("img" & Format(iFstImg, "000"))
imgX.SpecialEffect = dsSunken
Else
' arrow
If fFirstOff Then
iFstImg = ((iUnitTgl - 1) * cNumImgPerRow) + 1
Else
iFstImg = ((iUnitTgl - 1) * cNumImgPerRow) + ToggleNumFromDate(mdSelFrom)
End If
If fLastOff Then
iLstImg = iUnitTgl * cNumImgPerRow
Else
iLstImg = ((iUnitTgl - 1) * cNumImgPerRow) + ToggleNumFromDate(mdSelThru)
End If
iLstImg = iLstImg - 1
For x = iFstImg To iLstImg
Set imgX = Me.Controls("img" & Format(x, "000"))
imgX.SpecialEffect = dsSunken
Next x
End If
End If
Next iUnitTgl
End If
 
mfGridClear = False
DisplaySelectedGrid = True
 
ExitLine:
Exit Function
ErrLine:
DisplaySelectedGrid = False
Resume ExitLine
End Function
 
Private Sub cmdDrawGrid_Click()
DrawGrid
End Sub
 
Private Sub cmdNextUnit_Click()
ScrollUnits 1
End Sub
Private Sub cmdNextUnitGrp_Click()
ScrollUnits 10
End Sub
Private Sub cmdPrevUnit_Click()
ScrollUnits -1
End Sub
Private Sub cmdPrevUnitGrp_Click()
ScrollUnits -10
End Sub
 
Last edited:
second part code

Code:
Private Function ScrollUnits(iIncrement As Integer) As Boolean
' Called from the click events of the next/previous
' unit buttons. Scrolls the list of units on the toggles.
 
On Error GoTo ErrLine
 
' loop counter
Dim x As Integer
' Unit ID to start list at
Dim vUnitID As Variant
 
' clear the grid since we've scrolled
ClearGrid
DisplayGrid False
 
vUnitID = tglUnit01.Tag
 
With mrsU
If cUnitPKDataType = "Numeric" Then
.FindFirst cUnitPK & " = " & vUnitID
Else
.FindFirst cUnitPK & " = '" & vUnitID & "'"
End If
Select Case iIncrement
Case 1
.MoveNext
If .EOF Then .MoveLast
Case -1
.MovePrevious
If .BOF Then .MoveFirst
Case 10
For x = 1 To 10
If Not .EOF Then
.MoveNext
Else
.MoveLast
Exit For
End If
Next x
Case -10
For x = 1 To 10
If Not .BOF Then
.MovePrevious
Else
.MoveFirst
Exit For
End If
Next x
End Select
FillUnits .Fields(cUnitPK)
End With
 
ScrollUnits = True
ExitLine:
Exit Function
ErrLine:
ScrollUnits = False
Resume ExitLine
End Function
 
Private Function LoadData() As Boolean
' loads the form-level data variables
' set target database
On Error GoTo ErrLine
 
Set mdbT = CurrentDb
' recordsets
Set mrsU = mdbT.OpenRecordset(cUnitRS, dbOpenDynaset)
Set mrsP = mdbT.OpenRecordset(cPeriodRS, dbOpenDynaset)
 
LoadData = True
 
ExitLine:
Exit Function
ErrLine:
LoadData = False
Resume ExitLine
End Function
 
Private Function FillUnits(vFirstUnitID As Variant) As Boolean
' set the captions on toggles with the current 10 displayed units
 
' loop counter variable
Dim x As Integer
' counter for For Each loop
Dim v As Variant
' current toggle being worked on
Dim tgl As ToggleButton
' set to True if any units are selected
Dim fSel As Boolean
 
fSel = (mcolSelUnits.Count > 0)
 
If cUnitPKDataType = "Numeric" Then
mrsU.FindFirst cUnitPK & " = " & vFirstUnitID
Else
mrsU.FindFirst cUnitPK & " = '" & vFirstUnitID & "'"
End If
 
For x = 1 To 10
Set tgl = Me.Controls("tglUnit" & Format(x, "00"))
tgl = False
With mrsU
If Not .EOF Then
tgl.Caption = .Fields(cUnitDisplayFld)
tgl.Tag = .Fields(cUnitPK)
Me("cmdUnit" & Format(x, "00")).Enabled = True
If fSel Then
For Each v In mcolSelUnits
If CStr(v) = CStr(.Fields(cUnitPK)) Then
tgl = True
Exit For
End If
Next v
End If
.MoveNext
Else
tgl.Caption = ""
tgl.Tag = ""
Me("cmdUnit" & Format(x, "00")).Enabled = False
End If
End With
Next x
 
' disable unit scroll buttons if needed
Dim fPrev As Boolean, fNext As Boolean
tglUnit01.SetFocus
With mrsU
.MoveFirst
If (tglUnit01.Tag = .Fields(cUnitPK)) _
Or tglUnit01.Caption = "" Then
fPrev = False
Else
fPrev = True
End If
 
.MoveLast
If (tglUnit10.Tag = .Fields(cUnitPK)) _
Or (tglUnit10.Caption = "") Then
fNext = False
Else
fNext = True
End If
End With
 
cmdPrevUnit.Enabled = fPrev
cmdPrevUnitGrp.Enabled = fPrev
cmdNextUnit.Enabled = fNext
cmdNextUnitGrp.Enabled = fNext
 
 
End Function
 
Private Function GetColorKeyCaptions() As Boolean
' gets the captions for the color key labels from the color key table
On Error GoTo ErrLine
 
Dim rsColor As Recordset
 
Set rsColor = mdbT.OpenRecordset(cColorTbl)
 
With rsColor
.MoveFirst
Do Until .EOF
Select Case !ColorKey
Case "N": SetColorKey 1, Nz(!Caption, "")
Case "R": SetColorKey 2, Nz(!Caption, "")
Case "B": SetColorKey 3, Nz(!Caption, "")
Case "G": SetColorKey 4, Nz(!Caption, "")
Case "P": SetColorKey 5, Nz(!Caption, "")
Case "Y": SetColorKey 6, Nz(!Caption, "")
End Select
.MoveNext
Loop
End With
 
GetColorKeyCaptions = True
 
ExitLine:
On Error Resume Next
rsColor.Close
Set rsColor = Nothing
Exit Function
ErrLine:
GetColorKeyCaptions = False
Resume ExitLine
End Function
 
Private Sub SetColorKey(ByVal iKey As Integer, ByVal sCaption As String)
 
Dim x As Integer
Const cKeyStart As Integer = 600
 
With Me("lblColorKey" & iKey)
If sCaption = "" Then
.Visible = False
Select Case iKey
Case 1
imgND.Visible = False
imgRD.Left = cKeyStart
lblColorKey2.Left = imgRD.Left + 240
imgBD.Left = imgRD.Left + 1500
lblColorKey3.Left = imgBD.Left + 240
imgGD.Left = imgBD.Left + 1500
lblColorKey4.Left = imgGD.Left + 240
imgPD.Left = imgGD.Left + 1500
lblColorKey5.Left = imgPD.Left + 240
imgYD.Left = imgPD.Left + 1500
lblColorKey6.Left = imgYD.Left + 240
Case 2
imgRD.Visible = False
imgBD.Left = imgRD.Left
lblColorKey3.Left = imgBD.Left + 240
imgGD.Left = imgBD.Left + 1500
lblColorKey4.Left = imgGD.Left + 240
imgPD.Left = imgGD.Left + 1500
lblColorKey5.Left = imgPD.Left + 240
imgYD.Left = imgPD.Left + 1500
lblColorKey6.Left = imgYD.Left + 240
Case 3
imgBD.Visible = False
imgGD.Left = imgBD.Left
lblColorKey4.Left = imgGD.Left + 240
imgPD.Left = imgGD.Left + 1500
lblColorKey5.Left = imgPD.Left + 240
imgYD.Left = imgPD.Left + 1500
lblColorKey6.Left = imgYD.Left + 240
Case 4
imgGD.Visible = False
imgPD.Left = imgGD.Left
lblColorKey5.Left = imgPD.Left + 240
imgYD.Left = imgPD.Left + 1500
lblColorKey6.Left = imgYD.Left + 240
Case 5
imgPD.Visible = False
imgYD.Left = imgPD.Left
lblColorKey6.Left = imgYD.Left + 240
Case 6
imgYD.Visible = False
End Select
Else
.Caption = sCaption
End If
End With
 
End Sub
Private Function SetupCal(dStart As Date) As Boolean
' Sets up the calendar (starting at dFrom)
 
If Not mfGridClear Then ClearGrid
FillDateArray dStart
NumberCal
DisplayDate
ClearGrid
DisplayGrid False
 
End Function
 
Private Sub SelectUnits(fAdd As Boolean, vUnitID As Variant)
' adds or removes (fAdd) a unit id to the collection
If fAdd Then
mcolSelUnits.Add vUnitID, vUnitID
Else
mcolSelUnits.Remove vUnitID
End If
 
lblNumUnits.Caption = mcolSelUnits.Count & " Units Selected"
 
End Sub
 
Last edited:
thirth Part code

Code:
Private Sub ToggleUnit(iToggle As Integer)
 
Dim s As String, tgl As ToggleButton
 
s = "tglUnit" & Format(iToggle, "00")
Set tgl = Me.Controls(s)
' if the tag property contains no data, keep it deselected
If tgl.Tag = "" Then
tgl = False
Exit Sub
Else
SelectUnits tgl.Value, tgl.Tag
End If
 
DisplaySelectedGrid
 
End Sub
 
Public Function ClearGrid() As Boolean
' clears all pictures from the grid
On Error GoTo ErrLine
 
' don't bother doing this if we're already clear
If mfGridClear Then GoTo ExitLine
 
Dim x As Integer
Dim imgX As Image
 
For x = 1 To 320
Set imgX = Me.Controls("img" & Format(x, "000"))
With imgX
.Picture = ""
.SpecialEffect = dsEtched
.Tag = ""
End With
Next x
 
mfGridClear = True
 
ClearGrid = True
 
ExitLine:
Exit Function
ErrLine:
ClearGrid = False
Resume ExitLine
End Function
 
Public Sub DisplayGrid(fShow As Boolean)
' makes the grid visible or invisible
 
Dim x As Integer, img As Image
If img001.Visible = fShow Then Exit Sub
 
For x = 1 To 320
Set img = Me.Controls("img" & Format(x, "000"))
img.Visible = fShow
Next x
 
End Sub
 
Private Function Untoggle() As Boolean
' Sets all unit and day toggles to False (up)
On Error GoTo ErrLine
 
Dim x As Integer
 
' unit toggles
For x = 1 To 10
With Me.Controls("tglUnit" & Format(x, "00"))
If .Value Then
.Value = False
ToggleUnit x
End If
End With
Next x
 
' day toggles
For x = 1 To cNumImgPerRow
With Me.Controls("tglHour" & Format(x, "00"))
If .Value Then
.Value = False
ToggleHour x
End If
End With
Next x
 
Untoggle = True
 
ExitLine:
Exit Function
ErrLine:
Untoggle = False
Resume ExitLine
End Function
 
 
Public Function ImgClick(iImg As Integer) As Boolean
' Use this function to perform any desired activity when
' an image is clicked. Returns True if successful
 
On Error GoTo ErrLine
 
Dim d As Date, vUnitID As Variant
Dim iSubDate As Integer, iSubUnit As Integer
Dim sColor As String, sArrow As String
 
' store the color/arrow type of the image clicked
With Me.Controls("img" & Format$(iImg, "000"))
If .Tag <> "" Then
sColor = Left(.Tag, 1)
sArrow = Right(.Tag, 1)
Else
' no period here, so exit
GoTo ExitLine
End If
End With
 
' get the right subscript for the date
iSubDate = iImg Mod cNumImgPerRow
If iSubDate = 0 Then iSubDate = cNumImgPerRow
 
' get the unit toggle number
If iImg Mod cNumImgPerRow = 0 Then
iSubUnit = Int(iImg / cNumImgPerRow)
Else
iSubUnit = Int(iImg / cNumImgPerRow) + 1
End If
 
' store the date of the image clicked
d = madDisplay(iSubDate)
 
' store the unit of the image clicked
' this gives "" (empty string) if no unit matches
vUnitID = Me.Controls("tglUnit" & Format$(iSubUnit, "00")).Tag
 
' Now we've got the ID of the unit (sUnitID) and the date (d)
' associated with the image box clicked. Also the color and arrow
' (stored in sColor and sArrow).
' Use these to display information or a data form
' with code you write (or better still, call a function) below.
 
 
 
 
 
 
ImgClick = True
 
ExitLine:
Exit Function
ErrLine:
Debug.Print Error
ImgClick = False
Resume ExitLine
End Function
 
Private Function ToggleNumFromDate(dIn As Date) As Integer
' returns the number of the toggle that matches the passed in date
' returns 0 if vDate is off the grid
 
Dim x As Integer, iReturn As Integer
 
If dIn < madDisplay(1) Or dIn > madDisplay(cNumImgPerRow) Then
' date is off the grid
iReturn = 0
Else
For x = 1 To cNumImgPerRow
If madDisplay(x) = dIn Then
iReturn = x
Exit For
End If
Next x
End If
 
ToggleNumFromDate = iReturn
 
End Function
 
Private Sub grpHour_AfterUpdate()
SetupCal Date + TimeSerial(Hour(madDisplay(1)), 0, 0)
End Sub
 
Private Sub grpMonth_AfterUpdate()
SetupCal DateSerial(Year(madDisplay(1)), grpMonth, Day(madDisplay(1))) + _
TimeSerial(Hour(madDisplay(1)), Minute(madDisplay(1)), 0)
End Sub
 
 
Private Sub DisplayDate()
' This routine sets up the day labels, month toggles and year cmd
' controls on the calendar portion of the form.
 
' counter variable
Dim x As Integer
' first date/time in display array
Dim dFirst As Date
' length of days, in toggle widths
Dim iDay1 As Integer, iDay2 As Integer, iDay3 As Integer
' temp string for the caption of the day labels
Dim sCap As String
 
 
' initialize
dFirst = madDisplay(1)
 
' set the month and year controls to the first month/year displayed
grpMonth = Month(dFirst)
cmdYear.Caption = "Year " & Year(dFirst)
 
' now do the day labels
' start by hiding the second two labels
With lblDay2
.Width = 0
.Visible = False
End With
With lblDay3
.Width = 0
.Visible = False
End With
 
' get the widths of the labels
DayWidth iDay1, iDay2, iDay3
' set the width, visibility and caption of each day label
With lblDay1
.Width = iDay1
If (iDay1 / cHourW) > 7 Then
.Caption = Format(madDisplay(1), "Long Date")
Else
.Caption = Format(madDisplay(1), "Short Date")
End If
End With
 
If iDay2 > 0 Then
With lblDay2
.Left = lblDay1.Left + iDay1
.Width = iDay2
.Visible = True
If iDay2 / cHourW > 7 Then
.Caption = Format(madDisplay(iDay1 / cHourW + 1), "Long Date")
Else
.Caption = Format(madDisplay(iDay1 / cHourW + 1), "Short Date")
End If
End With
End If
 
If iDay3 > 0 Then
With lblDay3
.Left = lblDay2.Left + iDay2
.Width = iDay3
.Visible = True
.Caption = Format(madDisplay((iDay1 + iDay2) / cHourW + 1), "Short Date")
End With
End If
 
End Sub
 
Private Sub DayWidth(ByRef riFirst, _
ByRef riSecond, _
ByRef riThird)
' SETS the passed in variables to the width (in twips)
' that each day label should be
 
Dim iHour As Integer, iMinute As Integer
Dim iIncr As Integer
Dim iOne As Integer, iTwo As Integer, iThree As Integer
 
iHour = Hour(madDisplay(1))
iMinute = Minute(madDisplay(1))
iIncr = grpHour
 
If iIncr = 1 Then ' we're set to hours
iOne = 24 - iHour
If cNumImgPerRow - iOne >= 24 Then iTwo = 24 Else iTwo = cNumImgPerRow - iOne
If (iOne + iTwo) = cNumImgPerRow Then
iThree = 0
Else
iThree = cNumImgPerRow - (iOne + iTwo)
End If
ElseIf iIncr = 2 Then ' we're set to half-hours
iThree = 0
If iMinute = 0 Then
iOne = 48 - (iHour * 2)
Else
iOne = (48 - ((iHour + 1) * 2)) + 1
End If
If iOne > cNumImgPerRow Then iOne = cNumImgPerRow
If iOne < cNumImgPerRow Then iTwo = cNumImgPerRow - iOne
Else ' set to quarter-hours
iTwo = 0
iThree = 0
If iMinute = 0 Then
iOne = 96 - (iHour * 4)
ElseIf iMinute = 15 Then
iOne = (96 - ((iHour + 1) * 4)) + 3
ElseIf iMinute = 30 Then
iOne = (96 - ((iHour + 1) * 4)) + 2
Else
iOne = (96 - ((iHour + 1) * 4)) + 1
End If
If iOne > cNumImgPerRow Then iOne = cNumImgPerRow
If iOne < cNumImgPerRow Then iTwo = cNumImgPerRow - iOne
End If
 
riFirst = iOne * cHourW
riSecond = iTwo * cHourW
riThird = iThree * cHourW
 
End Sub
 
 
Private Sub NumberCal()
' numbers the calendar toggles
 
Dim x As Integer
Dim tgl As ToggleButton, s As String
 
For x = 1 To cNumImgPerRow
s = "tglHour" & Format(x, "00")
Set tgl = Me.Controls(s)
With tgl
' fill in the day numbers/letters
.Caption = Hour(madDisplay(x)) & vbCrLf & Minute(madDisplay(x))
' depress the button if it matches a selected date
.Value = ((madDisplay(x) = mdSelFrom) Or (madDisplay(x) = mdSelThru))
End With
Next x
 
End Sub
 
Private Sub FillDateArray(ByVal dStart As Date)
' Fills an array with all the dates to be displayed on the grid
Dim x As Integer, d As Date
 
Select Case grpHour
Case 1
For x = 1 To cNumImgPerRow
madDisplay(x) = DateAdd("h", x - 1, dStart)
Next x
Case 2
For x = 1 To cNumImgPerRow
madDisplay(x) = DateAdd("n", (x - 1) * 30, dStart)
Next x
Case 3
For x = 1 To cNumImgPerRow
madDisplay(x) = DateAdd("n", (x - 1) * 15, dStart)
Next x
End Select
 
' clean date array values
' without this operation date values sometimes won't eval as
' equal even though they are the same date/time
' we're not sure why this happens, but the following code loop fixes it
For x = 1 To cNumImgPerRow
d = madDisplay(x)
d = DateSerial(Year(d), Month(d), Day(d)) + _
TimeSerial(Hour(d), Minute(d), 0)
madDisplay(x) = d
Next x
 
End Sub
 
Last edited:
Code:
Private Sub FillDateArray(ByVal dStart As Date)
' Fills an array with all the dates to be displayed on the grid
Dim x As Integer, d As Date
 
Select Case grpHour
Case 1
For x = 1 To cNumImgPerRow
madDisplay(x) = DateAdd("h", x - 1, dStart)
Next x
Case 2
For x = 1 To cNumImgPerRow
madDisplay(x) = DateAdd("n", (x - 1) * 30, dStart)
Next x
Case 3
For x = 1 To cNumImgPerRow
madDisplay(x) = DateAdd("n", (x - 1) * 15, dStart)
Next x
End Select
 
' clean date array values
' without this operation date values sometimes won't eval as
' equal even though they are the same date/time
' we're not sure why this happens, but the following code loop fixes it
For x = 1 To cNumImgPerRow
d = madDisplay(x)
d = DateSerial(Year(d), Month(d), Day(d)) + _
TimeSerial(Hour(d), Minute(d), 0)
madDisplay(x) = d
Next x
 
End Sub
 
Private Sub ToggleHour(iToggle As Integer)
' This routine is called from the AfterUpdate events of the
' 32 day toggles. It determines which previously selected day toggle
' to cancel if one or two dates were already selected. Then it
' sets the appropriate selected date to that which matches the new toggle.
' The rule is to de-select the day closest to the one just clicked, as
' a best guess at what the user would want.
Dim s As String, tgl As ToggleButton
Dim x As Integer
Dim iFirstDif As Integer, iLastDif As Integer
Dim dTgl As Date
 
dTgl = madDisplay(iToggle)
 
s = "tglHour" & Format(iToggle, "00")
Set tgl = Me.Controls(s)
 
If tgl Then
' the if statement below handles the case where we have nulls in the
' selected dates
If mdSelFrom = cBlankDate And mdSelThru = cBlankDate Then
' the first toggle was just selected, so set both dates to that
mdSelFrom = dTgl
mdSelThru = mdSelFrom
GoTo DisplayLine
ElseIf mdSelThru = mdSelFrom Then
' just selected a second date
If dTgl > mdSelFrom Then
mdSelThru = dTgl
Else
mdSelThru = mdSelFrom
mdSelFrom = dTgl
End If
GoTo DisplayLine
End If
 
' deselect all the date toggles
For x = 1 To cNumImgPerRow
Me.Controls("tglHour" & Format(x, "00")) = False
Next x
 
' now determine the dates to display
If dTgl > mdSelThru Then
' new is after the last, so replace last
mdSelThru = dTgl
ElseIf dTgl < mdSelFrom Then
' new is before the first, so replace first
mdSelFrom = dTgl
Else
' new is between the first and last, so figure it out
iFirstDif = iToggle - ToggleNumFromDate(mdSelFrom)
iLastDif = ToggleNumFromDate(mdSelThru) - iToggle
If iFirstDif >= iLastDif Then
' keeps first date
mdSelThru = dTgl
Else
mdSelFrom = dTgl
End If
End If
' select the controls
If mdSelFrom >= madDisplay(1) And mdSelFrom <= madDisplay(cNumImgPerRow) Then
Me.Controls("tglHour" & Format(ToggleNumFromDate(mdSelFrom), "00")) = True
End If
If mdSelThru >= madDisplay(1) And mdSelThru <= madDisplay(cNumImgPerRow) Then
Me.Controls("tglHour" & Format(ToggleNumFromDate(mdSelThru), "00")) = True
End If
Else
' deselect the date just unpressed
If dTgl = mdSelFrom Then
If mdSelFrom = mdSelThru Then
mdSelFrom = cBlankDate
mdSelThru = cBlankDate
Else
mdSelFrom = mdSelThru
End If
Else
mdSelThru = mdSelFrom
End If
End If
 
DisplayLine:
' display the selected dates in lblSelect
If mdSelFrom = cBlankDate Then
s = " "
ElseIf mdSelThru = mdSelFrom Then
s = "Selected: " & UCase(Format(mdSelFrom, "dddd, m/d/yyyy, h:nn AM/PM"))
Else
s = "Selected: " & UCase(Format(mdSelFrom, "dddd, m/d/yyyy, h:nn AM/PM")) & _
" thru " & UCase(Format(mdSelThru, "dddd, m/d/yyyy, h:nn AM/PM"))
End If
 
lblSelect.Caption = s
 
DisplaySelectedGrid
 
ExitLine:
Exit Sub
ErrLine:
Resume ExitLine
End Sub
 
Private Function SetPic(iImg As Integer, _
Optional sColor As String = "", _
Optional sArrow As String = "D") As Boolean
' Changes the picture in the designated image box to one of those
' the arrow type and color designated. Also sets data in the Tag
' property of the target image box.
' Parameters:
' iImg: the number of the image control to update
' (Image controls on the grid are named img001 to img320.)
' sColor: B = Blue R = Red G = Green
' P = Purple Y = Yellow
' "" = clears the image control
' sArrow: D = diamond L = left B = bar R = right
' Returns: True if successful, False on Error
 
On Error GoTo ErrLine
 
Dim imgS As Image ' Source image box (invisible) holding the icon
Dim imgT As Image ' Target image box we are changing
 
' set the target
Set imgT = Me.Controls("img" & Format(iImg, "000"))
 
If sColor = "" Then
' clear the target imgbox
With imgT
.Picture = ""
.Tag = ""
End With
Else ' set the image on the target
Set imgS = Me.Controls("img" & sColor & sArrow)
With imgT
.PictureData = imgS.PictureData
.Tag = sColor & sArrow
End With
End If
 
SetPic = True
 
ExitLine:
Exit Function
ErrLine:
SetPic = False
Resume ExitLine
End Function
 
Private Function EnumUnits() As String
' Returns a string listing all the units currently displayed.
' For use in a SQL statement WHERE clause.
' Example of return value: "tblUnit.UnitID = 01 OR tblUnit.UnitID = 05 OR tblUnit.UnitID = 10 "
' if the units 01, 05, and 10 were displayed
 
Dim sReturn As String
Dim vID As Variant
Dim x As Integer
 
sReturn = ""
 
For x = 1 To 10
vID = Controls("tglUnit" & Format(x, "00")).Tag
If vID <> "" Then
If cUnitPKDataType = "Numeric" Then
sReturn = sReturn & " OR " & cUnitPK & " = " & vID
Else
sReturn = sReturn & " OR " & cUnitPK & " = '" & vID & "'"
End If
End If
Next x
 
sReturn = Right(sReturn, Len(sReturn) - 4)
 
EnumUnits = sReturn
 
End Function
 
Private Function DaysInMonth(iMonth As Integer, ByVal iYear As Integer) As Integer
' returns the number of days in a month
' Parameters: iMonth (1-12); iYear, 4-digit (eg. 1995, needed for leap years)
Dim d As Date
' Last day of month
d = DateSerial(iYear, iMonth + 1, 0)
DaysInMonth = Day(d)
End Function
 
' Toggle Events
Private Sub tglHour01_AfterUpdate()
ToggleHour 1
End Sub
Private Sub tglHour02_AfterUpdate()
ToggleHour 2
End Sub
Private Sub tglHour03_AfterUpdate()
ToggleHour 3
End Sub
Private Sub tglHour04_AfterUpdate()
ToggleHour 4
End Sub
Private Sub tglHour05_AfterUpdate()
ToggleHour 5
End Sub
Private Sub tglHour06_AfterUpdate()
ToggleHour 6
End Sub
Private Sub tglHour07_AfterUpdate()
ToggleHour 7
End Sub
Private Sub tglHour08_AfterUpdate()
ToggleHour 8
End Sub
Private Sub tglHour09_AfterUpdate()
ToggleHour 9
End Sub
Private Sub tglHour10_AfterUpdate()
ToggleHour 10
End Sub
Private Sub tglHour11_AfterUpdate()
ToggleHour 11
End Sub
Private Sub tglHour12_AfterUpdate()
ToggleHour 12
End Sub
Private Sub tglHour13_AfterUpdate()
ToggleHour 13
End Sub
Private Sub tglHour14_AfterUpdate()
ToggleHour 14
End Sub
Private Sub tglHour15_AfterUpdate()
ToggleHour 15
End Sub
Private Sub tglHour16_AfterUpdate()
ToggleHour 16
End Sub
Private Sub tglHour17_AfterUpdate()
ToggleHour 17
End Sub
Private Sub tglHour18_AfterUpdate()
ToggleHour 18
End Sub
Private Sub tglHour19_AfterUpdate()
ToggleHour 19
End Sub
Private Sub tglHour20_AfterUpdate()
ToggleHour 20
End Sub
Private Sub tglHour21_AfterUpdate()
ToggleHour 21
End Sub
Private Sub tglHour22_AfterUpdate()
ToggleHour 22
End Sub
Private Sub tglHour23_AfterUpdate()
ToggleHour 23
End Sub
Private Sub tglHour24_AfterUpdate()
ToggleHour 24
End Sub
Private Sub tglHour25_AfterUpdate()
ToggleHour 25
End Sub
Private Sub tglHour26_AfterUpdate()
ToggleHour 26
End Sub
Private Sub tglHour27_AfterUpdate()
ToggleHour 27
End Sub
Private Sub tglHour28_AfterUpdate()
ToggleHour 28
End Sub
Private Sub tglHour29_AfterUpdate()
ToggleHour 29
End Sub
Private Sub tglHour30_AfterUpdate()
ToggleHour 30
End Sub
Private Sub tglHour31_AfterUpdate()
ToggleHour 31
End Sub
Private Sub tglHour32_AfterUpdate()
ToggleHour 32
End Sub
Private Sub tglUnit01_AfterUpdate()
ToggleUnit 1
End Sub
Private Sub tglUnit02_AfterUpdate()
ToggleUnit 2
End Sub
Private Sub tglUnit03_AfterUpdate()
ToggleUnit 3
End Sub
Private Sub tglUnit04_AfterUpdate()
ToggleUnit 4
End Sub
Private Sub tglUnit05_AfterUpdate()
ToggleUnit 5
End Sub
Private Sub tglUnit06_AfterUpdate()
ToggleUnit 6
End Sub
Private Sub tglUnit07_AfterUpdate()
ToggleUnit 7
End Sub
Private Sub tglUnit08_AfterUpdate()
ToggleUnit 8
End Sub
Private Sub tglUnit09_AfterUpdate()
ToggleUnit 9
End Sub
Private Sub tglUnit10_AfterUpdate()
ToggleUnit 10
End Sub
 
Private Sub cmdNextHour_Click()
Dim iMin As Integer
Select Case grpHour
Case 1: iMin = 60
Case 2: iMin = 30
Case 3: iMin = 15
End Select
SetupCal DateAdd("n", iMin, madDisplay(1))
End Sub
Private Sub cmdNextDay_Click()
SetupCal madDisplay(1) + 1
End Sub
Private Sub CmdNextYear_Click()
SetupCal DateAdd("yyyy", 1, madDisplay(1))
End Sub
Private Sub cmdPrevHour_Click()
Dim iMin As Integer
Select Case grpHour
Case 1: iMin = -60
Case 2: iMin = -30
Case 3: iMin = -15
End Select
SetupCal DateAdd("n", iMin, madDisplay(1))
End Sub
Private Sub CmdPreviousYear_Click()
SetupCal DateAdd("yyyy", -1, madDisplay(1))
End Sub
Private Sub cmdPrevDay_Click()
SetupCal madDisplay(1) - 1
End Sub
Private Sub cmdNow_Click()
SetupCal Date + TimeSerial(Hour(Now), 0, 0)
End Sub
Private Sub cmdYear_Click()
SetupCal DateSerial(Year(Date), Month(madDisplay(1)), Day(madDisplay(1)))
End Sub
 
Last edited:
How about enclosing that code in code tags so it is actually readable?
 
Code:
Option Compare Database
Option Explicit
 
' data access objects needed
Dim mdbT As DAO.Database ' Target DB w/ unit & period tbls, default CurrentDb
Dim mrsU As DAO.Recordset ' Unit recordset
Dim mrsP As DAO.Recordset ' Period recordset
 
' Constants to identify the tables/queries
' Replace these with the names specific to
' your own application (see user manual).
' Note that the UnitRS can be either a table name,
' or saved query, or a SQL statement.
' Both recordsets must include the Primary Key field.
' The unit set must also include a display field, which
' may or may not be the same as the primary key.
' The period set must include the Primary Key field and
' both the FromDate field and the ThruDate field.
Const cUnitRS As String = "SELECT * FROM tblUnit ORDER BY Unit;"
' You must use a table or query name for the period,
' since a SQL statement that limits the period recordset
' to the currently displayed date range is created at run time.
Const cPeriodRS As String = "tblHourPeriod"
 
' constants for the names of the primary key fields
Const cUnitPK As String = "UnitID"
Const cPeriodPK As String = "PeriodID"
' constants for the field to display from the unit set
' note that this can be the same as the primary key
' but can be another field as well (as in the default)
Const cUnitDisplayFld As String = "Unit"
 
' constants for the data type of the PK fields
' DesGrid only supports single field primary keys
' of Number or Text data types. If your primary
' key fields are text fields, use the value "String",
' otherwise leave them as "Numeric".
Const cUnitPKDataType As String = "Numeric"
Const cPeriodPKDataType As String = "Numeric"
 
' constants for the names of the date fields used
' in the period table. These must be of data type
' Date/Time and there must be two of them:
' one for the FromDate (starting date) and
' one for the ThruDate (ending date)
Const cFromFld As String = "FromDate"
Const cThruFld As String = "ThruDate"
Const cColorKeyFld As String = "ColorKey"
 
' constant for the name of the color key table
Const cColorTbl As String = "trefColorKey"
 
' variables to hold selected dates and units
Dim mdSelFrom As Date
Dim mdSelThru As Date
Dim mcolSelUnits As New Collection
 
' array for currently displayed dates
Dim madDisplay(1 To 32) As Date
' constant for special effect for selected image boxes
Const dsRaised As Long = 1
Const dsSunken As Long = 2
Const dsEtched As Long = 3
 
' stores true if the grid is clear
' we check this before running code to
' clear the grid (speeds scrolling)
Dim mfGridClear As Boolean
 
' width (in twips) of a day toggle
Const cHourW As Integer = 288
' number of image boxes in a row
Const cNumImgPerRow As Integer = 32
 
' dummy value for date variables
Const cBlankDate As Date = #1/1/100#
 
' variable to store state of selected grid boxes
Dim mfSelectedGrid As Boolean
 
Private Sub Form_Load()
 
Dim dOpen As Date
 
' initialize
mdSelFrom = cBlankDate
mdSelThru = cBlankDate
mfSelectedGrid = False
 
' fill the recordsets
LoadData
 
' format calendar portion of the form
' if a date was passed in OpenArgs, use that, else use Date
If Not IsDate(OpenArgs) Then
dOpen = DateSerial(Year(Date), Month(Date), Day(Date)) + TimeSerial(Hour(Time), 0, 0)
SetupCal dOpen
Else
dOpen = CDate(OpenArgs)
dOpen = DateSerial(Year(dOpen), Month(dOpen), Day(dOpen)) + TimeSerial(Hour(dOpen), 0, 0)
SetupCal dOpen
End If
 
' format unit list portion of the form
FillUnits mrsU.Fields(cUnitPK)
 
' you might want to clear the grid instead
' depending on how you want the form to initialize
DrawGrid
 
' set up the color key from table data
GetColorKeyCaptions
 
End Sub
 
Public Function DrawGrid() As Boolean
' puts colored diamonds and arrows on the grid
' according to data in the period recordset
On Error GoTo ErrLine
 
Dim sqlU As String
Dim x As Integer, iCol As Integer
Dim Y As Integer, sColor As String
Dim iImgFirst As Integer, iImgLast As Integer
Dim sUnitID As String
Dim fFromOff As Boolean, fThruOff As Boolean
Dim iFirstInRow As Integer, iTimeUnit As Integer
Dim tgl As ToggleButton
 
' open a recordset of periods in the current date range
sqlU = "SELECT " & cUnitPK & ", " & cColorKeyFld & ", " & _
cFromFld & ", " & cThruFld & " FROM " & cPeriodRS & _
" WHERE (" & cFromFld & " <= #" & madDisplay(cNumImgPerRow) & "# " & _
"AND " & cThruFld & " >= #" & madDisplay(1) & "# " & _
"AND (" & EnumUnits & ")) " & _
"ORDER BY " & cUnitPK
Set mrsP = mdbT.OpenRecordset(sqlU)
 
With mrsP
' exit if no records
If (.BOF And .EOF) Then
On Error Resume Next
.Close
Set mrsP = Nothing
ClearGrid
DisplayGrid True
DisplaySelectedGrid
GoTo ExitLine
End If
' work through the records, drawing arrows/diamonds on the appropriate row
ClearGrid
.MoveFirst
Do Until .EOF ' if no period for this unit, on to next unit toggle
' find the row number that matches the current record
For x = 1 To 10
Set tgl = Me.Controls("tglUnit" & Format(x, "00"))
If tgl.Tag = .Fields(cUnitPK) Then
Exit For
End If
Next x
' check to see if the from/thru dates are off screen
fFromOff = (.Fields(cFromFld) < madDisplay(1))
fThruOff = (.Fields(cThruFld) > madDisplay(cNumImgPerRow) + 1)
' Determine which image box represents our first date
iFirstInRow = ((x - 1) * cNumImgPerRow) + 1
If fFromOff Then
iTimeUnit = 0
Else
Select Case grpHour
Case 1
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cFromFld)) / 60
Case 2
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cFromFld)) / 30
Case 3
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cFromFld)) / 15
End Select
End If
iImgFirst = iFirstInRow + iTimeUnit
' determine which image box represents our last date
If .Fields(cThruFld) > madDisplay(cNumImgPerRow) Then
iImgLast = x * cNumImgPerRow
Else
Select Case grpHour
Case 1
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cThruFld)) / 60
Case 2
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cThruFld)) / 30
Case 3
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cThruFld)) / 15
End Select
' remove the '- 1' from the following line if you want the arrow to
' go through the ending time field
iImgLast = iFirstInRow + (iTimeUnit - 1)
End If
' draw the appropriate symbol
sColor = .Fields(cColorKeyFld)
If iImgFirst >= iImgLast Then
' one-unit period, so use a diamond
SetPic iImgFirst, sColor, "D"
Else
' multi-day period, so line w/arrows
If fFromOff Then
SetPic iImgFirst, sColor, "B"
Else
SetPic iImgFirst, sColor, "L"
End If
If fThruOff Then
If (.Fields(cFromFld) = madDisplay(cNumImgPerRow)) Then
SetPic iImgLast, sColor, "L"
Else
SetPic iImgLast, sColor, "B"
End If
Else
SetPic iImgLast, sColor, "R"
End If
' now the bar in between the end points
For Y = (iImgFirst + 1) To (iImgLast - 1)
SetPic Y, sColor, "B"
Next Y
End If
.MoveNext
Loop
End With
 
DisplayGrid True
mfGridClear = False
DisplaySelectedGrid
DrawGrid = True
 
ExitLine:
Exit Function
ErrLine:
DrawGrid = False
Resume ExitLine
End Function
 
Private Function DisplaySelectedGrid() As Boolean
' Sets the special effect of selected grid image boxes
' to dsSunken. Note that this function is cosmetic, since
' there may be selected units not diplayed.
' Returns True if successful, false on error
On Error GoTo ErrLine
 
Dim iReturn As Integer
' loop counters
Dim iUnitTgl As Integer, x As Integer
' unit toggle, image box
Dim tglU As ToggleButton, imgX As Image
' image box numbers
Dim iFstImg As Integer, iLstImg As Integer
' booleans to determine displayability of arrows
Dim fFirstOff As Boolean, fLastOff As Boolean
Dim fBigWide As Boolean ' a range that spans beyond both sides of the grid
Dim iInterval As Integer
 
' make sure none are sunken
If mfSelectedGrid Then
For x = 1 To 320
Set imgX = Me.Controls("img" & Format(x, "000"))
imgX.SpecialEffect = dsEtched
Next x
End If
 
' if no date is selected or no units selected, exit
If ((mdSelFrom = cBlankDate) Or (mcolSelUnits.Count = 0)) Then
iReturn = 0
mfSelectedGrid = False
GoTo ExitLine
Else
mfSelectedGrid = True
End If
 
' check if the from/thru dates are off screen
' fFirstOff is True if the first selected date has been scrolled off screen
fFirstOff = ((mdSelFrom < madDisplay(1)) Or _
(mdSelFrom > madDisplay(cNumImgPerRow)))
' fLastOff is True if the last selected date has been scrolled off screen
fLastOff = ((mdSelThru > madDisplay(cNumImgPerRow) + 1) Or _
((mdSelThru <= madDisplay(1)) And _
(mdSelThru <> cBlankDate)))
' fBigWide is True if both the first and last dates are off screen (so just a big wide bar)
fBigWide = (mdSelFrom < madDisplay(1) And mdSelThru > madDisplay(cNumImgPerRow))
 
If (fFirstOff And fLastOff) And Not fBigWide Then
' don't draw an arrow
Else
' loop through the unit toggles, looking for selected ones
For iUnitTgl = 1 To 10
Set tglU = Me.Controls("tglUnit" & Format$(iUnitTgl, "00"))
If tglU Then
' here we figure out the first and last image boxes
Select Case grpHour
Case 1: iInterval = 60
Case 2: iInterval = 30
Case 3: iInterval = 15
End Select
' clean the date values
mdSelThru = DateSerial(Year(mdSelThru), Month(mdSelThru), Day(mdSelThru)) + _
TimeSerial(Hour(mdSelThru), Minute(mdSelThru), 0)
mdSelFrom = DateSerial(Year(mdSelFrom), Month(mdSelFrom), Day(mdSelFrom)) + _
TimeSerial(Hour(mdSelFrom), Minute(mdSelFrom), 0)
If mdSelThru <= DateAdd("n", iInterval, mdSelFrom) Then
' diamond
iFstImg = ((iUnitTgl - 1) * cNumImgPerRow) + ToggleNumFromDate(mdSelFrom)
Set imgX = Me.Controls("img" & Format(iFstImg, "000"))
imgX.SpecialEffect = dsSunken
Else
' arrow
If fFirstOff Then
iFstImg = ((iUnitTgl - 1) * cNumImgPerRow) + 1
Else
iFstImg = ((iUnitTgl - 1) * cNumImgPerRow) + ToggleNumFromDate(mdSelFrom)
End If
If fLastOff Then
iLstImg = iUnitTgl * cNumImgPerRow
Else
iLstImg = ((iUnitTgl - 1) * cNumImgPerRow) + ToggleNumFromDate(mdSelThru)
End If
iLstImg = iLstImg - 1
For x = iFstImg To iLstImg
Set imgX = Me.Controls("img" & Format(x, "000"))
imgX.SpecialEffect = dsSunken
Next x
End If
End If
Next iUnitTgl
End If
 
mfGridClear = False
DisplaySelectedGrid = True
 
ExitLine:
Exit Function
ErrLine:
DisplaySelectedGrid = False
Resume ExitLine
End Function
[quote]
Option Compare Database
Option Explicit
 
' data access objects needed
Dim mdbT As DAO.Database ' Target DB w/ unit & period tbls, default CurrentDb
Dim mrsU As DAO.Recordset ' Unit recordset
Dim mrsP As DAO.Recordset ' Period recordset
 
' Constants to identify the tables/queries
' Replace these with the names specific to
' your own application (see user manual).
' Note that the UnitRS can be either a table name,
' or saved query, or a SQL statement.
' Both recordsets must include the Primary Key field.
' The unit set must also include a display field, which
' may or may not be the same as the primary key.
' The period set must include the Primary Key field and
' both the FromDate field and the ThruDate field.
Const cUnitRS As String = "SELECT * FROM tblUnit ORDER BY Unit;"
' You must use a table or query name for the period,
' since a SQL statement that limits the period recordset
' to the currently displayed date range is created at run time.
Const cPeriodRS As String = "tblHourPeriod"
 
' constants for the names of the primary key fields
Const cUnitPK As String = "UnitID"
Const cPeriodPK As String = "PeriodID"
' constants for the field to display from the unit set
' note that this can be the same as the primary key
' but can be another field as well (as in the default)
Const cUnitDisplayFld As String = "Unit"
 
' constants for the data type of the PK fields
' DesGrid only supports single field primary keys
' of Number or Text data types. If your primary
' key fields are text fields, use the value "String",
' otherwise leave them as "Numeric".
Const cUnitPKDataType As String = "Numeric"
Const cPeriodPKDataType As String = "Numeric"
 
' constants for the names of the date fields used
' in the period table. These must be of data type
' Date/Time and there must be two of them:
' one for the FromDate (starting date) and
' one for the ThruDate (ending date)
Const cFromFld As String = "FromDate"
Const cThruFld As String = "ThruDate"
Const cColorKeyFld As String = "ColorKey"
 
' constant for the name of the color key table
Const cColorTbl As String = "trefColorKey"
 
' variables to hold selected dates and units
Dim mdSelFrom As Date
Dim mdSelThru As Date
Dim mcolSelUnits As New Collection
 
' array for currently displayed dates
Dim madDisplay(1 To 32) As Date
' constant for special effect for selected image boxes
Const dsRaised As Long = 1
Const dsSunken As Long = 2
Const dsEtched As Long = 3
 
' stores true if the grid is clear
' we check this before running code to
' clear the grid (speeds scrolling)
Dim mfGridClear As Boolean
 
' width (in twips) of a day toggle
Const cHourW As Integer = 288
' number of image boxes in a row
Const cNumImgPerRow As Integer = 32
 
' dummy value for date variables
Const cBlankDate As Date = #1/1/100#
 
' variable to store state of selected grid boxes
Dim mfSelectedGrid As Boolean
 
Private Sub Form_Load()
 
Dim dOpen As Date
 
' initialize
mdSelFrom = cBlankDate
mdSelThru = cBlankDate
mfSelectedGrid = False
 
' fill the recordsets
LoadData
 
' format calendar portion of the form
' if a date was passed in OpenArgs, use that, else use Date
If Not IsDate(OpenArgs) Then
dOpen = DateSerial(Year(Date), Month(Date), Day(Date)) + TimeSerial(Hour(Time), 0, 0)
SetupCal dOpen
Else
dOpen = CDate(OpenArgs)
dOpen = DateSerial(Year(dOpen), Month(dOpen), Day(dOpen)) + TimeSerial(Hour(dOpen), 0, 0)
SetupCal dOpen
End If
 
' format unit list portion of the form
FillUnits mrsU.Fields(cUnitPK)
 
' you might want to clear the grid instead
' depending on how you want the form to initialize
DrawGrid
 
' set up the color key from table data
GetColorKeyCaptions
 
End Sub
 
Public Function DrawGrid() As Boolean
' puts colored diamonds and arrows on the grid
' according to data in the period recordset
On Error GoTo ErrLine
 
Dim sqlU As String
Dim x As Integer, iCol As Integer
Dim Y As Integer, sColor As String
Dim iImgFirst As Integer, iImgLast As Integer
Dim sUnitID As String
Dim fFromOff As Boolean, fThruOff As Boolean
Dim iFirstInRow As Integer, iTimeUnit As Integer
Dim tgl As ToggleButton
 
' open a recordset of periods in the current date range
sqlU = "SELECT " & cUnitPK & ", " & cColorKeyFld & ", " & _
cFromFld & ", " & cThruFld & " FROM " & cPeriodRS & _
" WHERE (" & cFromFld & " <= #" & madDisplay(cNumImgPerRow) & "# " & _
"AND " & cThruFld & " >= #" & madDisplay(1) & "# " & _
"AND (" & EnumUnits & ")) " & _
"ORDER BY " & cUnitPK
Set mrsP = mdbT.OpenRecordset(sqlU)
 
With mrsP
' exit if no records
If (.BOF And .EOF) Then
On Error Resume Next
.Close
Set mrsP = Nothing
ClearGrid
DisplayGrid True
DisplaySelectedGrid
GoTo ExitLine
End If
' work through the records, drawing arrows/diamonds on the appropriate row
ClearGrid
.MoveFirst
Do Until .EOF ' if no period for this unit, on to next unit toggle
' find the row number that matches the current record
For x = 1 To 10
Set tgl = Me.Controls("tglUnit" & Format(x, "00"))
If tgl.Tag = .Fields(cUnitPK) Then
Exit For
End If
Next x
' check to see if the from/thru dates are off screen
fFromOff = (.Fields(cFromFld) < madDisplay(1))
fThruOff = (.Fields(cThruFld) > madDisplay(cNumImgPerRow) + 1)
' Determine which image box represents our first date
iFirstInRow = ((x - 1) * cNumImgPerRow) + 1
If fFromOff Then
iTimeUnit = 0
Else
Select Case grpHour
Case 1
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cFromFld)) / 60
Case 2
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cFromFld)) / 30
Case 3
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cFromFld)) / 15
End Select
End If
iImgFirst = iFirstInRow + iTimeUnit
' determine which image box represents our last date
If .Fields(cThruFld) > madDisplay(cNumImgPerRow) Then
iImgLast = x * cNumImgPerRow
Else
Select Case grpHour
Case 1
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cThruFld)) / 60
Case 2
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cThruFld)) / 30
Case 3
iTimeUnit = DateDiff("n", madDisplay(1), .Fields(cThruFld)) / 15
End Select
' remove the '- 1' from the following line if you want the arrow to
' go through the ending time field
iImgLast = iFirstInRow + (iTimeUnit - 1)
End If
' draw the appropriate symbol
sColor = .Fields(cColorKeyFld)
If iImgFirst >= iImgLast Then
' one-unit period, so use a diamond
SetPic iImgFirst, sColor, "D"
Else
' multi-day period, so line w/arrows
If fFromOff Then
SetPic iImgFirst, sColor, "B"
Else
SetPic iImgFirst, sColor, "L"
End If
If fThruOff Then
If (.Fields(cFromFld) = madDisplay(cNumImgPerRow)) Then
SetPic iImgLast, sColor, "L"
Else
SetPic iImgLast, sColor, "B"
End If
Else
SetPic iImgLast, sColor, "R"
End If
' now the bar in between the end points
For Y = (iImgFirst + 1) To (iImgLast - 1)
SetPic Y, sColor, "B"
Next Y
End If
.MoveNext
Loop
End With
 
DisplayGrid True
mfGridClear = False
DisplaySelectedGrid
DrawGrid = True
 
ExitLine:
Exit Function
ErrLine:
DrawGrid = False
Resume ExitLine
End Function
 
Private Function DisplaySelectedGrid() As Boolean
' Sets the special effect of selected grid image boxes
' to dsSunken. Note that this function is cosmetic, since
' there may be selected units not diplayed.
' Returns True if successful, false on error
On Error GoTo ErrLine
 
Dim iReturn As Integer
' loop counters
Dim iUnitTgl As Integer, x As Integer
' unit toggle, image box
Dim tglU As ToggleButton, imgX As Image
' image box numbers
Dim iFstImg As Integer, iLstImg As Integer
' booleans to determine displayability of arrows
Dim fFirstOff As Boolean, fLastOff As Boolean
Dim fBigWide As Boolean ' a range that spans beyond both sides of the grid
Dim iInterval As Integer
 
' make sure none are sunken
If mfSelectedGrid Then
For x = 1 To 320
Set imgX = Me.Controls("img" & Format(x, "000"))
imgX.SpecialEffect = dsEtched
Next x
End If
 
' if no date is selected or no units selected, exit
If ((mdSelFrom = cBlankDate) Or (mcolSelUnits.Count = 0)) Then
iReturn = 0
mfSelectedGrid = False
GoTo ExitLine
Else
mfSelectedGrid = True
End If
 
' check if the from/thru dates are off screen
' fFirstOff is True if the first selected date has been scrolled off screen
fFirstOff = ((mdSelFrom < madDisplay(1)) Or _
(mdSelFrom > madDisplay(cNumImgPerRow)))
' fLastOff is True if the last selected date has been scrolled off screen
fLastOff = ((mdSelThru > madDisplay(cNumImgPerRow) + 1) Or _
((mdSelThru <= madDisplay(1)) And _
(mdSelThru <> cBlankDate)))
' fBigWide is True if both the first and last dates are off screen (so just a big wide bar)
fBigWide = (mdSelFrom < madDisplay(1) And mdSelThru > madDisplay(cNumImgPerRow))
 
If (fFirstOff And fLastOff) And Not fBigWide Then
' don't draw an arrow
Else
' loop through the unit toggles, looking for selected ones
For iUnitTgl = 1 To 10
Set tglU = Me.Controls("tglUnit" & Format$(iUnitTgl, "00"))
If tglU Then
' here we figure out the first and last image boxes
Select Case grpHour
Case 1: iInterval = 60
Case 2: iInterval = 30
Case 3: iInterval = 15
End Select
' clean the date values
mdSelThru = DateSerial(Year(mdSelThru), Month(mdSelThru), Day(mdSelThru)) + _
TimeSerial(Hour(mdSelThru), Minute(mdSelThru), 0)
mdSelFrom = DateSerial(Year(mdSelFrom), Month(mdSelFrom), Day(mdSelFrom)) + _
TimeSerial(Hour(mdSelFrom), Minute(mdSelFrom), 0)
If mdSelThru <= DateAdd("n", iInterval, mdSelFrom) Then
' diamond
iFstImg = ((iUnitTgl - 1) * cNumImgPerRow) + ToggleNumFromDate(mdSelFrom)
Set imgX = Me.Controls("img" & Format(iFstImg, "000"))
imgX.SpecialEffect = dsSunken
Else
' arrow
If fFirstOff Then
iFstImg = ((iUnitTgl - 1) * cNumImgPerRow) + 1
Else
iFstImg = ((iUnitTgl - 1) * cNumImgPerRow) + ToggleNumFromDate(mdSelFrom)
End If
If fLastOff Then
iLstImg = iUnitTgl * cNumImgPerRow
Else
iLstImg = ((iUnitTgl - 1) * cNumImgPerRow) + ToggleNumFromDate(mdSelThru)
End If
iLstImg = iLstImg - 1
For x = iFstImg To iLstImg
Set imgX = Me.Controls("img" & Format(x, "000"))
imgX.SpecialEffect = dsSunken
Next x
End If
End If
Next iUnitTgl
End If
 
mfGridClear = False
DisplaySelectedGrid = True
 
ExitLine:
Exit Function
ErrLine:
DisplaySelectedGrid = False
Resume ExitLine
End Function
 
Last edited:
Don't know realy how to do it, btw the code is that long i have to split it. in 4 parts
 
Don't know realy how to do it, btw the code is that long i have to split it. in 4 parts

All you have to do is to go back to your original post, click the EDIT button and then go select all of the code in that post and click this button:
codetag01.png


It will then put code tags around it and make it easier to read (provided your code in its native format has indentations and all to make it read easier to begin with).
 
That's too much code... Just attach a db ;)
 
After 2 weeks of searching and trying i dont see the sun goes up anymore:eek:
 
Besides the earlier issue i entered a calendar subform to link to Planning grid, is there a way to let comunicate the Calendar subform with the grid

Like the commands, now, date click, year click, etc.. i want to navigate true the grid with a subform calendar.:(
 

Users who are viewing this thread

Back
Top Bottom