I'm in need of giving autonomy to people regarding the printing of labels without the need of having to use Excel cells as an input. In other words people will just have to open an access database and use a form to print from a design gerenated report.
As it stands, I have a query view named SSCC_Gen, which displays a temporary table that will store the labels for printing, a form named Imp_Etiq_SCC and a report named Labels_SSCC_Gen
Sample of SSCC_Gen query view
Sample of Imp_Etiq_SCC Form
Sample of Labels_SSCC_Gen Report
Now, the purpose of the Imp_Etiq_SCC Form is to give the user the ability to print a certain number of labels (that will be generated before printing them), after the user selects the printer where the labels will be print and types in the number of labels to print.
Here's the code for the Form_Load event:
Private Sub Form_Load()
Dim printerIndex As Integer
For printerIndex = 0 To Application.Printers.Count - 1
Me.cbPrintersList.AddItem (Application.Printers(printerIndex).DeviceName)
Next printerIndex
End Sub
And here's the code for the btnPrint_Click event
Private Sub btnPrint_Click()
'Catch blank control.
'If set to 0, label report is blank but runs.
If IsNull(Me.txtNumberOfLabels) Or Not IsNumeric(Me.txtNumberOfLabels.Value) Then
MsgBox "Please indicate the number of labels you want to print", _
vbOKOnly, "Error"
DoCmd.GoToControl "txtNumberOfLabels"
Exit Sub
End If
Dim availablePrinters As Printer
Dim selectedPrinter As String
DoCmd.GoToControl "cbPrintersList"
selectedPrinter = Me.cbPrintersList.Text
For Each availablePrinters In Application.Printers
If availablePrinters.DeviceName = selectedPrinter Then
Set Application.Printer = availablePrinters
Exit For
End If
Next availablePrinters
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim lastLabelRecordIndex_Part1 As String
Dim lastLabelRecordIndex_Part2 As String
Dim oldLastLabelRecordIndex_Part1 As String
Dim oldLastLabelRecordIndex_Part2 As String
Dim strSQL As String
Set db = CurrentDb
strSQL = "SELECT MAX(Pre_SSCC) As MaxRecord FROM SSCC_Gen"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
oldLastLabelRecordIndex_Part1 = CStr(Left(rs("MaxRecord"), 8))
oldLastLabelRecordIndex_Part2 = CStr(Right(rs("MaxRecord"), 9))
rs.Close
db.Close
Dim labelRecordIndex As Long
DoCmd.SetWarnings False
For labelRecordIndex = CLng(oldLastLabelRecordIndex_Part2) To CLng(oldLastLabelRecordIndex_Part2) + Me.txtNumberOfLabels.Value - 1
DoCmd.RunSQL "INSERT INTO SSCC_GenCount (Data) VALUES (#" & Format(Now(), "dd/mm/yyyy") & "#)"
Next labelRecordIndex
DoCmd.SetWarnings True
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
lastLabelRecordIndex_Part1 = CStr(Left(rs("MaxRecord"), 8))
lastLabelRecordIndex_Part2 = CStr(Right(rs("MaxRecord"), 9))
rs.Close
db.Close
Dim oldLastLabelRecordIndex As String
Dim lastLabelRecordIndex As String
oldLastLabelRecordIndex = oldLastLabelRecordIndex_Part1 & oldLastLabelRecordIndex_Part2
lastLabelRecordIndex = lastLabelRecordIndex_Part1 & lastLabelRecordIndex_Part2
Dim test As Report
Set test = Reports![Labels_SSCC_Gen]
test.RecordSource = "SELECT * FROM SSCC_Gen WHERE Pre_SSCC BETWEEN " & CStr(oldLastLabelRecordIndex) & " AND " & CStr(lastLabelRecordIndex)
'Variables for debugging purposes
'Dim reportNameMemo As String
'Dim reportOpenArgsMemo As String
'reportNameMemo = test.Name
'reportOpenArgsMemo = test.RecordSource
DoCmd.OpenReport test.Name, acViewReport
DoCmd.PrintOut , , , , 1
End Sub
My problem is on the following lines of code:
Set test = Reports![Labels_SSCC Gen]
DoCmd.OpenReport test.Name, acViewReport
Now, if I don't open the report on access, I get the following error on debug, for the first line of code I pointed out above (roughly translated):
- The name of the report 'Labels_SSCC_Gen' you entered in the expression is misspelled or refers to a report that is not open or does not exist.
However, if I do open the report (even just on design view), on debug I see the report loading up (showing up a progress bar on access), but after a while it crashes with the following error, for the second line of code pointed out above:
- Incorrect data type in criteria expression
First of all, I cannot understand why I need to have the report open to be able to access the properties, therefor I need to know if there's a way to set the recordsource of the report without the need to open it, because the purpose is to just to use the form.
Secondly, I do not understand where the incorrect data type error even comes from, because for starters, All I'm doing is to run a query on the SSCC_Gen query view so that the report only retrieves the labels that are intended for printing and the query is well written, following the example below (taken while debug was running):
"SELECT * FROM SSCC_Gen WHERE Pre_SSCC BETWEEN 35601871000061300 AND 35601871000061301"
May someone shed some light as to what am I doing wrong? What am I missing?




I cannot understand why I need to have the report open to be able to access the properties-- That's just the way Access works. A report is an object, like any other object. The report designer is a "class," so to speak; there's no properties available until you actually instantiate the object. - Robert HarveyOpenReport. - Robert Harvey