We help IT Professionals succeed at work.

Access VBA syntax for report criteria

Robert Wardlow
on
Thank you in advance for your help.

I am printing a report using VBA. I need to specify 2 criteria for the report to select what I want to print.
I created the syntax the way I thought it should work but I am getting a type mismatch error.

[Property Address 1] is a text field and [DAte Placed] is a date field


Below is my syntax.

    Dim strLinkCriteria As String
    Dim stDocName as String

    stDocName = "Result Letter"

    strLinkCriteria = "[Property Address 1] =" & "'" & Me![Property Address 1] & "'" And "[Date Placed] =" & "'" & Me.[Date Placed] & "'"
   
    DoCmd.OpenReport stDocName, acViewPreview, , strLinkCriteria

Many thanks for your help

Bob
Comment
Watch Question

Michael VasilevskySolutions Architect
CERTIFIED EXPERT

Commented:
Date needs #, i.e.

strLinkCriteria = "[Property Address 1] =" & "'" & Me![Property Address 1] & "'" And "[Date Placed] =" & "#" & Me.[Date Placed] & "#"
Robert WardlowPresident

Author

Commented:
Thanks for your suggestion.

But I am still getting the type mismatch. Does the other [Date Placed] need the # too?
Michael VasilevskySolutions Architect
CERTIFIED EXPERT

Commented:
Type mismatch on this line:

strLinkCriteria = "[Property Address 1] =" & "'" & Me![Property Address 1] & "'" And "[Date Placed] =" & "#" & Me.[Date Placed] & "#"
 
right?

try: strLinkCriteria = "[Property Address 1] =" & "'" & Me![Property Address 1] & "'" And "[Date Placed] =" & "#" & Me![Date Placed] & "#"
 
Solutions Architect
CERTIFIED EXPERT
Commented:
Or:

strLinkCriteria = "[Property Address 1] ='" &  Me![Property Address 1] & "' And [Date Placed] =#" & Me![Date Placed] & "#"

Any make sure Me![Date Placed] is date format
Robert WardlowPresident

Author

Commented:
Yes, that is the line but that suggestion did not work either.
Thank you
Robert WardlowPresident

Author

Commented:
Great, that worked! Thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.