Joggerboy2220
New member
- Local time
- Today, 11:32
- Joined
- Feb 27, 2024
- Messages
- 5
I have a well-functioning database, I now want to have multiple envelopes printed from a query with addresses with a logo at the top left and then a different address per envelope each time, I have written the following procedure The envelopes are printed perfectly,
but afterwards an error message appears: Set rpt = Reports("ledenbriefRap")
what am I doing wrong?
Public Sub KnopBriefAfdruk_Click()
' On Error GoTo Err_KnopBriefAfdruk_Click
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim rpt As Report
' Open the query that contains the addresses
Set db = CurrentDb
Set qdf = db.QueryDefs("ledenbrief query")
Set rst = qdf.OpenRecordset()
‘ Verify that the recordset is not empty
If rst.EOF Or rst.BOF Then
MsgBox "Geen gegevens gevonden in de query.", vbExclamation
Exit Sub
End If
' Open the report designed for the envelopes
DoCmd.OpenReport "ledenbriefRap", acViewPreview
' Go through the record set and print each envelope
Do Until rst.EOF
' Adjust the fields on the report based on the data in the recordset
Set rpt = Reports("ledenbriefRap")
rpt.Controls("txtName").ControlSource = rst!Name
rpt.Controls("txtAdres").ControlSource = rst!adres
rpt.Controls("txtZip").ControlSource = rst!zip
'Go to the next record
rst.MoveNext
Loop
' Print the envelope
DoCmd.PrintOut acPrintAll
' Close the report and recordset
DoCmd.Close acReport, "ledenbriefRap"
rst.Close
Set rst = Nothing
Set qdf = Nothing
Set db = Nothing
'Err_KnopBriefAfdruk_Click:
' MsgBox Err.Description
' Resume Exit_KnopBriefAfdruk_Click
End Sub
but afterwards an error message appears: Set rpt = Reports("ledenbriefRap")
what am I doing wrong?
Public Sub KnopBriefAfdruk_Click()
' On Error GoTo Err_KnopBriefAfdruk_Click
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim rpt As Report
' Open the query that contains the addresses
Set db = CurrentDb
Set qdf = db.QueryDefs("ledenbrief query")
Set rst = qdf.OpenRecordset()
‘ Verify that the recordset is not empty
If rst.EOF Or rst.BOF Then
MsgBox "Geen gegevens gevonden in de query.", vbExclamation
Exit Sub
End If
' Open the report designed for the envelopes
DoCmd.OpenReport "ledenbriefRap", acViewPreview
' Go through the record set and print each envelope
Do Until rst.EOF
' Adjust the fields on the report based on the data in the recordset
Set rpt = Reports("ledenbriefRap")
rpt.Controls("txtName").ControlSource = rst!Name
rpt.Controls("txtAdres").ControlSource = rst!adres
rpt.Controls("txtZip").ControlSource = rst!zip
'Go to the next record
rst.MoveNext
Loop
' Print the envelope
DoCmd.PrintOut acPrintAll
' Close the report and recordset
DoCmd.Close acReport, "ledenbriefRap"
rst.Close
Set rst = Nothing
Set qdf = Nothing
Set db = Nothing
'Err_KnopBriefAfdruk_Click:
' MsgBox Err.Description
' Resume Exit_KnopBriefAfdruk_Click
End Sub