2015-12-02 71 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