在特定位置打印标签
printing label at specific position
我有 11 英寸 X 8.5 英寸的纸张来打印标签。论文分为两列,即每列 4.25 宽度,每列包含 11 个标签。
因此,每个标签的大小为 1 inch X 4.25 inch
。
现在我的问题是:我在 MS Access 2010 中使用 Northwind DB,考虑到 table dbo_Products,我想打印 Product ID 和每个标签上的 产品名称 。
我可以加入标签报告形成,但我无法得到输出。
如前所述 sheet 包含两列,如果用户想在特定标签位置打印标签,应该可以在该位置打印。
(例如,用户要在位置5打印Product ID:10,相应的产品信息必须打印在label located on the 5th position of the page.(标签在页面上的位置如下图)
1 | 2
3 | 4
5 | 6
7 | 8
...... till 22
如果有人可以通过显示表单与标签之间的连接并将其打印在特定的标签位置来帮助我解决这个问题,那就太好了。
谢谢
中的说明开始
接下来我将其修改为三个文本框,而不是一个。它们被命名为 'txtStart'、'txtEnd'、'txtLabelPos'。为该表格使用下面的代码。
注意 SQL 中的 'WHERE' 子句...更改表/字段名称以满足您自己的需要。
Option Compare Database
Option Explicit
Private Sub cmdCancel_Click()
'Reset and take no further action.
Me!txtStart.Value = 1
End Sub
Private Sub cmdPrint_Click()
'Pass table with label data, position for first label, and label report.
Dim bytPosition As Variant
Dim bytCounter As Byte
Dim rst As New ADODB.Recordset
If IsNull(Me.txtStart) Or Me.txtStart = "" Then
MsgBox "You must enter a starting range for the data.", vbOKOnly + vbCritical, "Missing Start Range"
Exit Sub
End If
If IsNull(Me.txtEnd) Or Me.txtEnd = "" Then
MsgBox "You must enter an ending range for the data.", vbOKOnly + vbCritical, "Missing End Range"
Exit Sub
End If
If IsNull(Me.txtLabelPos) Or Me.txtLabelPos = "" Or Not IsNumeric(Me.txtLabelPos) Then
MsgBox "You must enter the starting label position to print on.", vbOKOnly + vbCritical, "Missing Label Position"
Exit Sub
End If
Set rst.ActiveConnection = CurrentProject.Connection
rst.Open "SELECT * FROM tblCustomerLabels" _
, , adOpenDynamic, adLockOptimistic
'Delete previous label data.
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE FROM tblCustomerLabels"
'Add one empty record for each missing label.
bytPosition = Nz(Me!txtLabelPos.Value, 0)
For bytCounter = 2 To bytPosition
rst.AddNew
rst.Update
Next
'Update label data.
Dim strSQL As String
strSQL = "INSERT INTO tblCustomerLabels ( Company, [Last Name], [First Name], Address, City, [State/Province], [ZIP/Postal Code], [Country/Region] ) " & _
"SELECT Customers.Company, Customers.[Last Name], Customers.[First Name], Customers.Address, Customers.City, Customers.[State/Province], Customers.[ZIP/Postal Code], Customers.[Country/Region] " & _
"FROM Customers " & _
"Where [Last Name] >= '" & Me.txtStart & "' AND [Last Name] <= '" & Me.txtEnd & "';"
DoCmd.RunSQL strSQL
'Open label report.
DoCmd.SetWarnings True
DoCmd.OpenReport "rptCustomerLabels", acViewPreview
rst.Close
Set rst = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
rst.Close
Set rst = Nothing
DoCmd.SetWarnings True
End Sub
我有 11 英寸 X 8.5 英寸的纸张来打印标签。论文分为两列,即每列 4.25 宽度,每列包含 11 个标签。
因此,每个标签的大小为 1 inch X 4.25 inch
。
现在我的问题是:我在 MS Access 2010 中使用 Northwind DB,考虑到 table dbo_Products,我想打印 Product ID 和每个标签上的 产品名称 。
我可以加入标签报告形成,但我无法得到输出。
如前所述 sheet 包含两列,如果用户想在特定标签位置打印标签,应该可以在该位置打印。
(例如,用户要在位置5打印Product ID:10,相应的产品信息必须打印在label located on the 5th position of the page.(标签在页面上的位置如下图)
1 | 2
3 | 4
5 | 6
7 | 8
...... till 22
如果有人可以通过显示表单与标签之间的连接并将其打印在特定的标签位置来帮助我解决这个问题,那就太好了。
谢谢
接下来我将其修改为三个文本框,而不是一个。它们被命名为 'txtStart'、'txtEnd'、'txtLabelPos'。为该表格使用下面的代码。
注意 SQL 中的 'WHERE' 子句...更改表/字段名称以满足您自己的需要。
Option Compare Database
Option Explicit
Private Sub cmdCancel_Click()
'Reset and take no further action.
Me!txtStart.Value = 1
End Sub
Private Sub cmdPrint_Click()
'Pass table with label data, position for first label, and label report.
Dim bytPosition As Variant
Dim bytCounter As Byte
Dim rst As New ADODB.Recordset
If IsNull(Me.txtStart) Or Me.txtStart = "" Then
MsgBox "You must enter a starting range for the data.", vbOKOnly + vbCritical, "Missing Start Range"
Exit Sub
End If
If IsNull(Me.txtEnd) Or Me.txtEnd = "" Then
MsgBox "You must enter an ending range for the data.", vbOKOnly + vbCritical, "Missing End Range"
Exit Sub
End If
If IsNull(Me.txtLabelPos) Or Me.txtLabelPos = "" Or Not IsNumeric(Me.txtLabelPos) Then
MsgBox "You must enter the starting label position to print on.", vbOKOnly + vbCritical, "Missing Label Position"
Exit Sub
End If
Set rst.ActiveConnection = CurrentProject.Connection
rst.Open "SELECT * FROM tblCustomerLabels" _
, , adOpenDynamic, adLockOptimistic
'Delete previous label data.
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE FROM tblCustomerLabels"
'Add one empty record for each missing label.
bytPosition = Nz(Me!txtLabelPos.Value, 0)
For bytCounter = 2 To bytPosition
rst.AddNew
rst.Update
Next
'Update label data.
Dim strSQL As String
strSQL = "INSERT INTO tblCustomerLabels ( Company, [Last Name], [First Name], Address, City, [State/Province], [ZIP/Postal Code], [Country/Region] ) " & _
"SELECT Customers.Company, Customers.[Last Name], Customers.[First Name], Customers.Address, Customers.City, Customers.[State/Province], Customers.[ZIP/Postal Code], Customers.[Country/Region] " & _
"FROM Customers " & _
"Where [Last Name] >= '" & Me.txtStart & "' AND [Last Name] <= '" & Me.txtEnd & "';"
DoCmd.RunSQL strSQL
'Open label report.
DoCmd.SetWarnings True
DoCmd.OpenReport "rptCustomerLabels", acViewPreview
rst.Close
Set rst = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
rst.Close
Set rst = Nothing
DoCmd.SetWarnings True
End Sub