Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add <All> to combo box selection

Status
Not open for further replies.

oxicottin

Programmer
Jun 20, 2008
353
US
Hello, I have a combobox on my search form that uses a query as its RowSource to display a text employee name. I want to be able to select "All" to display all records.... below is my RowSource.

Code:
SELECT [qry_ActiveInactiveEmployees].[EmployeeName] FROM [qry_ActiveInactiveEmployees] ORDER BY [EmployeeName];


Here is what I tried with no luck....

Code:
SELECT EmployeeName FROM qry_ActiveInactiveEmployees 
UNION SELECT "<All>" FROM qry_ActiveInactiveEmployees SELECT [qry_ActiveInactiveEmployees].[EmployeeName] FROM [qry_ActiveInactiveEmployees] ORDER BY [EmployeeName];


Thanks,
SoggyCashew.....
 
Did you try:
[tt]
SELECT "<All>" AS EmployeeName FROM qry_ActiveInactiveEmployees
UNION
SELECT [qry_ActiveInactiveEmployees].[EmployeeName]
FROM [qry_ActiveInactiveEmployees] ORDER BY 1
[/tt]



Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Andy, your example does give me the "All" at the top of my list but it doesnt do anything. I have also tried using the employeeID number which gives me the <All> but doesnt do anything untill I select an employees name from the list.

Code:
SELECT 0 as EmployeeID,  "< ALL >" as EmployeeName FROM qry_ActiveInactiveEmployees Union SELECT [qry_ActiveInactiveEmployees].[EmployeeID], [qry_ActiveInactiveEmployees].[EmployeeName] FROM [qry_ActiveInactiveEmployees] ORDER BY [EmployeeName];

Thanks,
SoggyCashew.....
 
untill I select an employees name from the list
Which code is executed when an employee is selected ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV, with Andys example I tried the commented out part of my search function and with the part not commented out I tried in my last responce.


Code:
If Not IsNull(Me.cboEmployeeName) Then
        'varWhere = varWhere & "([EmployeeName] = """ & Me.cboEmployeeName & """) AND "
        varWhere = varWhere & "([EmployeeID] = " & Me.cboEmployeeName & ") AND "
    End If

Thanks,
SoggyCashew.....
 
Here is the whole thing in short...

Code:
Private Sub cboEmployeeName_AfterUpdate()
' Update the record source
    Me.Form.RecordSource = "SELECT * FROM qry_SearchEntries " & BuildFilter
    Me.Requery
End Sub

Code:
Private Function BuildFilter() As Variant
    Dim varWhere As Variant
    Dim intIndex As Integer
    Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.

    varWhere = Null
    
    If Not IsNull(Me.cboEmployeeName) Then
         varWhere = varWhere & "([EmployeeName] = """ & Me.cboEmployeeName & """) AND "
        'varWhere = varWhere & "([EmployeeID] = " & Me.cboEmployeeName & ") AND "
    End If
    
    If Not IsNull(Me.txtContactDate) Then
        varWhere = varWhere & "([DateOfIncident] = " & Format(Me.txtContactDate, conJetDate) & ") AND "
    End If
        
    If Not IsNull(Me.optContactGroup) Then
        If Me.optContactGroup.Value = 1 Then
        varWhere = varWhere & "([Contact] = " & Me.optContactGroup & ") AND "
        Else
        varWhere = varWhere & "([Contact] = " & Me.optContactGroup & ") AND "
        End If
    End If
    
    ' Check if there is a filter to return...
    If IsNull(varWhere) Then
        varWhere = ""
    Else
        varWhere = "WHERE " & varWhere
        
        ' strip off last "AND" in the filter
        If Right(varWhere, 5) = " AND " Then
            varWhere = Left(varWhere, Len(varWhere) - 5)
        End If
    End If
    
    BuildFilter = varWhere

End Function

Thanks,
SoggyCashew.....
 
If Not IsNull(Me.cboEmployeeName) [!]And Me.cboEmployeeName <> "< ALL >" [/!]Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You would be a lot better off if you can actually SEE your Filter:
[tt]...[blue]
Debug.Print varWhere[/blue]
BuildFilter = varWhere
...
[/tt]
This way you would notice right away that you have something like:
[tt]
([EmployeeName] = "<All>") AND ...
[/tt]

Unless you do have an employee with the name <All>, this will not work :-(

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Andy if I did it PHV way and debug it does give me....

WHERE ([EmployeeID] = 0)

So shouldnt that show all employees since there is no 0 ID? .... I use the same technique on a simular search and it works I just cant figure out why not with this one....

Thanks,
SoggyCashew.....
 
So, use this:
If Not IsNull(Me.cboEmployeeName) And Me.cboEmployeeName <> 0 Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV, lol I dont know why I didnt try zero instead of the <All>..... I worked great! Thanks very much!!!!

Thanks,
SoggyCashew.....
 
You can thank PHV by clicking on:
[blue]
Like this post?
Star it![/blue]


This way other TT users will know this post was helpful



Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Even simpler:
If Nz(Me.cboEmployeeName, 0) <> 0 Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top