如何将值分配给where子句中的Access列

时间:2014-04-02 10:42:36

标签: sql excel vba excel-vba

在我的项目中,我们从MS Excel连接到MS Access,在excel中我们有一个带有是或否值的下拉列表(medd)。同时,在MS Access中,我们有Med_D列,其中包含Y或N值。

因此,如果用户从下拉列表中选择“是”,那么它应该获取Med_D = Y的MS Access行。 如果用户从下拉列表中选择“否”,那么它应该获取Med_D('Y','N')中的MS Access行。

我想在一个查询中检查这个条件,我们不能在Access中使用CASE,尝试过IIF&切换但我失败了。

3 个答案:

答案 0 :(得分:0)

使用它:

Select * from "Table Name" Where Med_D = "xxxxx" 

xxxxx是Y和N

的下拉列表

我在猜你使用ADODB连接?

答案 1 :(得分:0)

这里有一个更详细的代码视图:     Sub Extract()

Blah = yourdropdown.value

Set Con1 = New ADODB.Connection
Set RcdSet1 = New ADODB.Recordset

Application.ScreenUpdating = False

DBPath = "" <<<path of your access database
DBProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
DBParam = DBProvider & "data source =" & DBPath

Con1.Open DBParam
RcdSet1.ActiveConnection = Con1
RcdSet1.Open "Select * from "Table Name" Where Med_D = "Blah""

rcdset1.movefirst
Range("xx").copyfromrecordset rcdset1   <<< where you will paste the records to
rcdset1.close
set rcdset1 = Nothing

end sub

玩得开心=)

答案 2 :(得分:0)

Sub MakeDynamicSQL()

Const SQL As String =“select * from YourAccessTable”

Dim sWhereClause As String

Select Case Cells(1, 1).Text ' cell A1 contains Y or N

    Case "Y": sWhereClause = " where Med_D = 'Y'"

    Case "N": sWhereClause = " where Med_D in ('Y','N')"

    Case Else: MsgBox "error": Exit Sub

End Select

Debug.Print SQL & sWhereClause ' send this to Access

End Sub