iT邦幫忙

0

關於 Access 以日期和公司名稱篩選表單資料

你好~ 想問一下知道怎麼可以以日期和公司名稱篩選表單資料嗎?

我之前有看過網上以VBA程式碼分析,

但是一直無法篩選資料, 可以幫忙看一下哪裡出錯了?

謝謝!

https://ithelp.ithome.com.tw/upload/images/20210122/20134604II2llfmMJO.pnghttps://ithelp.ithome.com.tw/upload/images/20210122/20134604u5UUKup0aM.png
Option Compare Database

Private Sub 開始日期_Click()
開始日期.Value = Date
End Sub
Private Sub 結束日期_Click()
結束日期.Value = Date
End Sub

Private Sub 按鈕結束日期為今天_Click()
結束日期.Value = Date '取今天日期
End Sub

Private Sub 按鈕開始日期為今天_Click()
開始日期.Value = Date '取今天日期
End Sub
Private Sub 公司_AfterUpdate()
公司.Value = [物流承辦商]
End Sub
Private Sub 清除內容_Click()
開始日期.Value = Null
結束日期.Value = Null
公司.Value = Null
End Sub

Private Sub 查詢_Click()
Dim str
str = ""
If IsNull(開始日期) = False Then
str = str & "[寄出日期]>= #" & 開始日期 & "#"
End If
If IsNull(結束日期) = False Then
str = str & "[寄出日期]<= #" & 結束日期 & "#"
End If
If 公司.Value <> "" Then
If str <> "" Then str = str & "And"
str = str & "[物流承辦商]="" & 公司.Value &"""
End If

Me![所有訂單資料查詢].Form.Filter = str
Me![所有訂單資料查詢].Form.FilterOn = True
Me![所有訂單資料查詢].Form.Requery

End Sub

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2021-01-22 18:03:58
最佳解答
Dim str
str = ""
If IsNull(開始日期) = False Then
    str = str & "[寄出日期]>= #" & 開始日期 & "#"
End If
If IsNull(結束日期) = False Then
    str = str & "[寄出日期]<= #" & 結束日期 & "#"
End If
If 公司.Value <> "" Then
    If str <> "" Then str = str & "And"
    str = str & "[物流承辦商]="" & 公司.Value &"""
End If

改成

Dim str
str = ""
If IsNull(開始日期) = False Then
    If str <> "" Then
        str = str & " AND [寄出日期]>= #" & 開始日期 & "#"
    Else
        str = str & "[寄出日期]>= #" & 開始日期 & "#"
    End If
End If
If IsNull(結束日期) = False Then
    If str <> "" Then
        str = str & " AND [寄出日期]<= #" & 結束日期 & "#"
    Else
        str = str & "[寄出日期]<= #" & 結束日期 & "#"
    End If
End If
If 公司.Value <> "" Then
    If str <> "" Then
        str = str & " AND [物流承辦商]="" & 公司.Value &"""
    Else
        str = str & "[物流承辦商]="" & 公司.Value &"""
    End If
End If

試試看

根本的解決方法
是把 str 的內容顯示出來看
最可以確定程式寫對還寫錯

c1204994 iT邦新手 5 級 ‧ 2021-01-23 12:41:58 檢舉

謝謝你的幫忙! 但我的公司.Value的資料來源不知道是不是做錯了, 值一直重複,而且點選不到,篩選不了。https://ithelp.ithome.com.tw/upload/images/20210123/20134604wFFQXvLvNR.pnghttps://ithelp.ithome.com.tw/upload/images/20210123/20134604DZ97McblZp.pnghttps://ithelp.ithome.com.tw/upload/images/20210123/20134604PYax8wShwx.png

這篇有點久了
還是可以參考看看...

上面第一張圖裡有「唯一值」的屬性值
把他改成「是」試試看

我要發表回答

立即登入回答