Question : Duplicating a record in an MS Access Database

I have an MS Access Database that contains one record (contains names and numbers, bench staff) for each hockey team in a tournament. In this tournament, each team will play between 4 and 5 games, depending on their division. I will print three mailing labels for each game for each team, or a total of between 12 and 15 labels for each team in the tournament. The labels are stuck to the "game sheet" which is a multipart form used to records goals, penalties, etc. Each label will contain the names and numbers of the players on one team (the labels are 4x2 and the names are printed across the 2" dimension in 10pt font). Currently I use Mailmerge to create one label for each team from the Access database, and then I copy and paste as many as I need before printing.
The copy-paste process is prone to error. The database is sorted by Team within Division. I would like to write a VB program that reads through the database, and for each Division, prompts for the number of labels needed. It then duplicates each Access database record as many times as requested for each team within that Division. When done, the code should print a summary of how many teams are in each division to a log file, e.g.'log.txt' in the current directory is fine. I can then use mailmerge as before but it will make the exact number of labels requested for each team.  
I am (very) familiar with programming but I do VB/Excel/Access programming at most once a year. Can you provide a code snippet that duplicates the records as above, and instructions for how to invoke it ?
If you just assume in the code that each team consists of two text fields, Division and Team, I can fill in the remaining details. You can assume that there are at most 100 teams and 20 Divisions if necessary. I am running Access 2002 under XP.

Thanks

Answer : Duplicating a record in an MS Access Database

I was surprised that this question did not get answered right away. The code was not very complicated - knowing the Access tricks and syntax was the main problem. I eventually figured out how to do it myself. One hangup was that the DAO reference needed to be before the ADO reference. Another tricky thing was getting the records sorted by Division and TeamName. The third trick was to write the new records without interfering with the existing ones. Here is the code that I came up with. Any comments or criticisms are appreciated.

Sub tst()
    Dim db As Database, rs As Recordset, newrs As Recordset
    Dim ndiv As Long, nteam As Long, numReqd As Long
    Dim currDiv As String, tableName As String
    tableName = "Office_Address_List"
    Set db = DBEngine.Workspaces(0).Databases(0)
    ' open table sorted by Division then TeamName
    Set rs = db.OpenRecordset("SELECT * FROM Office_Address_List order by Division, TeamName")
    ndiv = 0
    nteam = 0
    currDiv = ""
    ' we will output the new records to newrs, which starts off as the same set of records as rs
    Set newrs = db.OpenRecordset(tableName, dbOpenTable)
    'examine each record in the table
    Do Until rs.EOF
        If rs![Division] <> currDiv Then
            currDiv = rs![Division]
            'prompt for how many dups reqd
            numReqd = InputBox("Enter number of extra labels per team for Division " & currDiv, , "0")
            Debug.Print "new Division" & rs![Division]
            ndiv = ndiv + 1
        End If
        nteam = nteam + 1
        ' make as many duplicates of the cuirrent record as required
        For i = 1 To numReqd
            newrs.AddNew
            newrs.Fields("Division") = rs![Division]
            newrs.Fields("TeamName") = rs![TeamName]
            newrs.Update
        Next i
        rs.MoveNext
    Loop
    Debug.Print "Divs found " & ndiv;
    Debug.Print "Teams found " & nteam;

    newrs.Close
    Set newrs = Nothing
    rs.Close
    Set rs = Nothing
    db.Close
End Sub
Random Solutions  
 
programming4us programming4us