Variable not recognised in SQL (1 Viewer)

Kayleigh

Member
Local time
Today, 16:25
Joined
Sep 24, 2020
Messages
706
Hi,
I have code to display query result as a drop-down of field but the variable Wkdy keeps popping up as error. Have I coded this wrong?
Code:
Private Sub fldSessionLogID_Enter()
Dim strSql As String
Dim Wkdy As Integer

Wkdy = Weekday(Me.fldIDate)

strSql = "SELECT DISTINCT tblSession.fldSessionID, tblSubject.fldSubjectName, qrylkpStaffNameShort.StaffShortName " & vbCrLf & _
"FROM qrylkpStaffNameShort RIGHT JOIN (tblWeekdays RIGHT JOIN (tblTerm INNER JOIN (tblSubject RIGHT JOIN ((tblRooms RIGHT JOIN (tblSession LEFT JOIN qrylkpClassName ON tblSession.fldClassID = qrylkpClassName.fldClassID) ON tblRooms.fldRoomID = tblSession.fldRoomID) LEFT JOIN jtblSessionWeekday ON tblSession.fldSessionID = jtblSessionWeekday.fldSessionID) ON tblSubject.fldSubjectID = tblSession.fldSubjectID) ON tblTerm.fldTermID = tblSession.fldTermID) ON tblWeekdays.fldWeekdayID = jtblSessionWeekday.fldWeekdayID) ON qrylkpStaffNameShort.fldStaffID = tblSession.fldStaffID " & vbCrLf & _
"WHERE (((tblTerm.fldStart)<[Forms]![frmIncidentLogList]![txtTo]) AND ((tblTerm.fldEnd)>[Forms]![frmIncidentLogList]![txtFrom]) AND ((tblWeekdays.fldWeekdayID)= Wkdy )) " & vbCrLf & _
"ORDER BY tblSubject.fldSubjectName;"

Me.fldSessionLogID.RowSource = strSql
Me.fldSessionLogID.Dropdown

End Sub
 

Isaac

Lifelong Learner
Local time
Today, 08:25
Joined
Mar 14, 2017
Messages
8,777
You can't refer to variables directly in Access SQL, like you can in T-SQL. You have to build a dynamic sql string by concatenating your static literal sql with the variable's literal value.

For example, instead of:
Code:
dim var
var="something"
strSQL = "select * from table where field=var"

You'd need something more like:
Code:
dim var
var="something"
strSQL = "select * from table where field=" & var
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:25
Joined
May 7, 2009
Messages
19,237
also, but not related, you don't need to add vbCrLf on the SQL string.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 01:25
Joined
Jan 20, 2009
Messages
12,852
also, but not related, you don't need to add vbCrLf on the SQL string.
That is included to include line breaks if you want to view the output.
The second line would need a lot more of them to really work though.
 

Users who are viewing this thread

Top Bottom