Creating a 'calendar matrix' in Access
Asked Answered
B

4

5

I’m trying to create either a report or form that displays data in essentially a “calendar” form.

I have a course query that is (simplified) as “Course name”; “course days”; “course times”---

Course; Days; Times
PSY 1; MW; 8A-9A
SOC 150; M; 8A-11A
ANTH 2; Tu; 8A-9A
ANTH 199; MW; 8A-9A

In Access, I’m trying to create a form based on the query that would give me a matrix of the following:

  • Columns: Times in hour increments
  • Rows: Days of week

So, for example, with the above data, it would appear like this: Edit: Yargh, I can't submit an image unfortunately. So, here is a link to a "course schedule" that is essentially what I'm trying to do: Schedule

I have no idea where to start with this. Any tips (or links)?

Edit:

One idea I have is to create a form with a field for every possible cell in the matrix (so, for example, there would be one "Monday, 8-9A" field--and that field would be a filter on the query that ONLY displays results where "day" contains "M" and BeginTime or EndTime or between 8A and 9A). Unfortunately, I'm not sure how to do that.

Bootlick answered 26/10, 2012 at 19:42 Comment(1)
Hmmm. Well, I'm not trying to set up a traditional "calendar"--as in, "where will I be on a day and at what time." What I am trying to do is set up a curriculum to use administratively to see our entire schedule (a couple hundred courses) and where the days and times are used the heaviest. We also will want to filter down to ONLY see certain subject areas, etc. I'm not sure if Google calendar would provide the flexibility.Bootlick
I
10

You can do something close to what you seem to want as an Access form, but it's not easy. This screen capture displays your sample data in a Datasheet View form whose record source is an ADO disconnected recordset. It uses conditional formatting to set the text box background color when the text box value is not Null. Your picture suggested a different color for each Course, but I didn't want to deal with that when more than one Course can be scheduled in the same time block ... my way was simpler for me to cope with. :-)

enter image description here

The code to create and load the disconnected recordset is included below as GetRecordset(). The form's open event sets its recordset to GetRecordset().

Private Sub Form_Open(Cancel As Integer)
    Set Me.Recordset = GetRecordset
End Sub

Note I stored your sample data differently. Here is my Class_sessions table:

Course   day_of_week  start_time  end_time
------   -----------  ----------  -----------
PSY 1              2  8:00:00 AM   9:00:00 AM
PSY 1              4  8:00:00 AM   9:00:00 AM
SOC 150            2  8:00:00 AM  11:00:00 AM
ANTH 2             3  8:00:00 AM   9:00:00 AM
ANTH 199           2  8:00:00 AM   9:00:00 AM
ANTH 199           4  8:00:00 AM   9:00:00 AM

This is the function to create the disconnected recordset which is the critical piece for this approach. I developed this using early binding which requires a reference for "Microsoft ActiveX Data Objects [version] Library"; I used version 2.8. For production use, I would convert the code to use late binding and discard the reference. I left it as early binding so that you may use Intellisense to help you understand how it works.

Public Function GetRecordset() As Object
    Dim rsAdo As ADODB.Recordset
    Dim fld As ADODB.Field
    Dim db As DAO.Database
    Dim dteTime As Date
    Dim i As Long
    Dim qdf As DAO.QueryDef
    Dim rsDao As DAO.Recordset
    Dim strSql As String

Set rsAdo = New ADODB.Recordset
With rsAdo
    .Fields.Append "start_time", adDate, , adFldKeyColumn
    For i = 2 To 6
        .Fields.Append WeekdayName(i), adLongVarChar, -1, adFldMayBeNull
    Next
    .CursorType = adOpenKeyset
    .CursorLocation = adUseClient
    .LockType = adLockPessimistic
    .Open
End With

strSql = "PARAMETERS block_start DateTime;" & vbCrLf & _
    "SELECT day_of_week, Course, start_time, end_time" & vbCrLf & _
    "FROM Class_sessions" & vbCrLf & _
    "WHERE [block_start] BETWEEN start_time AND end_time" & vbCrLf & _
    "ORDER BY day_of_week, Course;"
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strSql)

dteTime = #7:00:00 AM#
Do While dteTime < #6:00:00 PM#
    'Debug.Print "Block start: " & dteTime
    rsAdo.AddNew
    rsAdo!start_time = dteTime
    rsAdo.Update

    qdf.Parameters("block_start") = dteTime
    Set rsDao = qdf.OpenRecordset(dbOpenSnapshot)
    Do While Not rsDao.EOF
        'Debug.Print WeekdayName(rsDao!day_of_week), rsDao!Course
        rsAdo.Fields(WeekdayName(rsDao!day_of_week)) = _
            rsAdo.Fields(WeekdayName(rsDao!day_of_week)) & _
            rsDao!Course & vbCrLf
        rsAdo.Update
        rsDao.MoveNext
    Loop

    dteTime = DateAdd("h", 1, dteTime)
Loop

rsDao.Close
Set rsDao = Nothing
qdf.Close
Set qdf = Nothing
Set GetRecordset = rsAdo
End Function
Indoctrinate answered 27/10, 2012 at 19:14 Comment(2)
Great answers to all, but this is exactly what I needed! Thanks SO much!Bootlick
I know, I know, we are four years down the line BUT this just happens to be exactly what I was looking for. Unfortunately, my datasheet shows up empty. I am running MS Access 2016 and followed the answer to the letter. Could you please give some details on the form settings? ThanxSwashbuckling
D
3

Actually, if you look at the following video of mine, you can see a calendar created in Access that runs inside of a browser with the new Access Web publishing feature.

http://www.youtube.com/watch?v=AU4mH0jPntI

So, all you really need to do here is format a form with text boxes and setup some code to fill them. VBA or even the above video shows this is quite easy for Access.

Derry answered 27/10, 2012 at 20:22 Comment(0)
G
2

I doubt that you will find an easy solution for this problem in Access forms or reports.
The issue is that you need to format different cells differently, and that cells can span multiple rows and have to be merged.

If I were you, I would go in either of these 2 directions:

  • Drive Excel from Access, because you can format and merge cells independently
  • Use the Web Browser Control to display HTML that you construct yourself using tables, or a more high-level library like FullCalendar

I would be partial to tryingthe Web Browser and find the right library that can properly format the data.

Graptolite answered 27/10, 2012 at 15:12 Comment(0)
W
0

I know this post is quite old, but I had the same requirement but I got round it by doing the following:

  • I created a module that would write HTML code (as a text file) to produce the data using a table.
  • I used the colspan feature of tables to enable me to dynamically produce the view I needed.
  • The HTML file was created on the Form_Open event and by pointing the webbrowser control to the HTML file the view shows the latest data.
  • A css file is used to handle the visual display of the HTML file so that it looks similar to the Access forms already in use.

If anyone is interested I can post some code to further illustrate.

Waylonwayman answered 13/7, 2016 at 10:4 Comment(1)
Thanks for taking the time to post. An answer should concisely, completely, and unbiasedly answer the question. If you have an answer that will answer the question please do post it.Baber

© 2022 - 2024 — McMap. All rights reserved.