|
|
Question : Report Based on Multiple Filters!
|
|
Hi I am a newbie to MS Access and am using MSAccess97. I have a table with 8 fields giving Vehicle details. I have created a form with four fields which are combo boxes pulling data from the table itself. I want to generate a report from the choice a user makes on selecting values in these comboboxes. The tricky bit is a user may choose not to enter any value which should then give all records. Can somebody help me please?!
|
Answer : Report Based on Multiple Filters!
|
|
The following code should work if you have four comboboxes with names combo1,combo2,combo3 and combo4
Dim ComVal As Boolean Dim sql As String
sql = "select * from tablename where "
ComVal = False
If Not IsNull(Combo1.Value) Then sql = sql & " field1 = ' " & Combo1.Value & " '" ComVal = True End If If Not IsNull(Combo2.Value) And ComVal Then sql = sql & " and " & " field2 = " & Combo2.Value ElseIf Not IsNull(Combo2.Value) And Not ComVal Then sql = sql & " field2 = " & Combo2.Value ComVal = True End If If Not IsNull(Combo3.Value) And ComVal Then sql = sql & " and " & " field3 = " & Combo3.Value ElseIf Not IsNull(Combo3.Value) And Not ComVal Then sql = sql & " field3 = " & Combo3.Value ComVal = True End If If Not IsNull(Combo4.Value) And ComVal Then sql = sql & " and " & " field4 = " & Combo4.Value ElseIf Not IsNull(Combo4.Value) And Not ComVal Then sql = sql & " field4 = " & Combo4.Value ComVal = True End If If ComVal = False Then sql = "select * from tablename" End If
Dim dB As Database Dim rS As Recordset
Set dB = CurrentDb Set rS = dB.OpenRecordset(sql, dbOpenDynaset) If Not rS.EOF Then rS.MoveLast rS.MoveFirst End If
MsgBox rS.RecordCount take the recordset and display the values as reqd.
This should work based on the values in each of the combo boxes.
If all the values are null then it selects all records otherwise based on the values in the combo boxes.
replace tablename and the fielnames with your tablename and fieldnames appropriately.
|
|
|
|