Code:
Private Sub Cmdletter_Click()On Error GoTo Err_Cmdletter_Click
Dim rs As dao.Recordset
Dim qdf As QueryDef
Dim dbs As dao.Database
Dim startdate As Date
Dim enddate As Date
Dim DateRange As String
startdate = Me.Text2.Value
enddate = Me.Text4.Value
DateRange = "item_donations.date >= #" & startdate & "# AND item_donations.date <= #" & enddate & "#"
If IsNull(Me.Text2.Value) Then
MsgBox " Please enter the range of dates for the report", vbOKOnly, "Missing dates"
Me.Text2.SetFocus
ElseIf IsNull(Me.Text4.Value) Then
MsgBox " Please enter the range of dates for the report", vbOKOnly, "Missing dates"
Me.Text4.SetFocus
Else:
Set dbs = CurrentDb
strsql = "SELECT Org_zip, Org_name, Org_phone, Org_CEO, Org_fax, Org_website, logopath, Org_address1, Org_address2, Org_city, Org_state, " & _
"Contact_Info.Salutation, Contact_Info.Spouse_First_Name, Contact_Info.Display_Name, Contact_Info.ID_numberPK, Contact_Info.Last_Name, Contact_Info.First_Name, Contact_Info.Phone1," & _
"Contact_Info.Contact_Name, Contact_Info.Address1, Contact_Info.Address2, Contact_Info.City, Contact_Info.State, Contact_Info.Zip_Code," & _
"Item_Donations.Date, Item_donations.ID_number_fk, Item_Donations.Amount, Item_Donations.[Item type], Item_Donations.[Approximate Cash Value], Item_donations.[restricted use]" & _
"FROM Files_settings, Organization_info, Contact_Info INNER JOIN Item_Donations ON Contact_Info.ID_numberPK = Item_Donations.ID_number_FK " & _
"WHERE item_donations.date >= #" & startdate & "# AND item_donations.date <= #" & enddate & "# ; "
Set qdf = dbs.CreateQueryDef("Thank_you_letter_query", strsql)
Set rs = qdf.OpenRecordset()
End If
rptname = "Tax info letter"
prnttype = "Document"
DoCmd.OpenReport rptname, acViewPreview, "Thank_You_letter_Query", DateRange, acWindowNormal
Set rpt = Reports(rptname)
Set rpt.Printer = Application.Printers(Selprnt(prnttype))
rpt.Printer.Orientation = acPRORPortrait
Me.Refresh
DoCmd.Close acQuery, "Thank_You_letter_query", acSaveNo
dbs.QueryDefs.Delete ("Thank_You_letter_query")
Set rpt = Nothing
Application.Printer = Nothing
Set qdf = Nothing
Set rs = Nothing
Exit_Err_Cmdletter_Click:
Exit Sub
Err_Cmdletter_Click:
MsgBox Err.Description
Resume Exit_Err_Cmdletter_Click
End Sub
Thanks