如何通过组合框

时间:2017-09-18 14:15:33

标签: mysql vba ms-access combobox access-vba

这是我的SQL(我的任务是处理我没有制作表格的数据,我知道它们的名字很糟糕):

SQL = "SELECT [risk - original].IDrisk AS [Risk ID], [risk - original].Riskname AS [Risk Name], [risk - original].Description, " & _
"[risk - original].Category, [risk - original].InitialProbability AS [Probability], [risk - original].InitialSeverity AS [Severity], " & _
"[risk - original].InitialProbability * [risk - original].InitialSeverity AS [Risk Score], [risk - original].RootCause AS [Root Cause] " & _
"FROM [risk - original] " & _
"WHERE ([InitialSeverity] = " & Me.cboSeverity & ") OR ([InitialProbability] = " & Me.cboProbability & ") OR ([Category] = '" & Me.cboRiskCategory & "'))"

我可以使用WHERE子句只使用单个组合框的条件,但是当我尝试添加所有组合框字段代码失败时,现在组合框被编程为(例如):

Dim mySeverity As String
mySeverity = "Select * from [risk - original] where ([InitialSeverity] = " & Me.cboSeverity & ")"
Me.riskOriginalSubform.Form.RecordSource = mySeverity
Me.riskOriginalSubform.Form.Requery
Me.cboRiskCategory = Null
Me.cboProbability = Null

将来我希望能够按多个组合框进行排序,然后从子表单中选择数据,最后将数据导出到excel。

谢谢!

1 个答案:

答案 0 :(得分:0)

他是一个通用答案:

vbSql = "SELECT stuff " & _
        "FROM tbl " & _
        "WHERE (cmb.value = condition "

If cmb.value = condition Then
    vbSql = vbSql & "AND moreSqlconditions"
End If

vbSql = vbSql & ");"

Me.subForm.Form.RecordSource = vbSql

希望这有效吗?