I post the database in order to preserve time for every one which wish to help me in finding a solution. Not to preserve
my time.
In attached pics are the definitions for tables and the "in trouble" query.
The SQL for this query is:
- SELECT tblStaff.Name_Surname, tblAssignedAbilities.Abilitie_ID
-
FROM tblStaff INNER JOIN tblAssignedAbilities ON tblStaff.Staff_ID = tblAssignedAbilities.Staff_ID
-
WHERE (((tblStaff.Staff_ID)=WhereClause()));
Also I have a form,
frmStaffAbilities (for VBA it's name is
Form_frmStaffAbilities) which has as record source the query
qStaffAbilities.
Also, in this form, I have a list box named
lstAbilities. It is a Multi Select list box.
The row source for this list box is:
- SELECT [tblAbilities].[Abbilitie_ID], [tblAbilities].[Abilitie] FROM tblAbilities ORDER BY [Abilitie];
Finally the function
WhereClause() - Public Function WhereClause()
-
Dim WhereSQL As String 'Store the Where clause
-
-
Dim FirstItem As Boolean 'True until first selected item is find in lstAbilities
-
FirstItem = True
-
-
Dim i As Integer, IDabil As Integer 'Work variables
-
-
On Error GoTo ErrorHandler
-
For i = 0 To Form_frmStaffAbilities.lstAbilities.ListCount - 1 'Scan lstAbilities
-
If Form_frmStaffAbilities.lstAbilities.Selected(i) Then
-
IDabil = Form_frmStaffAbilities.lstAbilities.Column(0, i) 'Store ID_Abilitie
-
If FirstItem Then
-
WhereSQL = IDabil
-
FirstItem = False
-
Else
-
WhereSQL = WhereSQL & " Or " & IDabil
-
End If
-
End If
-
Next i
-
-
If FirstItem Then 'Nothing is selected
-
WhereClause = "Like ""*""" 'Not working
-
Else 'There are selected items
-
WhereClause = WhereSQL
-
End If
-
-
Ex:
-
MsgBox (WhereClause)
-
' Debug.Print WhereClause
-
Exit Function
-
-
ErrorHandler:
-
Select Case Err.Number
-
Case 2427 'You entered an expression that has no value - When the form is first loading
-
WhereClause = "Like ""*""" 'Not working
-
Case Else
-
MsgBox ("MyMesge " & Err.Number & " " & Err.Description)
-
End Select
-
-
Resume Ex
-
End Function
Also I try a very simplified function:
- 'Simplified function:
-
Public Function WhereClauseSimple()
-
WhereClause = 1 'Working
-
WhereClause = 2 'Working
-
WhereClause = 3 'Working
-
-
WhereClause = "Like ""*""" 'Not working
-
WhereClause = "1 Or 2" 'Not working
-
MsgBox (WhereClause)
-
End Function