Hi mate,
Yes i think i now see wot u r looking for. Here is the code attached to my PrintLabels cmd button that uses a query called qryMergeContacts to populate a temporary table called tblContactsForLabels. This temp table gets its field values from the main table called tblContacts. Hope this makes sense when u see the code. Here goes:
""""""""""""""""""""""""""""""""""""""""""""""""""
Private Sub cmdPrintLabels_Click()
On Error GoTo ErrorHandler
Dim strSQL As String
Dim lst As Access.ListBox
Dim strName As String
Dim strAddress As String
Dim strJobTitle As String
Dim strTestFile As String
Dim varItem As Variant
Dim intIndex As Integer
Dim intCount As Integer
Dim intRow As Integer
Dim intColumn As Integer
Dim strTest As String
Dim i As String
Dim lngID As Long
Set lst = Me![lstSelectContacts]
'Clear old temp table
DoCmd.SetWarnings False
strSQL = "DELETE * from tblContactsForLabels"
DoCmd.RunSQL strSQL
'Check that at least one contact has been selected
If lst.ItemsSelected.Count = 0 Then
MsgBox "Please select at least one contact"
lst.SetFocus
Exit Sub
End If
For Each varItem In lst.ItemsSelected
'Check for required address information
strTest = Nz(lst.Column(5, varItem))
Debug.Print "Street address: " & strTest
If strTest = "" Then
Debug.Print "Can't send letter -- no street address!"
Exit Sub
End If
strTest = Nz(lst.Column(6, varItem))
Debug.Print "City: " & strTest
If strTest = "" Then
Debug.Print "Can't send letter -- no city!"
Exit Sub
End If
strTest = Nz(lst.Column(8, varItem))
Debug.Print "Postal code: " & strTest
If strTest = "" Then
Debug.Print "Can't send letter -- no postal code!"
Exit Sub
End If
'All information is present; write a record to the temp table
lngID = lst.Column(0, varItem)
Debug.Print "Selected ID: " & lngID
strSQL = "INSERT INTO tblContactsForLabels (ContactID, FirstName, " _
& "LastName, Salutation, StreetAddress, Town, City, StateOrProvince, " _
& "PostalCode, Country, CompanyName, JobTitle )" _
& "SELECT ContactID, FirstName, LastName, Salutation, " _
& "StreetAddress, Town, City, StateOrProvince, PostalCode, Country, " _
& "CompanyName, JobTitle FROM tblContacts " _
& "WHERE ContactID = " & lngID & ";"
DoCmd.RunSQL strSQL
Next varItem
'Print report
DoCmd.OpenReport reportname:="rptContactLabels", View:=acViewPreview
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
"""""""""""""""""""""""""""""""""""""""""""
I can obviously explain "most" of the components of the code and how they relate to my forms/tables/reports etc. But i'm afraid u will have 2 wait till tommorow nite as i am off to bed now, sorry i have early start 4 work tommorow. Meantime u can maybe play around with the code and see if u can get it to do what u want.
Best of Luck mate, will check in again tommorow night.
C ya
Stu
UK
PS: Thanks to Helen Feddema for the original code in the book.