Greetings Sam!
Here is a solution that will work when passing the table name followed by a
series of commas as place holders inbetween the desired field names you wish
to search. In other words if you only want to look at day 31 of table1 then
you would need to pass the 30 commas as place holders between table1 and
true. there is an example at the bottom on how to call this. You can copy and
paste this directly to your forms module. You should be able to take the user
input and pass it to this sub. Enjoy!
Private Sub LoadListBox(strTableName As String, _
Optional blnDay1 As Boolean = False, Optional blnDay2 As Boolean = False,
Optional blnDay3 As Boolean = False, _
Optional blnDay4 As Boolean = False, Optional blnDay5 As Boolean = False,
Optional blnDay6 As Boolean = False, _
Optional blnDay7 As Boolean = False, Optional blnDay8 As Boolean = False,
Optional blnDay9 As Boolean = False, _
Optional blnDay10 As Boolean = False, Optional blnDay11 As Boolean = False,
Optional blnDay12 As Boolean = False, _
Optional blnDay13 As Boolean = False, Optional blnDay14 As Boolean = False,
Optional blnDay15 As Boolean = False, _
Optional blnDay16 As Boolean = False, Optional blnDay17 As Boolean = False,
Optional blnDay18 As Boolean = False, _
Optional blnDay19 As Boolean = False, Optional blnDay20 As Boolean = False,
Optional blnDay21 As Boolean = False, _
Optional blnDay22 As Boolean = False, Optional blnDay23 As Boolean = False,
Optional blnDay24 As Boolean = False, _
Optional blnDay25 As Boolean = False, Optional blnDay26 As Boolean = False,
Optional blnDay27 As Boolean = False, _
Optional blnDay28 As Boolean = False, Optional blnDay29 As Boolean = False,
Optional blnDay30 As Boolean = False, _
Optional blnDay31 As Boolean = False)
Dim strSqlSelect As String
strSqlSelect = _
"SELECT " & strTableName & ".def " & _
"FROM " & strTableName & " " & _
"WHERE (((" & strTableName & ".def) Is Not Null) "
If blnDay1 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[1])= 0) "
End If
If blnDay2 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[2])= 0) "
End If
If blnDay3 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[3])= 0) "
End If
If blnDay4 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[4])= 0) "
End If
If blnDay5 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[5])= 0) "
End If
If blnDay6 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[6])= 0) "
End If
If blnDay7 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[7])= 0) "
End If
If blnDay8 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[8])= 0) "
End If
If blnDay9 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[9])= 0) "
End If
If blnDay10 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[10])= 0) "
End If
If blnDay11 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[11])= 0) "
End If
If blnDay12 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[12])= 0) "
End If
If blnDay13 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[13])= 0) "
End If
If blnDay14 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[14])= 0) "
End If
If blnDay15 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[15])= 0) "
End If
If blnDay16 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[16])= 0) "
End If
If blnDay17 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[17])= 0) "
End If
If blnDay18 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[18])= 0) "
End If
If blnDay19 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[19])= 0) "
End If
If blnDay20 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[20])= 0) "
End If
If blnDay21 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[21])= 0) "
End If
If blnDay22 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[22])= 0) "
End If
If blnDay23 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[23])= 0) "
End If
If blnDay24 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[24])= 0) "
End If
If blnDay25 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[25])= 0) "
End If
If blnDay26 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[26])= 0) "
End If
If blnDay27 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[27])= 0) "
End If
If blnDay28 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[28])= 0) "
End If
If blnDay29 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[29])= 0) "
End If
If blnDay30 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[30])= 0) "
End If
If blnDay31 = True Then
strSqlSelect = strSqlSelect & _
"AND ((" & strTableName & ".[31])= 0) "
End If
strSqlSelect = strSqlSelect & ");"
'Debug.Print strSqlSelect
'Stop
Me.list.RowSource = strSqlSelect
Me.list.Requery
End Sub
Also Note: Here is an example of how to call it.
Private Sub TestLoadListBox()
Call LoadListBox("tblMonth2", , , , , , , , , , , , True, True, True)
End Sub
This will set the forms listbox named "list", rowsource to show all of the
defs (Autonumbers) from that table where fields named 11, 12, 13 are equal to
0 (zero) now if you are looking at null values or some sort of text values
other than zeros you will need to change the code accordingly. I know it
isn't pretty but neither is this design! LOL... Take Care & God Bless -- ~
SPARKER ~
Sam said:
I have form which searches for available days from db. Db contains tables for
6 month with fields named 1-31(or 30 depends on month). In the form I put
date1 (ex. 12/06/2005) and date2 (ex.14/06/2005). The code should find which
of those days are available (table contains something else than 0).
Following picture's first line shows a case where all those days are
available(0)in table. Def-field contains autonumber.
def 12 13 14
1 0 0 0
2 1 45 0
Now I want to run simple query which returns "def" field's value to
list-control "List" in the form named "Form1" if all the days contains "0".
So in this case the list would contain 1 (see picture). The query has to be
run through VBA, because the table and field names in query cannot be fixed.
I'm not sure if it was clear enough, but you are free to ask more information.