Combobox with all option (1 Viewer)

mmaybe

New member
Local time
Today, 07:29
Joined
Oct 23, 2018
Messages
4
Hello,
I made a combobox with value list (All,Paid,Unpaid) to filter a subform
and put a VBA code in After Update Event, it works for Paid and Unpaid because it's mach the same choices in my query. I want help to make (All)option display all paid and unpaid record

Code is:

Code:
Private Sub cboStatus_AfterUpdate()
Dim cstatus As String
cstatus = " select * from Client_Invoice_Status where ([Company]= '" & Me.cboCompany & "') And ([Status]= '" & Me.cboStatus & "') And ([PolicyType]= '" & Me.cboPolicyType & "' )"
Me.Client_Status.Form.RecordSource = cstatus
Me.Client_Status.Form.Requery
End Sub


Thank you
 

plog

Banishment Pending
Local time
Yesterday, 22:29
Joined
May 11, 2011
Messages
11,646
What you do is compile your WHERE statement dynamically in the VBA. First test to see if the value is 'All', if it is you don't do anything because there's no need to apply criteria. If the value is not 'All' you add it to your WHERE:

pseudo code:


strWhere="(1=1)"
if (cbo1<>"All) Then strWhere = " AND (Field1 = "' & cbo1 & "')"
if (cbo2<>"All) Then strWhere = " AND (Field2 = "' & cbo2 & "')"
...
Me.Client_Status.Form.RecordSource = cstatus & strWhere
 

Micron

AWF VIP
Local time
Yesterday, 23:29
Joined
Oct 20, 2018
Messages
3,478
There seems to be only one combo, so why not just apply a filter using the selected item as long as it's not "All"?
EDIT
Sorry, was going by the explanation rather than the code. I suppose one could simply use the WHERE part of that select statement as a filter though.
 
Last edited:

Dreamweaver

Well-known member
Local time
Today, 04:29
Joined
Nov 28, 2005
Messages
2,466
SELECT "*" As SaveName,"All" As SaveType FROM "same table as in other side of union" UNION SELECT Your sql FROM your tableWHERE your criteria



You will need to edit it but will do what you want
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:29
Joined
Oct 29, 2018
Messages
21,471
Hi mmaybe. If I understand it correctly, you could also try the following:

Code:
cstatus = " select * from Client_Invoice_Status where ([Company]= '" & Me.cboCompany & "') And ([Status]= '" & Me.cboStatus & "' OR 'All'='" & Me.cboStatus & "') And ([PolicyType]= '" & Me.cboPolicyType & "' )"
 

mmaybe

New member
Local time
Today, 07:29
Joined
Oct 23, 2018
Messages
4
Hi mmaybe. If I understand it correctly, you could also try the following:

Code:
cstatus = " select * from Client_Invoice_Status where ([Company]= '" & Me.cboCompany & "') And ([Status]= '" & Me.cboStatus & "' OR 'All'='" & Me.cboStatus & "') And ([PolicyType]= '" & Me.cboPolicyType & "' )"

I'm grateful to all of you but this simply what I need.
Thanks theDBguy :]
 

Dreamweaver

Well-known member
Local time
Today, 04:29
Joined
Nov 28, 2005
Messages
2,466
SELECT "*" As SaveName,"All" As SaveType FROM "same table as in other side of union" UNION SELECT Your sql FROM your tableWHERE your criteria



You will need to edit it but will do what you want


I'm not sure if this is what you really want after reading your post again as this will require code in the afterupdate event like


If Me![Combo] = "*" Then
Do something

Else
Do Something
End If
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:29
Joined
Oct 29, 2018
Messages
21,471
I'm grateful to all of you but this simply what I need.
Thanks theDBguy :]
Hi. You're welcome! We were all happy to assist. Good luck with your project.
 

Micron

AWF VIP
Local time
Yesterday, 23:29
Joined
Oct 20, 2018
Messages
3,478
Nice trick (at least that's what I'd call it).
Given that there's only one AND in the first part, maybe not problematic, but never having used that technique I'd like to ask if there are multiple AND's wouldn't it be safer to wrap the segments in parentheses to ensure correct evaluation? As in "(SELECT...WHERE...AND...AND...) OR ( ... ). Thinking that you could end up with just a single (last?) AND getting paired with the OR. This is based on my experience with poorly paired logical operators when I don't use grouping.
 

Users who are viewing this thread

Top Bottom