I have 3 comboboxes and intend to have code that when a button (cmdSearch) is clicked will read the data from the comboboxes and dynamically create a query, here is my function:
Private Function fncGenerateQuery() As Boolean
Dim strSQL As String
Dim qdfTemp As QueryDef
Dim strWhere As String
strSQL = "SELECT Teacher.TeacherName, Subject.SubjectName, Work.YearGroup, Work.Dateset, Work.Datedue, Work.Details, Work.Additionalinfo, FROM Teacher INNER JOIN (Subject INNER JOIN Work ON Subject.SubjectID = Work.SubjectID) ON Teacher.TeacherID = Work.TeacherID "
If Not (Me.Combo17.Value = vbNullString) Then
If Len(strWhere) > 0 Then
strWhere = strWhere & " WHERE "
Else
strWhere = strWhere & " AND "
End If
strWhere = strWhere & " Teacher.Name = '" & Me.Combo17.Value & "'"
If Not (Me.cboSubject.Value = vbNullString) Then
If Len(strWhere) > 0 Then
strWhere = strWhere & " WHERE "
Else
strWhere = strWhere & " AND "
End If
strWhere = strWhere & " Teacher.Name = '" & Me.cboSubject.Value & "'"
If Not (Me.Combo21.Value = vbNullString) Then
If Len(strWhere) > 0 Then
strWhere = strWhere & " WHERE "
Else
strWhere = strWhere & " AND "
End If
strWhere = strWhere & " Teacher.Name = '" & Me.Combo21.Value & "'"
strSQL = strSQL & strWhere & ";"
Set qdfTemp = CurrentDB.QueryDefs("qryMain"
qdfTemp.SQL = strSQL
Set qdfTemp = Nothing
fncGenerateQuery = True
End Function
I have then tried to call up this function from the command button so:
Private Sub cmdSearch_Click()
Call fncGenerateQuery
End Sub
However, when I try this it displays the error message:
Run-time error "3296"
Join expression not supported
Which highlights the line:
qdfTemp.SQL = strSQL
Please help!
Private Function fncGenerateQuery() As Boolean
Dim strSQL As String
Dim qdfTemp As QueryDef
Dim strWhere As String
strSQL = "SELECT Teacher.TeacherName, Subject.SubjectName, Work.YearGroup, Work.Dateset, Work.Datedue, Work.Details, Work.Additionalinfo, FROM Teacher INNER JOIN (Subject INNER JOIN Work ON Subject.SubjectID = Work.SubjectID) ON Teacher.TeacherID = Work.TeacherID "
If Not (Me.Combo17.Value = vbNullString) Then
If Len(strWhere) > 0 Then
strWhere = strWhere & " WHERE "
Else
strWhere = strWhere & " AND "
End If
strWhere = strWhere & " Teacher.Name = '" & Me.Combo17.Value & "'"
If Not (Me.cboSubject.Value = vbNullString) Then
If Len(strWhere) > 0 Then
strWhere = strWhere & " WHERE "
Else
strWhere = strWhere & " AND "
End If
strWhere = strWhere & " Teacher.Name = '" & Me.cboSubject.Value & "'"
If Not (Me.Combo21.Value = vbNullString) Then
If Len(strWhere) > 0 Then
strWhere = strWhere & " WHERE "
Else
strWhere = strWhere & " AND "
End If
strWhere = strWhere & " Teacher.Name = '" & Me.Combo21.Value & "'"
strSQL = strSQL & strWhere & ";"
Set qdfTemp = CurrentDB.QueryDefs("qryMain"
qdfTemp.SQL = strSQL
Set qdfTemp = Nothing
fncGenerateQuery = True
End Function
I have then tried to call up this function from the command button so:
Private Sub cmdSearch_Click()
Call fncGenerateQuery
End Sub
However, when I try this it displays the error message:
Run-time error "3296"
Join expression not supported
Which highlights the line:
qdfTemp.SQL = strSQL
Please help!