Home > front end >  Print form on current record and records after current record
Print form on current record and records after current record

Time:08-05

I have a simple SelectRecord/PrintSelected macro button to print the current record open on the form. The current record has a numeric textbox on the form named [Aantal], if [Aantal] has value 5, I want the print button to print the current record and the next 4 records after that, so 5 total.

I thought maybe something like this in the print VBA code.

Dim db As Database                  'Current database.
Dim lng As Long                     'Loop controller.
Dim rs As DAO.Recordset             'Table to append to.
Const conMaxRecords As Long = 20    'Number of records you want.

Set db = DBEngine(0)(0)
Set rs = db.OpenRecordset("Aantal")
With rs
    For lng = 1 To conMaxRecords
        .AddNew
            !CountID = lng
        .Update
    Next
End With
rs.Close
Set rs = Nothing
Set db = Nothing
MakeData = "Records created."

CodePudding user response:

For anyone that needs it, if your record has a column containing an amount. Let's say 5. And you are standing on the first record, then this will print that first record and the 4 records after that, so total 5.

This is usefull if you have multiple records created after each other as a group, so they can be printed as a group instead of printing all records or doing it one by one.

Change: AantalForm to your textbox on the form containing the amount.

Private Sub PrintFrm_Click()
On Error GoTo 0
Dim t1 As Integer
Dim t2 As Integer
Dim j As Long
Dim frm As Form
Dim pgn As Integer
pgn = Me.CurrentRecord
t1 = Nz(Me.AantalForm, 1)

If t1 > 1 Then
t2 = pgn   t1 - 1

For j = pgn To t2
    DoCmd.PrintOut acSelection
    DoCmd.RunCommand acCmdRecordsGoToNext
Next j
Exit Sub
Else
t2 = 0
DoCmd.PrintOut acSelection
Exit Sub
End If
End Sub
  • Related