0
votes

So I'm fairly new to Ms Access and VBA. I'm trying to create an attendance calendar template where

  • I calculate an attendance code based on an employee's attendance
  • present that code onto a continuous form, as the employee number is fairly large

I have gotten all the necessary queries for all data required but I just can't seem to display it the way I want to

I used a DAO recordset to pull the data and whatnot. I have searched the internet high and low but can't seem to find and answer, so here I am.

Problem:

  • second and succeeding records use the first record's ID number instead of their own. The Name textbox is already bound to a query.

https://i.stack.imgur.com/TLeFJ.png

PS. I've used this template as basis for the VBA coding.

Public Sub Main()

'On Error GoTo ErrorHandler

Call InitVariables
Call InitArray
Call LoadArray
Call PrintArray


'ExitSub:
'    Exit Sub
'ErrorHandler:
'    msgbox "There has been an error. Please reload the form"
'    Resume ExitSub
End Sub

Private Sub InitVariables()
'On Error GoTo ErrorHandler

intMonth = Me.cboMonth
intYear = Me.cboYear
lngFirstDayOfMonth = CLng(DateSerial(intYear, intMonth, 1))
intFirstWeekday = getFirstWeekday(lngFirstDayOfMonth)
intDaysInMonth = getDaysInMonth(intMonth, intYear)

'ExitSub:
'    Exit Sub
'ErrorHandler:
'    msgbox "There has been an error. Please reload the form"
'    Resume ExitSub
End Sub


Private Sub InitArray()
Dim i As Integer

ReDim myArray(0 To 30, 0 To 2)

For i = 0 To 30


    myArray(i, 0) = lngFirstDayOfMonth - intFirstWeekday + 2 + i
    If Month(myArray(i, 0)) = intMonth Then
        myArray(i, 1) = True
        myArray(i, 2) = Day(myArray(i, 0))
    Else
        myArray(i, 1) = False

    End If
Next i

End Sub

Private Sub LoadArray()

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset

Dim rsFiltered As DAO.Recordset
Dim strSQL, strSQLTraxID As String
Dim i As Integer



    'If Not rs.BOF And Not rs.EOF Then

        For i = LBound(myArray) To UBound(myArray)

            If myArray(i, 1) Then

   strSQL = "SELECT EiSyS.Date, [CAM Database].[Trax ID],  [CAM Database].[Full Name], EiSyS.AttendanceCode " _
  & "FROM EiSyS INNER JOIN [CAM Database] ON EiSyS.EmployeeID = [CAM Database].[Trax ID] where EiSyS.Date = CDate('" & Format(myArray(i, 0), "mm/dd/yyyy") & "') and " _
  & "EiSyS.EmployeeID = " & Me.[EmployeeID]

                Set db = CurrentDb
                Set rs = db.OpenRecordset(strSQL)
                Set rsFiltered = rs.OpenRecordset

                Do While (Not rsFiltered.EOF)

                    myArray(i, 2) = rs!AttendanceCode

                    Debug.Print Format(myArray(i, 0), "mm/dd/yyyy") & " - " & rs!AttendanceCode & " - " & Me.[EmployeeID]

                    rsFiltered.MoveNext
                Loop

            End If
        Next i

    'End If

    rsFiltered.Close

    rs.Close

Set rsFiltered = Nothing
Set rs = Nothing
Set rs1 = Nothing

Set db = Nothing


End Sub

Private Sub PrintArray()
'On Error GoTo ErrorHandler

Dim strCtlName As String
Dim i As Integer

For i = LBound(myArray) To UBound(myArray)
    strCtlName = "text" & CStr(i + 1)
    Controls(strCtlName).Tag = i
    Controls(strCtlName) = ""
    Controls(strCtlName) = myArray(i, 2)


Next i


'ExitSub:
'    Exit Sub
'ErrorHandler:
'    msgbox "There has been an error. Please reload the form"
'    Resume ExitSub

End Sub
1
your textboxes have to be bound to a field in your query - all of them - dbmitch
@dbmitch I only need one textbox bound and it already is.. Me.[EmployeeID] is the textbox that's bound with the employeeID but as you can see all succeeding entries still use the first textbox's data.. Perhaps i'm missing something in the loop.. - Trevor Shardani
@dbmitch Thanks for the insight.. I used your suggestion and my form works perfectly ^_^. I'll have to use VBA to dynamically update the record source tho.. - Trevor Shardani

1 Answers

0
votes

Used @dbmitch's suggestion to bind all textboxes to a query. I used Transform Pivot on the original query to make the necessary column names, matched and bound those to the appropriate textboxes. I'll prolly use VBA to update them if I want to view it differently but so far it works.