简体   繁体   中英

Wrong Date Filter Applying in VBA Access Subform

I am having a little bit of a problem while applying a filter on date field to my sub form, my date and time on the table/form and PC are formatted as dd/mm/yyyy .I have a function that is called while pressing a button. My code looks like this :

Function ThisMonth()
Dim strFilter3 As String
Dim dDate as String
dDate = Format(Date - Day(Date) + 1, "dd/mm/yyyy")
strFilter3 = "[Data] >  #" & dDate & "#"

' Or I am trying to apply it like this , I tried to see what I get from dDate and dDate2 with MsgBox.
' SECOND OPTION :

'dDate = "01/" & Month(Date) & "/" & Year(Date)   
'dDate2 = Format(dDate, "dd/mm/yyyy")
'strFilter3 = "[Data] >  #" & dDate2 & "#"

Forms!frmLogistica!sfrmLogistica.Form.Filter = strFilter3
Forms!frmLogistica!sfrmLogistica.Form.FilterOn = True

'Date return the date of today 27/11/2015 while Day(date) return the day 27
End Function

When the filter is applied with any of the both options , as i see the filter applyes like this : > 11/01/2015 [mm/dd/yyyy] insteed of 11/01/2015 [dd/mm/yyyy].

I tried to declare dDate & dDate2 as String or Date but nothing changes.... I hope I was explicit with the details , I things is something very easy about formatting , I read a lot of things but cannot find the right option for it .... I am going to try to get the value from a Text Box and see what I figure it out with a command like this :

strFilter3 = "[Data] > #" & Me.txtASD & "#"

In the end I want to make a filter on a subform for Today/This Week/This Month/All Time . My Today function works well and it looks like this :

Function Today()

Dim dDate As String    
dDate = Format(Date, "dd/mm/yyyy")    
strFilter2 = "[Data] = #" & dDate & "#" 
Forms!frmLogistica!sfrmLogistica.Form.Filter = strFilter2
Forms!frmLogistica!sfrmLogistica.Form.FilterOn = True

End Function

I found what was the problem and now it works, had to change Format(dDate, "mm/dd/yyyy") and make a bracket with BETWEEN & AND. I finally made the functions work as i want them to , i post the code hope hepls somebody :D

frmLogistica - MainForm

sfrmLogistica - SubForm

Forms!MainForm!SubForm.Form.Filter it can be replaced with Me.SubForm.Form.Filter

Function Today()

Dim dDate As String



dDate = Format(Date, "dd/mm/yyyy")


strFilter2 = "[Data] = #" & dDate & "#"


Forms!frmLogistica!sfrmLogistica.Form.Filter = strFilter2
Forms!frmLogistica!sfrmLogistica.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!frmLogistica!sfrmLogistica.Form.Filter = strFilter3

Forms!frmLogistica!sfrmLogistica.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!frmLogistica!sfrmLogistica.Form.Filter = strFilter3

Forms!frmLogistica!sfrmLogistica.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!frmLogistica!sfrmLogistica.Form.Filter = strFilter3

Forms!frmLogistica!sfrmLogistica.Form.FilterOn = True

End Function




Function All()

Forms!frmLogistica!sfrmLogistica.Form.FilterOn = False


End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM