繁体   English   中英

用于在VBA中为SQL查询输入输入的复选框(userform-excel)

[英]Checkboxes for taking input for a sql query in VBA (userform-excel)

我正在尝试创建一个用户窗体-它将具有不同年份的复选框和不同查询的命令按钮。 因此,例如,如果用户选择了三个复选框-1990、1993、1995,然后用户单击了特定查询。 然后,该查询必须在当年进入“ 何处是查询的一部分”来执行这是我的代码: -

*

  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

基本上,我无法从复选框中获取值并将其用于查询语句中。

我需要帮助 。 有人可以指导我吗?


编辑:以下内容最初是作为答案发布的:

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

上面是我的代码,问题在我的最后评论中发布

就像...and d.Year=" & CheckBox6.Caption & " group by...

如果要使用变量的值,则它必须在双引号之外。 然后,您可以使用&运算符将各个部分重新结合在一起。

但是:通过将字符串连接在一起从用户输入构造SQL查询会带来麻烦。 SQL注入是一种非常现实的可能性-请在此处此处查看

可以将ADODB Command对象Parameters集合结合使用,以安全的方式执行参数化查询。 请参阅此答案以演示如何执行此操作


编辑:如果某些值是文本字符串,则应将它们括在单引号中,如下所示:

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

这将产生如下输出:

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

如果仅某些变量可能具有值,那么您需要以不同的方式构造IN子句:

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

名称部分的工作原理类似,不同之处在于您需要在值两边加上单引号:

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

建立SQL查询非常简单:

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;"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM