Question : How to filter between two dates when opening and Access form.

Hi,

I am trying to open a form from a label on another form.  The form that I am trying to open will currently display data from a linked sql table.  I want to apply a filter when I open the form from the label.  The code below is what I am currently trying but get "An expression you entered is the wrong data type for one of the arguments".  The filter I am applying is on a field called datelogged (format short date dd/mm/yyyy).

Code used:
Private Sub lblJan_Click()
            Call Jan_Click
End Sub
Private Sub Jan_Click()
On Error GoTo Err_Goto_Jan_form_Click

    Dim stDocName As String
    Dim StartDate As String
    Dim EndDate As String
    Dim JanSDate As Date
    Dim JanEDate As Date
   
   
    JanSDate = CDate("01/01/" & CStr(Year(Now())))
    StartDate = Format(JanSDate, "dd/mm/yyyy")
    JanEDate = CDate("31/01/" & CStr(Year(Now())))
    EndDate = Format(JanEDate, "dd/mm/yyyy")
   
    stDocName = "frmSupportIssuesList"
    'DoCmd.OpenForm stDocName, , , "DateLogged=" & CStr(Year(Now()))
    DoCmd.OpenForm stDocName, , , " = .DateLogged >= #" & CStr(CDate(StartDate)) & "# AND .DateLogged <= #" & CStr(CDate(EndDate)) & "#"
   

Exit_Goto_Jan_form_Click:
    Exit Sub

Err_Goto_Jan_form_Click:
    MsgBox Err.Description
    Resume Exit_Goto_Jan_form_Click
End Sub

Any help would be greatly appreciated.

Ian

Answer : How to filter between two dates when opening and Access form.


(What is the "." before the Field Name?)
Try:

DoCmd.OpenForm stDocName, , , "DateLogged Between " & "#" & CStr(CDate(StartDate)) & "#" & " AND " &  "#" & CStr(CDate(EndDate)) & "#"

Make sure that your criteria evaluates to something like this, ...which works fine form me:
DoCmd.OpenForm "frmOrders", , , "OrderDate Between " & "#1/1/1995#" & " AND " & "#1/1/1996#"
   

;-)

JeffCoachman
Random Solutions  
 
programming4us programming4us