我遇到了以下问题。 在我的表中有一个名为" datum"该字段来自日期类型。这个领域是欧洲的 格式(年/月/日) 我还有一个popupform,有两个名为" begindatum"和" einddatum" 当我在popupform中填写日期时,我只想看到我在弹出窗体中选择的子窗体中的记录
到目前为止,这是我的代码,
CODE
Private Sub cmdFilterAan_Click()
Dim strFilter As String
If begindatum <> "" Then
strFilter = strFilter & " And begindatum=#" & Format(CDate(Me.begindatum), "dd/mm/yyyy") & "#"
End If
If einddatum <> "" Then
strFilter = strFilter & " And einddatum=#" & Format(CDate(Me.einddatum), "dd/mm/yyyy") & "#"
End If
If naam <> "" Then
strFilter = strFilter & " And naam=" & Me.naam
End If
If beroep <> "" Then
strFilter = strFilter & " And beroep=" & Me.beroep
End If
If strFilter <> "" Then
strFilter = Mid(strFilter, 6)
Forms!frmData.FilterOn = True
Forms!frmData.Filter = strFilter
End If
End Sub
strFilter for&#34; naam&#34;和&#34; beroep&#34;工作正常,但对于日期字段不是。 我想我正在寻找VBA代码中的BETWEEN语句,该怎么做我不知道。
当我在弹出窗口中填写日期时,没有任何事情发生,事实上是记录 在子表单中所有disapears
任何人都可以帮我解决这个问题。 thx提前 问候,ahc59
答案 0 :(得分:2)
尝试以mm / dd / yyyy格式传递日期。过滤器似乎特别关于使用该格式。所以,例如:
If begindatum <> "" Then
strFilter = strFilter & " And begindatum=#" & Format(CDate(Me.begindatum), "mm/dd/yyyy") & "#"
End If
If einddatum <> "" Then
strFilter = strFilter & " And einddatum=#" & Format(CDate(Me.einddatum), "mm/dd/yyyy") & "#"
End If
如果你想在日期之间做,你可以这样做:
If begindatum <> "" And einddatum <> "" Then
strFilter = strFilter & " And begindatum >= #" & Format(CDate(Me.begindatum), "mm/dd/yyyy") & "# And einddatum <= #" & Format(CDate(Me.einddatum), "mm/dd/yyyy") & "#"
End If
您不能在此使用BETWEEN,因为日期分为两个不同的字段。
答案 1 :(得分:1)
不幸的是在核心版本中,vba使用美国格式(“mm / dd / yyyy”),我在我的表单上用来过滤这段代码的日期,其中[Data]是表格中的一个字段:
Function Today()
Dim dDate As String
dDate = Format(Date, "mm/dd/yyyy")
strFilter2 = "[Data] = #" & dDate & "#"
Forms!MainForm!SubForm.Form.Filter = strFilter2
Forms!MainForm!SubForm.Form.FilterOn = True
End Function
Function ThisWeek()
dDate = Format(Date - 2, "mm/dd/yyyy")
dDate2 = Weekday(dDate)
dDate3 = Date - dDate2
dDate4 = Format(Date - 1, "mm/dd/yyyy")
dDate5 = Weekday(dDate4)
dDate6 = 7 - dDate5
dDate7 = Date + dDate6
strFilter3 = "[Data] between #" & dDate3 & "# AND #" & dDate7 & "#"
Me.Refresh
Forms!MainForm!SubForm.Form.Filter = strFilter3
Forms!MainForm!SubForm.Form.FilterOn = True
End Function
Function ThisMonth()
dDate = Date - Day(Date) + 1
dDate2 = DateSerial(Year(Date), Month(Date) + 1, 0)
dDate3 = Format(dDate, "mm/dd/yyyy")
dDate4 = Format(dDate2, "mm/dd/yyyy")
strFilter3 = "[Data] between #" & dDate3 & "# AND #" & dDate4 & "#"
Me.Refresh
Forms!MainForm!SubForm.Form.Filter = strFilter3
Forms!MainForm!SubForm.Form.FilterOn = True
End Function
Function ThisYear()
dDate = "1/1/" & Year(Date)
dDate2 = "31/12/ " & Year(Date)
dDate3 = Format(dDate, "mm/dd/yyyy")
dDate4 = Format(dDate2, "mm/dd/yyyy")
strFilter3 = "[Data] between #" & dDate3 & "# AND #" & dDate4 & "#"
Me.Refresh
Forms!MainForm!SubForm.Form.Filter = strFilter3
Forms!MainForm!SubForm.Form.FilterOn = True
End Function
Function All()
'If you have default to show them all
Forms!MainForm!SubForm.Form.FilterOn = False
End Function