1

I am trying to create a userform - which will have checkboxes for different years and command buttons for different queries. So if for example a user choses three checkboxes -1990,1993, 1995 and then the user clicks on a particular query. Then that query must be executed with that year in going into the "where part of that query" This is my code :-

*

  Private Sub CommandButton1_Click()
    Dim connection As New ADODB.connection
    Dim rst As New Recordset
    Dim strConnectionStr As String
    Dim Qry As String
    strConnectionStr = "Provider=SQLOLEDB;" & "DATA SOURCE=" & _
                      "INITIAL CATALOG=;" & _
                      "UID=; PWD="
    Qry = "  SELECT d.Year,d.[University name],d.[School name], COUNT(distinct d.Title) 'Number of paper published'from [dbo].[Ashish$] d where [Business/Non-business]='1'group by d.Year,d.[University name],d.[School name]  order by d.[Year], [Number of paper published] desc;"

    ActiveSheet.Cells.ClearContents
    connection.Open strConnectionStr
    rst.Open Qry, connection
    For iCols = 0 To rst.Fields.Count - 1
        Sheets("Query1").Cells(1, iCols + 1).Value = rst.Fields(iCols).Name
    Next
    Sheets("Query1").Range("A2").CopyFromRecordset rst*`


    rst.Close
    connection.Close

    End Sub


Above is the code for normal command buttons without check box . Below is the code for taking user inputs by checkboxes ...

    Private Sub CommandButton1_Click()
    Dim connection As New ADODB.connection
    Dim rst As New Recordset
    Dim strConnectionStr As String
    Dim Qry As String
    Dim ctl As Control
    Dim i As Integer
    i = 0
    strConnectionStr = "Provider=SQLOLEDB;" & "DATA SOURCE=;" & _
                      "INITIAL CATALOG=;" & _
                      "UID=; PWD="
    If CheckBox6.Value = True Then
       year1 = CheckBox6.Caption
       i = i + 1
    End If
    If CheckBox5.Value = True Then
       year2 = CheckBox5.Caption
       i = i + 1
    End If

    Qry = "  SELECT d.Year,d.[University name],d.[School name], COUNT(distinct d.Title) 'Number of paper published'from [dbo].[Ashish$] d where [Business/Non-business]='1'  and d.Year=CheckBox6.Caption  group by d.Year,d.[University name],d.[School name]  order by d.[Year], [Number of paper published] desc;"

    ActiveSheet.Cells.ClearContents

    connection.Open strConnectionStr

    rst.Open Qry, connection

    For iCols = 0 To rst.Fields.Count - 1
        Sheets("Query1").Cells(1, iCols + 1).Value = rst.Fields(iCols).Name
    Next

    Sheets("Query1").Range("A2").CopyFromRecordset rst


    rst.Close
    connection.Close
    End Sub

ub

Basically I am unable to take values from checkboxes and use them inside query statement for the purpose.

I need help . Can anyone guide me on this?


edit: the following was originally posted as an answer:

If CheckBox1.Value = True Then
 y1 = CheckBox1.Caption
 i = i + 1
End If
If CheckBox2.Value = True Then
 y2 = CheckBox2.Caption
 i = i + 1
End If

If CheckBox3.Value = True Then
x1 = CheckBox3.Caption
j = j + 1
End If

If CheckBox4.Value = True Then
x2 = CheckBox4.Caption
j = j + 1
End If

If CheckBox5.Value = True Then
x3 = CheckBox5.Caption
j = j + 1
End If

If i = 0 Then
MsgBox "Select at least one year "
End If

If j = 0 Then
MsgBox "Select at least one journal "
End If


strConnectionStr = "Provider=SQLOLEDB;" & "DATA SOURCE=;" & _
                  "INITIAL CATALOG=;" & _
                  "UID=; PWD="
Qry = "  SELECT d.Year,d.[University name],d.[School name], COUNT(distinct d.Title) 'Number of paper published'from [dbo].[Ashish$] d where [Business/Non-business]='1' and "
Qry = Qry & "[Year] IN (" & y1 & "," & y2 & ") and [Name] IN  (" & x3 & "," & x4 & ") & vbCrLf"
Qry = Qry & "group by d.Year,d.[University name],d.[School name]  order by d.[Year], [Number of paper published] desc;"

ActiveSheet.Cells.ClearContents

connection.Open strConnectionStr

rst.Open Qry, connection

For iCols = 0 To rst.Fields.Count - 1
    Sheets("Query1").Cells(1, iCols + 1).Value = rst.Fields(iCols).Name
Next

Sheets("Query1").Range("A2").CopyFromRecordset rst


rst.Close
connection.Close
End Sub

Above is my code and the question is posted in my last comment

barrowc
  • 10,444
  • 1
  • 40
  • 53
Ashish
  • 11
  • 4
  • Thanks for the response "barrowc". Now, I am able to use varianles in query. I have few more issues :- 1. If I click all years then the following code works but if I select any one of them, it doesn't 2. if [name] is "Academy of Science". On executing query, it displays syntax error near 'of' 3. I want that it should work for any checkbox checked – Ashish Dec 03 '15 at 20:36
  • I've updated my answer with more code and edited the code from your answer into the question – barrowc Dec 04 '15 at 00:16

1 Answers1

0

It's as simple as ...and d.Year=" & CheckBox6.Caption & " group by...

If you want to use the value of a variable then it has to be outside of the double quotes. You can then use the & operator to join the pieces back together.

BUT: constructing an SQL query from user input by joining strings together is asking for trouble. SQL injection can be a very real possibility - see here and here

You can use an ADODB Command object in conjunction with the Parameters collection to execute a paramaterized query in a safe manner. See this answer for a demonstration of how to do so


edit: if some of the values are text strings then they should be enclosed in single quotes like this:

and [Name] IN ('" & x3 & "','" & x4 & "')

which will produce output like this:

and [Name] IN ('Academy of Science','Foo')

If only some of the variables might have values then you need to construct the IN clause differently:

Dim yearsChosen As String

If CheckBox1.Value = True Then
    yearsChosen = yearsChosen & "," & CheckBox1.Caption
    i = i + 1
End If
If CheckBox2.Value = True Then
    yearsChosen = yearsChosen & "," & CheckBox2.Caption
    i = i + 1
End If

' Check to see if anything was selected - if it was, remove the
' leading comma and add the AND [Year] IN part
If Len(yearsChosen <> 0) Then
    yearsChosen = " AND [Year] IN (" & Mid$(yearsChosen, 2) & ")"
End If

The Names part would work similarly except you need single quotes around the values:

namesChosen = namesChosen & ",'" & CheckBox5.Caption & "'"

Building the SQL query is quite simple:

Qry = "  SELECT d.Year,d.[University name],d.[School name], COUNT(distinct d.Title) 'Number of paper published'from [dbo].[Ashish$] d where [Business/Non-business]='1' "
Qry = Qry & yearsChosen & namesChosen & vbCrLf
Qry = Qry & "group by d.Year,d.[University name],d.[School name]  order by d.[Year], [Number of paper published] desc;"
Community
  • 1
  • 1
barrowc
  • 10,444
  • 1
  • 40
  • 53
  • *If CheckBox1.Value = True Then y1 = CheckBox1.Caption i = i + 1 End If If CheckBox2.Value = True Then – Ashish Dec 03 '15 at 21:27