I have set up an access system which will take bookings for a company. The user ideally wants to have it in a calendar style (like Outlook), so I am trying to implement a FlexGrid. I was given a sample code by a friend of mine (not sure if he wrote it being honest), and Ive tried to import this to my program in order to use it as a basis for a Flexgrid of my own. However I keep getting errors. Im basically now looking for help on how to implement a flexgrid as quickly as possible
The current form for inputting bookings is very basic. Code as follows
==========================
==========
Option Compare Database
Private Sub ChckBkngs_Click()
On Error GoTo Err_ChckBkngs_Click
Dim stDocName As String
stDocName = "Booking_Data Query"
DoCmd.OpenReport stDocName, acPreview
Exit_ChckBkngs_Click:
Exit Sub
Err_ChckBkngs_Click:
MsgBox Err.Description
Resume Exit_ChckBkngs_Click
End Sub
Private Sub pricechck_Click()
On Error GoTo Err_pricechck_Click
Dim stDocName As String
stDocName = "Treatments_Data Query"
DoCmd.OpenReport stDocName, acPreview
Exit_pricechck_Click:
Exit Sub
Err_pricechck_Click:
MsgBox Err.Description
Resume Exit_pricechck_Click
End Sub
==========================
==========
The Flex Grid code is next below. Now I have never used a flex grid before so I really could use the help here. Ive attached a jpg of my current form & of the flexgrid I would ideally love to get.
==========================
==========
Option Compare Database
Option Explicit
Const conCream As Long = 13303807
Const conNavy As Long = 8404992
Const conPaleGreen As Long = 13434828
Const conLightBlue As Long = 16760445
Private Sub btnClose_Click()
DoCmd.Close
End Sub
Private Sub Form_Activate()
'Re-display FlexGrids when form Activated
FillCalendarGrid 'display Calendar FlexGrid
ShowAppointments 'display appointments FlexGrid
End Sub
Private Sub Form_Open(Cancel As Integer)
txtMonth = Month(Date) 'set Month to this month
txtYear = Year(Date) 'set Year to this year
txtToday = Date 'set today's date
End Sub
Private Sub fraMode_AfterUpdate()
ShowAppointments 'update appts
End Sub
Public Sub ShowDay()
'Shows flex grid as current day
'Entry (txtToday) = Date to be displayed
' (tblDiary) holds appointments
Dim vRow As Long, vRow2 As Long
Dim vTime As Date
Dim rst As Recordset
On Error GoTo ErrorCode
flxDates.Redraw = False 'switch off Redraw first
flxDates.Clear 'clear all cells
flxDates.Cols = 2 'set No of cols = 2
flxDates.FixedRows = 1 'set No of fixed Rows = 1
flxDates.FixedCols = 1 'set No of fixed Columns = 1
flxDates.ScrollBars = 0 'set No scroll bars reqd
flxDates.MergeCol(1) = True 'set Merge on Col 1
flxDates.ColWidth(0) = 800 'Width for Col 0 = 800 twips
flxDates.ColWidth(1) = 10300 'Width for Col 1 = 10300 twips
flxDates.RowHeight(-1) = 278 'Height of all rows = 278 twips
flxDates.ColAlignment(0) = flexAlignCenterCenter 'align Col 0 CenterCenter
flxDates.ColAlignment(1) = flexAlignLeftCenter 'align Col 1 LeftCenter
SelectRange flxDates, 0, 0, 0, 1 'align Row 0 as
flxDates.CellAlignment = flexAlignCenterCenter 'CenterCenter
SelectRange flxDates, 0, 0, 0, 1 'paint header row only
flxDates.CellBackColor = conNavy 'in white on navy blue
flxDates.CellForeColor = vbWhite
vTime = "09:00:00" 'set work start time as 0900 hours
vRow = ((Hour(vTime) - 7) * 2) 'calc row No for start time
If Minute(vTime) >= 30 Then vRow = vRow + 1
If vRow > 0 Then 'skip if < 07:00
SelectRange flxDates, 1, 0, vRow, 1 'paint first rows in cream (0700-0830)
flxDates.CellBackColor = conCream
End If
vTime = "17:00:00" 'set work end time as 1700 hours
vRow = ((Hour(vTime) - 7) * 2) + 1 'calc row No for start time
If Minute(vTime) >= 30 Then vRow = vRow + 1
If vRow < 29 Then
SelectRange flxDates, vRow, 0, 28, 1 'paint last rows in cream (1700-2030)
flxDates.CellBackColor = conCream
End If
flxDates.TextMatrix(0, 0) = "Time" 'show header text
flxDates.TextMatrix(0, 1) = "Diary appointments for " & Format(txtToday, "dddd d mmmm yyyy")
vTime = "07:00:00" 'show times for each appointment line (30 min intervals)
For vRow = 1 To 28
flxDates.TextMatrix(vRow, 0) = Format(vTime, "hh:nn")
vTime = DateAdd("n", 30, vTime)
Next
For vRow = 1 To 28 Step 2 'set whole hour rows in bold font
flxDates.Row = vRow
flxDates.Col = 0
flxDates.CellFontBold = True
Next
'Copy appointments from tblDiary to grid
Set rst = CurrentDb.OpenRecordset("S
ELECT * FROM tblDiary WHERE StartDate = #" _
& Format(txtToday, "m-d-yyyy") & "#") 'make list of appointments for selected date from tblDiary
Do Until rst.EOF 'then copy them to appropriate rows
vRow = ((Hour(rst!StartTime) - 7) * 2) + 1 'calc row No for start time
If Minute(rst!StartTime) >= 30 Then vRow = vRow + 1 'add 1 to row if minutes part >= 30
vRow2 = (DateDiff("n", rst!StartTime, rst!EndTime)) / 30 'calc difference between start time and end time
flxDates.TextMatrix(vRow, 1) = Nz(rst!Notes) 'show Notes field
If vRow2 > 1 Then 'if appt is over 30 mins then
Do While vRow2 > 1 'add extra rows for remaining time
vRow = vRow + 1 'next row
flxDates.TextMatrix(vRow, 1) = Nz(rst!Notes) 'copy Notes to field to allow merge option
vRow2 = vRow2 - 1 'dec counter
Loop
End If
rst.MoveNext 'next appointment
Loop
rst.Close
Set rst = Nothing
flxDates.Redraw = True 're-display grid
Exit Sub
ErrorCode:
If Not rst Is Nothing Then rst.Close
Set rst = Nothing
Beep
MsgBox Err.Description
End Sub
Public Sub ShowWeek()
'Shows flex grid as current day
'Entry (txtToday) = Date to be displayed
' (tblDiary) holds appointments
Dim vRow As Long
Dim vDate As Date, vStartDate As Date
Dim rst As Recordset
Dim vNotes As String, vDates As String
On Error GoTo ErrorCode
flxDates.Redraw = False 'switch off Redraw first
flxDates.Clear 'clear all cells
flxDates.Cols = 2 'No of cols = 2
flxDates.FixedRows = 1 'No of fixed rows = 1
flxDates.FixedCols = 1 'No of fixed columns = 1
flxDates.ScrollBars = 0 'No scroll bars reqd
flxDates.ColWidth(0) = 800 'Col 0 width = 800 twips
flxDates.ColWidth(1) = 10300 'Col 1 width = 10300 twips
flxDates.RowHeight(0) = 310 'set height of row 0 to 310 twips
flxDates.RowHeight(1) = 450 'set height of row 1 to 450 twips
flxDates.RowHeight(2) = 1410 'set height of rows 2-6 to 1410 twips
flxDates.RowHeight(3) = 1410
flxDates.RowHeight(4) = 1410
flxDates.RowHeight(5) = 1410
flxDates.RowHeight(6) = 1410
flxDates.RowHeight(7) = 450 'set height of row 1 to 450 twips
flxDates.ColAlignment(0) = flexAlignCenterCenter 'align Col 0 CenterCenter
flxDates.ColAlignment(1) = flexAlignCenterCenter 'align Col 1 CenterCenter
SelectRange flxDates, 1, 1, 7, 1 'align Col 1, Rows 1-7 as
flxDates.CellAlignment = flexAlignLeftTop 'LeftTop
SelectRange flxDates, 1, 0, 7, 0 'set bold for col 0
flxDates.CellFontBold = True
SelectRange flxDates, 0, 0, 0, 1 'paint header row in white on navy blue
flxDates.CellBackColor = conNavy
flxDates.CellForeColor = vbWhite
SelectRange flxDates, 1, 0, 1, 1 'paint 1st row in cream (Sun)
flxDates.CellBackColor = conCream
SelectRange flxDates, 7, 0, 7, 1 'paint 7th col in cream (Sat)
flxDates.CellBackColor = conCream
vDate = txtToday - Weekday(txtToday) + 1 'calc first day of week from txtToday
vStartDate = vDate 'and save in vStartDate
flxDates.TextMatrix(0, 0) = "Date" 'show headers
flxDates.TextMatrix(0, 1) = "Diary appointments for week commencing Sunday " _
& Format(vDate, "d mmmm yyyy")
For vRow = 1 To 7 'enter day and dates in col 0
flxDates.TextMatrix(vRow, 0) = Format(vDate, "ddd") & vbCrLf & Format(vDate, "dd") _
& vbCrLf & Format(vDate, "mmm")
vDate = vDate + 1
Next
vDates = "#" & Format(vStartDate, "m-d-yyyy") & "# AND #" _
& Format(vStartDate + 6, "m-d-yyyy") & "#" 'format dates for week as a string
Set rst = CurrentDb.OpenRecordset("S
ELECT * FROM tblDiary WHERE StartDate BETWEEN " _
& vDates & " ORDER BY StartDate, StartTime") 'make list of appointments for week in order
Do Until rst.EOF
vRow = rst!StartDate - vStartDate + 1 'calc row No from date
vNotes = Format(rst!StartTime, "hh:nn") & " - " & Format(rst!EndTime, "hh:nn") _
& " " & Nz(rst!ClientName) & " " & " " & Nz(rst!Notes) 'fetch times and Notes
flxDates.TextMatrix(vRow, 1) = flxDates.TextMatrix(vRow, 1) _
& " " & vNotes & vbCrLf 'append new notes + CRLF to cell
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
flxDates.Redraw = True 're-display grid
Exit Sub
ErrorCode:
If Not rst Is Nothing Then rst.Close
Set rst = Nothing
Beep
MsgBox Err.Description
End Sub
Public Sub ShowMonth()
'Shows flex grid as current day
'Entry (txtToday) = Date to be displayed
' (tblDiary) holds appointments
Dim vRow As Long, vCol As Long, vCount As Long, vDays As Long
Dim vFirst As Date, vLast As Date
Dim vFlag As Boolean
Dim rst As Recordset
Dim vMonthYear As String
On Error GoTo ErrorCode
flxDates.Redraw = False
flxDates.Clear
flxDates.Cols = 7 'No of cols = 7
flxDates.MergeRow(0) = True 'set Merge on Row 0
flxDates.FixedRows = 2
flxDates.ScrollBars = 0
flxDates.FixedCols = 0
flxDates.ColWidth(0) = 1570 'Set Col 0 width to 1570 twips
flxDates.ColWidth(1) = 1584 'Set Col 1-5 width to 1584 twips
flxDates.ColWidth(2) = 1584
flxDates.ColWidth(3) = 1584
flxDates.ColWidth(4) = 1584
flxDates.ColWidth(5) = 1584
flxDates.ColWidth(6) = 1570 'Set Col 6 width to 1570 twips
flxDates.RowHeight(-1) = 1250 'set height all rows = 1250 twips
flxDates.RowHeight(0) = 350 'set height of row 0 = 350 twips
flxDates.RowHeight(1) = 450 'set height of row 1 = 450 twips
SelectRange flxDates, 0, 0, 1, 6 'set cell alignment of header rows 0 & 1
flxDates.CellAlignment = flexAlignCenterCenter
SelectRange flxDates, 2, 0, 7, 6 'set cell alignment of rows 2-6
flxDates.CellAlignment = flexAlignLeftTop
SelectRange flxDates, 0, 0, 0, 6 'paint header row in white on navy blue
flxDates.CellBackColor = conNavy
flxDates.CellForeColor = vbWhite
flxDates.CellFontBold = True 'set header font to bold
SelectRange flxDates, 2, 0, 7, 0 'paint 1st col in cream (Sun)
flxDates.CellBackColor = conCream
SelectRange flxDates, 2, 6, 7, 6 'paint 7th col in cream (Sat)
flxDates.CellBackColor = conCream
SelectRange flxDates, 1, 0, 1, 6 'paint header row 2 in white on Light blue
flxDates.CellBackColor = conLightBlue
flxDates.CellFontBold = True 'and in bold
vMonthYear = Format("1-" & txtMonth & "-" & txtYear, "mmmm yyyy") 'enter date in header
For vCol = 0 To 6 'set header for Col 0
flxDates.TextMatrix(0, vCol) = "Diary Appointments for " & vMonthYear
flxDates.TextMatrix(1, vCol) = Format(vCol + 1, "dddd") 'set day names for Row 1
Next
'Show dates in calendar first
vFirst = DateSerial(txtYear, txtMonth, 1) 'set vFirst = first date of month
vDays = DateSerial(Year(vFirst), Month(vFirst) + 1, 1) - DateSerial(Year(vFirst), Month(vFirst), 1) 'set vDays = No of days in current month
vLast = DateSerial(txtYear, txtMonth, vDays) 'set vLast = last date of month
vFlag = False
vCount = 1
For vRow = 2 To 7
For vCol = 0 To 6
If vFlag = False Then 'skip if 1st day done
If vCol = Weekday(vFirst) - 1 Then 'if Col No = weekday No then
vFlag = True 'set flag when 1st of month set
flxDates.TextMatrix(vRow, vCol) = vCount & vbCrLf 'copy day number to grid
vCount = vCount + 1 'inc counter
End If
Else
flxDates.TextMatrix(vRow, vCol) = vCount & vbCrLf 'copy day number to grid
vCount = vCount + 1
If vCount > vDays Then Exit For
End If
Next
If vCount > vDays Then Exit For
Next
'Copy scheduled dates to grid from table tblDiary
Set rst = CurrentDb.OpenRecordset("S
ELECT * FROM tblDiary WHERE StartDate BETWEEN #" _
& Format(vFirst, "m-d-yyyy") & "# AND #" & Format(vLast, "m-d-yyyy") & "#") 'return list of scheduled dates in current month
Do Until rst.EOF = True 'copy result of query grid
vDays = Day(rst!StartDate)
For vRow = 2 To 7
For vCol = 0 To 6
If Left(flxDates.TextMatrix(v
Row, vCol), 2) = vDays Then
flxDates.Row = vRow
flxDates.Col = vCol
flxDates.CellBackColor = conPaleGreen 'paint cell in light green
flxDates.TextMatrix(vRow, vCol) = flxDates.TextMatrix(vRow, vCol) _
& Nz(rst!ClientName) & vbCrLf 'add 1st 19 chrs to cell
End If
Next
Next
rst.MoveNext 'next record (if any)
Loop
rst.Close
Set rst = Nothing
flxDates.Redraw = True
Exit Sub
ErrorCode:
If Not rst Is Nothing Then rst.Close
Set rst = Nothing
Beep
MsgBox Err.Description
End Sub
Public Sub FillCalendarGrid()
'Display calendar for current month
'Entry (txtMonth) = month number (1-12)
' (txtYear) = year
Dim rst As Recordset
Dim vCount As Long, vRow As Long, vCol As Long, vDays As Long
Dim vFirst As Date
Dim vFlag As Boolean
Dim vMonthYear As String
On Error GoTo ErrorCode
flxCal.Redraw = False 'turn off screen updating
flxCal.ColWidth(-1) = 450 'set all col widths to 450
flxCal.RowHeight(-1) = 450 'set all row heights to 450
flxCal.RowHeight(0) = 300 'set row 0 height to 300
flxCal.RowHeight(1) = 300 'set row 1 height to 300
flxCal.ColAlignment(-1) = flexAlignCenterCenter 'Align all columns Centre-Centre (4)
flxCal.MergeRow(0) = True 'merge cells in row 0
flxCal.Clear 'clear all cells
'Show header titles
vMonthYear = Format("1-" & txtMonth & "-" & txtYear, "mmmm yyyy") 'set vMonthYear to 1-current month-current year
flxCal.TextMatrix(0, 0) = "<<"
flxCal.TextMatrix(0, 1) = vMonthYear
flxCal.TextMatrix(0, 2) = vMonthYear
flxCal.TextMatrix(0, 3) = vMonthYear
flxCal.TextMatrix(0, 4) = vMonthYear
flxCal.TextMatrix(0, 5) = vMonthYear
flxCal.TextMatrix(0, 6) = ">>"
flxCal.TextMatrix(1, 0) = "Sun" 'show days of week
flxCal.TextMatrix(1, 1) = "Mon"
flxCal.TextMatrix(1, 2) = "Tue"
flxCal.TextMatrix(1, 3) = "Wed"
flxCal.TextMatrix(1, 4) = "Thu"
flxCal.TextMatrix(1, 5) = "Fri"
flxCal.TextMatrix(1, 6) = "Sat"
SelectRange flxCal, 2, 0, 7, 6 'paint all rows in white first
flxCal.CellBackColor = vbWhite
SelectRange flxCal, 1, 0, 1, 6 'paint 2nd row in light blue (Sun Mon Tue Wed Thu Fri Sat)
flxCal.CellBackColor = conLightBlue
SelectRange flxCal, 2, 0, 7, 0 'paint 1st col in cream (Sun)
flxCal.CellBackColor = conCream
SelectRange flxCal, 2, 6, 7, 6 'paint 7th col in cream (Sat)
flxCal.CellBackColor = conCream
SelectRange flxCal, 2, 0, 7, 6 'set alignment to Centre-Centre for all squares
flxCal.ColAlignment(-1) = flexAlignCenterCenter
SelectRange flxCal, 0, 0, 0, 6 'show 1st row in Bold
flxCal.CellFontBold = True
'Show dates in calendar
vFirst = DateSerial(txtYear, txtMonth, 1) 'set vFirst = first date of month
vDays = DateSerial(Year(vFirst), Month(vFirst) + 1, 1) - DateSerial(Year(vFirst), Month(vFirst), 1) 'set vDays = No of days in current month
vFlag = False
vCount = 1
For vRow = 2 To 7
For vCol = 0 To 6
If vFlag = False Then 'skip if 1st day done
If vCol = Weekday(vFirst) - 1 Then 'if Col No = weekday No then
vFlag = True 'set flag when 1st of month set
flxCal.TextMatrix(vRow, vCol) = vCount 'copy day number to grid
If CheckToday(vCount) = True Then 'if calendar date = today then
SelectRange flxCal, vRow, vCol, vRow, vCol 'paint current square in bold/red
flxCal.CellFontBold = True
flxCal.CellForeColor = vbRed
End If
vCount = vCount + 1 'inc counter
End If
Else
flxCal.TextMatrix(vRow, vCol) = vCount 'copy day number to grid
If CheckToday(vCount) = True Then 'if calendar date = today then
SelectRange flxCal, vRow, vCol, vRow, vCol 'paint current text in bold/red
flxCal.CellFontBold = True
flxCal.CellForeColor = vbRed
End If
vCount = vCount + 1
If vCount > vDays Then Exit For
End If
Next
If vCount > vDays Then Exit For 'exit loop when all days displayed
Next
'Copy scheduled dates to Calendar grid
Set rst = CurrentDb.OpenRecordset("S
ELECT * FROM tblDiary " _
& "WHERE Month(StartDate) = " & txtMonth & " AND Year(StartDate) = " & txtYear) 'return list of scheduled dates in current month
Do Until rst.EOF = True 'copy result of query grid
vDays = Day(rst!StartDate)
For vRow = 2 To 7
For vCol = 0 To 6
If flxCal.TextMatrix(vRow, vCol) = vDays Then
flxCal.Row = vRow
flxCal.Col = vCol
flxCal.CellBackColor = conPaleGreen 'paint cell green if any appointments for date
End If
Next
Next
rst.MoveNext 'next record (if any)
Loop
rst.Close
Set rst = Nothing
flxCal.Redraw = True
Exit Sub
ErrorCode:
If Not rst Is Nothing Then rst.Close
Set rst = Nothing
Beep
MsgBox Err.Description
End Sub
Public Function CheckToday(vDay As Long) As Boolean
'Returns TRUE if vDay = Today's date
'Entry (vDay) = Date in month
' (txtMonth) = Month No (1-12)
' (txtYear) = Year
Dim vDate As Date
vDate = DateSerial(txtYear, txtMonth, vDay)
If vDate = Date Then CheckToday = True
End Function
Private Sub flxCal_Click()
'User clicks on calendar grid
Dim vCol As Long, vRow As Long
Dim vText As String
Dim vDate As Date
FlexGridClick flxCal, vRow, vCol, vText 'return Row, Col & Contents of selected cell
If vRow = 0 And vCol = 0 Then DecMonth: Exit Sub 'if user clicks << then decrement month (and year)
If vRow = 0 And vCol = 6 Then IncMonth: Exit Sub 'if user clicks << then increment month (and year)
If vRow > 1 Then 'if user clicks on a date box then
If vText <> "" Then 'if not blank then
vDate = vText & "-" & txtMonth & "-" & txtYear 'calc new date
txtToday = vDate 'and copy to txtToday field
ShowAppointments 're-display date list
Else 'if User clicks on blank cell then
If fraMode = 2 Then 'if WEEK mode then
For vCol = 0 To 6
If vText <> "" Then 'if not blank then
vDate = vText & "-" & txtMonth & "-" & txtYear 'calc new date
txtToday = vDate 'and copy to txtToday field
ShowAppointments 're-display date list
End If
Next
End If
End If
End If
End Sub
Public Sub DecMonth()
'Decrement Month number
'Entry (txtMonth) = Current Month number
' (txtYear) = Current Year
txtMonth = txtMonth - 1 'subtract 1 from month
If txtMonth = 0 Then 'if now 0 then
txtMonth = 12 'change to 12 and
txtYear = txtYear - 1 'decrement Year
End If
FillCalendarGrid 're-display calendar grid
If fraMode = 3 Then ShowAppointments 'and re-display appointments grid if in MONTH mode
End Sub
Public Sub IncMonth()
'Increment Month number
'Entry (txtMonth) = Current Month number
' (txtYear) = Current Year
txtMonth = txtMonth + 1 'add 1 to month
If txtMonth = 13 Then 'if now 13 then
txtMonth = 1 'change to 1 and
txtYear = txtYear + 1 'increment Year
End If
FillCalendarGrid 're-display calendar grid
If fraMode = 3 Then ShowAppointments 'and re-display appointments grid if in MONTH mode
End Sub
Public Sub ShowAppointments()
'Update display if user changes modes
Select Case fraMode
Case 1 'if DAY mode
ShowDay 'show appts for selected day
Case 2 'if WEEK mode
ShowWeek 'show appts for selected week
Case 3 'if MONTH mode
ShowMonth 'show appts for selected month
End Select
End Sub
==========================
==========
I would really love to get this implemented as soon as I could so any suggestions would be great, Im already behind schedule on the delivery and this is the only remaining holdup.
Start Free Trial