我有点卡在这里。我有以下代码:
` Private Sub Command88_Click()
Dim filterString As String
Dim startDate As String
Dim endDate As String
startDate = txtStartDate
endDate = txtEndDate
selectedfield = CboxSelectField.Value
filterString = "[HW End of Support] BETWEEN #" & Format(startDate, "MM-DD-YYYY") & "# And #" & Format(endDate, "MM-DD-YYYY") & "#"
Me.Filter = filterString
Me.FilterOn = True
End Sub`
只要要过滤的列的值保持硬编码即[HW End of Support]
,过滤器就会正常运行,但我有一个名为CboxSelectField
的组合框,它有4个不同的列名,我需要的是根据从组合框中选择的列过滤数据?
如果我将[HW End of Support]
替换为包含组合框选择值的selectedField
变量,则代码会中断并且不会过滤。
答案 0 :(得分:1)
看起来您尚未定义"selectedfield"
- 您应该将其定义为string
,然后将其设置为"[" & CboxSelectField.Value & "]"
,以使其按照您尝试的方式运行此刻。
所以你的编码看起来像这样:
Private Sub Command88_Click()
Dim startDate As String, endDate As String
Dim selectedfield As String, filterString As String
startDate = txtStartDate
endDate = txtEndDate
selectedfield = "[" & CboxSelectField.Value & "]"
filterString = selectedfield & " BETWEEN #" & _
Format(startDate, "MM-DD-YYYY") & "# And #" & _
Format(endDate, "MM-DD-YYYY") & "#"
Me.Filter = filterString
Me.FilterOn = True
End Sub
答案 1 :(得分:0)
这有效:
filterString = "" & "[" & selectedField & "]" & " BETWEEN #" & Format(startDate, "MM-DD-YYYY") & "# And #" & Format(endDate, "MM-DD-YYYY") & "#"
答案 2 :(得分:-2)
尝试:
filterString = "[" & selectedfield & "] BETWEEN #" & Format(startDate, "MM-DD-YYYY") & "# And #" & Format(endDate, "MM-DD-YYYY") & "#"
这将输出字段名称"我的字段":
[My Field] BETWEEN #04-25-2018# And #04-25-2018#
您的扩展表达式将返回:
[My Field] BETWEEN #04-25-2018# And #04-25-2018#