lundi 1 août 2016

Using checkboxes values in SQL "IN"-operator

I have a form with several checkboxes in it. Values of ticking checkboxes are used after in the SQL-query including in Excel-macro. I use these values in SQL "IN"-operator. So, everythig works. But I don't like the code for my macro.

For ticking checkboxes I use such code (and if there were more value the list would be very huge):

Public Location1 As String
Public Location2 As String
Public Location3 As String
Public Location4 As String

Private Sub OKCommandButton2_Click()
If CheckBox1.Value = True Then Location1 = "LocationValue1"
If CheckBox2.Value = True Then Location2 = "LocationValue2"
If CheckBox3.Value = True Then Location3 = "LocationValue3"
If CheckBox4.Value = True Then Location4 = "LocationValue4"
...

And for using it in SQl I use such code:

query = "SELECT Param1, Param2, Param3, Param4, 0, 0, Param5, 0 FROM Table1 " & _
"WHERE Param1 like'" & "%" & CraftDefinition.Craft & "%" & "'AND Param6>0 AND Param2 IN ('" & _
LocationDefinition.Location1 & "','" & LocationDefinition.Location2 & "','" & LocationDefinition.Location3 & "','" & _
LocationDefinition.Location4 & "')" & _
"ORDER BY Param2, Param3"

The question is: can I rewrite my code in more compact, laconic and sophisticated way? Maybe I should use another operator in SQL-part; maybe I can rewrite my VBA-part, for using only one parameter in SQl.

Thank you.




Aucun commentaire:

Enregistrer un commentaire