SOUTHAMERICA70
asked on
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.
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
Me.txtMaxEndDate.Caption = Format(mdatLatest, "mm/dd/yyyy")
Set rs = Nothing
Set db = Nothing
End Sub
Thanks.
Have you checked your references to see if any are listed as missing? Do you have a reference to DAO?
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
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
ASKER
matthewspatrick:
Thanks,
I added the DAO. But now I get a runtime error: Type mismatch on the 4th line Set db = CurrentDb
Thanks,
I added the DAO. But now I get a runtime error: Type mismatch on the 4th line Set db = CurrentDb
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
matthewspatrick:
Thank you very much for your help. That fixed my problem. Many Thanks!
Thank you very much for your help. That fixed my problem. Many Thanks!