D
default105
I have the following code to put the date a student shows for class into the
proper table. I have included the entire code to hopefully better help you
understand what I am doing. The fields in this table are named for the year
respectively, ie... 2006, 2007, 2008, 2009 etc...
Is there a way to use a variable to allow me to tell the query to choose the
field based on the current year? I already have code to determine the
current year but I cannot figure out how to input it into replace the current
field [2006] in the DoCmd.RunSQL.
**************************CODE******************
************************************************
Private Sub Login_Click()
On Error GoTo Err_Login_Click
If Me.PassVerify = Me.Password Then 'determine correct password
Dim MemName, MyDate, MyMonth, YourMonth, MyDay, MyYear, TodayDate, AllowEntry
MyDate = Now() ' Assign a date.
MyMonth = month(MyDate) 'Determines current month number
YourMonth = MonthName(MyMonth) 'Determines current month name
MyDay = day(MyDate) 'Determines current day
MyYear = year(MyDate) 'Determines current year
MemName = Me.Username 'Show selected user from listbox in text box
TodayDate = MyMonth & "/" & MyDay & "/" & MyYear 'Set format for todays date
AllowEntry = DCount(MyYear, "Attendance", "[MemberID]=" & [MemberID])
'disable multiple logins
Me.Date.Value = TodayDate 'Place date in date text box for append query
If AllowEntry = 0 Then 'Determine if student has logged in
'Show Student they have logged in for class
Me.LoginOkLabel.Caption = MemName & " has attended class on " & YourMonth &
" " & MyDay & ", " & MyYear
'If the UserId field type is string, add a single quote before and after
DoCmd.RunSQL "INSERT INTO Attendance ( [MemberID], [2006]) VALUES (" &
[MemberID] & ", [Date])"
Else
'Alert Student they have already logged in
Dim EMsg, EStyle, ETitle, EHelp, ECtxt, EResponse
EMsg = "You have already logged in today," + Chr(13) + _
"Please click Ok" ' Define message.
EStyle = vbOKOnly + vbCritical ' Define buttons.
ETitle = "Already Logged In" ' Define title.
EHelp = "DEMO.HLP" ' Define Help file.
ECtxt = 1000 ' Define topic
' context.
' Display message.
EResponse = MsgBox(EMsg, EStyle, ETitle, EHelp, ECtxt)
End If
Else 'Notify student they entered the wrong password
Me.LoginOkLabel.Caption = "Incorrect Password, please try again"
Dim Msg, Style, Title, Help, Ctxt, Response
Msg = "The password you entered is incorrect," + Chr(13) + _
"Please try again" ' Define message.
Style = vbOKOnly + vbCritical ' Define buttons.
Title = "Login Failed" ' Define title.
Help = "DEMO.HLP" ' Define Help file.
Ctxt = 1000 ' Define topic
' context.
' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
End If
Me.PassVerify = Null 'Clear the password field
Exit_Login_Click:
Exit Sub
Err_Login_Click:
MsgBox Err.Description
Resume Exit_Login_Click
End Sub
proper table. I have included the entire code to hopefully better help you
understand what I am doing. The fields in this table are named for the year
respectively, ie... 2006, 2007, 2008, 2009 etc...
Is there a way to use a variable to allow me to tell the query to choose the
field based on the current year? I already have code to determine the
current year but I cannot figure out how to input it into replace the current
field [2006] in the DoCmd.RunSQL.
**************************CODE******************
************************************************
Private Sub Login_Click()
On Error GoTo Err_Login_Click
If Me.PassVerify = Me.Password Then 'determine correct password
Dim MemName, MyDate, MyMonth, YourMonth, MyDay, MyYear, TodayDate, AllowEntry
MyDate = Now() ' Assign a date.
MyMonth = month(MyDate) 'Determines current month number
YourMonth = MonthName(MyMonth) 'Determines current month name
MyDay = day(MyDate) 'Determines current day
MyYear = year(MyDate) 'Determines current year
MemName = Me.Username 'Show selected user from listbox in text box
TodayDate = MyMonth & "/" & MyDay & "/" & MyYear 'Set format for todays date
AllowEntry = DCount(MyYear, "Attendance", "[MemberID]=" & [MemberID])
'disable multiple logins
Me.Date.Value = TodayDate 'Place date in date text box for append query
If AllowEntry = 0 Then 'Determine if student has logged in
'Show Student they have logged in for class
Me.LoginOkLabel.Caption = MemName & " has attended class on " & YourMonth &
" " & MyDay & ", " & MyYear
'If the UserId field type is string, add a single quote before and after
DoCmd.RunSQL "INSERT INTO Attendance ( [MemberID], [2006]) VALUES (" &
[MemberID] & ", [Date])"
Else
'Alert Student they have already logged in
Dim EMsg, EStyle, ETitle, EHelp, ECtxt, EResponse
EMsg = "You have already logged in today," + Chr(13) + _
"Please click Ok" ' Define message.
EStyle = vbOKOnly + vbCritical ' Define buttons.
ETitle = "Already Logged In" ' Define title.
EHelp = "DEMO.HLP" ' Define Help file.
ECtxt = 1000 ' Define topic
' context.
' Display message.
EResponse = MsgBox(EMsg, EStyle, ETitle, EHelp, ECtxt)
End If
Else 'Notify student they entered the wrong password
Me.LoginOkLabel.Caption = "Incorrect Password, please try again"
Dim Msg, Style, Title, Help, Ctxt, Response
Msg = "The password you entered is incorrect," + Chr(13) + _
"Please try again" ' Define message.
Style = vbOKOnly + vbCritical ' Define buttons.
Title = "Login Failed" ' Define title.
Help = "DEMO.HLP" ' Define Help file.
Ctxt = 1000 ' Define topic
' context.
' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
End If
Me.PassVerify = Null 'Clear the password field
Exit_Login_Click:
Exit Sub
Err_Login_Click:
MsgBox Err.Description
Resume Exit_Login_Click
End Sub