2015-12-02 70 views
1

我正在尝试创建一个用户窗体 - 其中包含不同年份的复选框和用于不同查询的命令按钮。因此,例如,如果用户选择三个复选框-1990,1993,1995,然后用户点击特定查询。然后,该查询必须与当年进入执行的“,其中该查询的一部分” 这是我的代码: -用于在VBA(userform-excel)中输入sql查询的复选框

*

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 

以上是我的代码和问题在我的最后评论

+0

感谢您的回复“barrowc”。现在,我可以在查询中使用varianles。我还有几个问题:1.如果我点击所有年份,则以下代码有效,但是如果我选择其中任何一个,则不会是2。如果[姓名]是“科学院”。在执行查询时,它会在'of'附近显示语法错误3.我希望它可以用于任何复选框的检查 – Ashish

+0

我已用更多代码更新了我的答案,并将答案中的代码编辑为问题 – barrowc

回答

0

发布它的那样简单...and d.Year=" & CheckBox6.Caption & " group by...

如果你想使用变量的值,那么它必须在双引号之外。然后,您可以使用&操作员将这些部分重新连接在一起。

但是:通过将字符串连接在一起构造一个来自用户输入的SQL查询是很麻烦的。 SQL注入是一个非常现实的可能性 - 看herehere

您可以结合使用ADODB Command objectParameters collection以安全的方式来执行paramaterized查询。见this answer对于如何证明这样做


编辑:如果某些值是文本字符串,就应该用单引号是这样的:

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

*如果CheckBox1.Value = True Then y1 = CheckBox1.Caption i = i + 1 End If If CheckBox2.Value = True Then – Ashish