Form that load list ->date filter

mfaqueiroz

Registered User.
Local time
Today, 09:22
Joined
Sep 30, 2015
Messages
125
Hello,

I doing one form that shows all the machine that don't work since a certain date. The user choose trought one combo box the month and the year and after click int he button "update table", after that i want to show in a list the filter table.
As the following image.

ListUpdate.png
So i have write the code:


PHP:
Private Sub LoadTable_Click()

DoCmd.SetWarnings False
    
    Dim strsql As String
 
    Month11 = Me.Month1
    Year11 = Me.Year1
    

 strsql = "SELECT * FROM Historic WHERE year([Date])< '& year11 &' or  (year(Date))='&year11&' and month(Date)< '&Month11&' );"

Me!List.RowSource = strsql
        
Me.Refresh
    
End Sub




but isn't working, do you know where i'm falling?

thanks
 
First you shouldn't mix single and double quotes the way you are. As it is '& year 11 &' is taken to be literally '& year 11 &' rather than concatenating the value in. These need to be double quotes, Then you have Date in brackets and then not. Assuming it's a field name (Bad choice for that) it needs to be in brackets in all places to to make sure it is not confused in the date function. Then

year([Date])< '& year11 &' or (year(Date))='&year11&'

can be simplified by using the <= operator. So maybe this is what you want.

Code:
strsql = "SELECT * FROM Historic WHERE Year([Date]) <= " & year11 & " And Month([Date]) < " & Month11

Assuming the combo boxes have numbers for the months (1-12) and years (e.g. 2016)
 
Thanks SneuBerg.
It's solved :)
!!
 
Last edited:
Just a small tip for that function, you need to switch the warnings back on when you are finished so DoCmd.SetWarnings True should be placed before the End Sub otherwise they will stay switched off.
 

Users who are viewing this thread

Back
Top Bottom