開始於http://www.techrepublic.com/blog/how-do-i/how-do-i-start-an-access-label-report-with-any-label-on-the-sheet/發現的說明
接下來我修改了這個文件夾,而不是一個文本框。它們被命名爲'txtStart','txtEnd','txtLabelPos'。使用下面的代碼表單。
注意「WHERE」在SQL條款等改變表/字段名,以滿足自己的需要。
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
謝謝@Omar的編輯。希望我爲此得到合適的解決方案。 – Hiranya
您使用普通紙嗎?我不知道雙列4.25英寸寬的不乾膠標籤,標籤通常設計爲從第一張到最後一張,從紙張左上角的標籤開始打印。您是否希望用戶有能力選擇在一張紙上任意給定的「標籤」來打印嗎?如果是這樣,這將需要一些特殊的代碼來處理。 –
是的,我希望用戶能夠打印自己選擇的給定尺寸的不乾膠標籤紙的標籤上的能力。你理解我的問題是正確的。@ WayneG.Dunn。希望我得到一個解決方案。 – Hiranya