Complie error: Variable not defined

Hello Experts:  

When I try opening my report I get this error:  Variable not defined on the 6th line at dbOpenSnapshot.  Also the first line is highlighted in yellow.  Here is the code example.

Private Sub Report_Open(Cancel As Integer)
Dim db As Database
Dim rs As Recordset
  Set db = CurrentDb
  Set rs = db.OpenRecordset("SELECT Min([Start Date]) AS MinOfStartDate " _
                & " FROM Projects", dbOpenSnapshot)
  If rs.RecordCount > 0 Then
    mdatEarliest = rs!MinOfStartDate
  End If
  Set rs = db.OpenRecordset("SELECT Max(IIf(IsDate([End Date]),CDate([End Date]),Null)) " _
                & "AS MaxOfEndDate FROM Projects", dbOpenSnapshot)
  If rs.RecordCount > 0 Then
    mdatLatest = rs!MaxOfEndDate
  End If
 
  mintDayDiff = DateDiff("d", mdatEarliest, mdatLatest)
 
  Me.txtMinStartDate.Caption = Format(mdatEarliest, "mm/dd/yyyy")
  Me.txtMaxEndDate.Caption = Format(mdatLatest, "mm/dd/yyyy")
  Set rs = Nothing
  Set db = Nothing
End Sub

Thanks.
SOUTHAMERICA70Asked:
Who is Participating?
 
Patrick MatthewsConnect With a Mentor Commented:
Change your declarations to:

Dim db As DAO.Database
Dim rs As DAO.Recordset
0
 
Patrick MatthewsCommented:
Have you checked your references to see if any are listed as missing?  Do you have a reference to DAO?
0
 
carazuulCommented:
Hi SOUTAMERICA70,
You may have trouble with mdatEarliest, mdatLatest, and mintDayDiff if you are using Option Explicit at the top of the code page. Also, if txtMinStartDate and txtMaxEndDate are text boxes as the naming implies you'll need to change the value instead of the caption.
Regards,
carazuul
0
 
SOUTHAMERICA70Author Commented:
matthewspatrick:

Thanks,
I added the DAO.  But now I get a runtime error:  Type mismatch on the 4th line Set db = CurrentDb
0
 
SOUTHAMERICA70Author Commented:
matthewspatrick:

Thank you very much for your help.  That fixed my problem.  Many Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.