error 3022 in vba export access qry to Excel file

starrcruise

Registered User.
Local time
Today, 15:01
Joined
Mar 4, 2011
Messages
18
I am totally at a loss. I have a query in Access that I want to export the multiple filtered versions based on "LeadID's in another table and exported to separate worksheets (per LeadID) using transfer spreadsheet vba. I am asking it to find the PeopleID (autonumber NOT indexed) from the tblListPeople, then create a recordset listing the name matching up with each PeopleID so it can loop through the names and create a filtered query for each name, then export each to a tab on one excel file. I pretty much copied this code, just added my DB object names.

It creates the first qrydef = strtemp(LeadReportExport)
Then comes up with the error 3022 the changes to the table were not successful because of ....indexed duplicate values,

I have gone to each table, and made sure all foreign fields and main ID fields are not EVEN indexed (which I am not happy about, but the primary ID in each table is auto numbered). Nothing seems to work. Here is code.

Code:
Case 3    'exports Tasks lists by lead to excel tabbed file
    If Me.cboChooseReport = 3 Then
        Dim qdf As DAO.QueryDef
        Dim dbs As DAO.Database
        Dim rstLead As DAO.Recordset
        Dim strSQL As String, strTemp As String, strLead As String
 
        Const strFileName As String = "Project and Task Tracking Input"
        Const strQName As String = "LeadReportExport"
        Set dbs = CurrentDb
        'create temporary query to export data
        strTemp = dbs.TableDefs(0).Name
        strSQL = "SELECT * FROM [" & strTemp & "] where 1=0;"
        Set qdf = dbs.CreateQueryDef(strQName, strSQL)
        qdf.Close
        strTemp = strQName
        'Get list of PeopleID values
        strSQL = "SELECT DISTINCT PeopleID FROM tblListPeople;"
        Set rstLead = dbs.OpenRecordset(strSQL, dbOpenDynaset, dbReadOnly)
        'Loop through list of People values and create a query
        'for each ID so data can be exported
        If rstLead.EOF = False And rstLead.BOF = False Then
            rstLead.MoveFirst
            Do While rstLead.EOF = False
 
                'set strLead
                strLead = DLookup("Name", "tblListPeople", _
                                  "PeopleID=" & rstLead!PeopleID.value)
 
                'set strSQL
                strSQL = "SELECT * FROM qrySubrptTaskDetailOpenExport WHERE" & _
                         "LeadID = " & rstLead!PeopleID.value & ";"
                Set qdf = dbs.QueryDefs(strTemp)
                qdf.Name = "q_" & strLead
                strTemp = qdf.Name
                qdf.sql = strSQL
                qdf.Close
                Set qdf = Nothing
 
                'send to excel
                DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
                                          strTemp, "U:\Enterprise Risk\Corporate Risk Committee\Project and Task Tracking\" & strFileName & ".xls"
                rstLead.MoveNext
            Loop
        End If
 
        rstLead.Close
        Set rstLead = Nothing
 
        dbs.QueryDefs.Delete strTemp
        dbs.Close
        Set dbs = Nothing
    End If
 
 
Exit_Procedure:
End Select
Exit Sub
Error_Handler:
MsgBox "An error has occurred in this application." _
     & "Please contact your technical support and" _
     & "tell them this information:" _
     & vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " _
     & Err.Description, _
       Buttons:=vbCritical, Title:="CRC Project DB"
Resume Exit_Procedure
End Sub
 
Last edited by a moderator:
Thank you for the reply. I checked out that link and it had some really good stuff. I finally figured out 99% of the above. I didn't actually "copy" code, I used and adapted part of the code from "Ken's Examples for Exporting to Excel...." So below is the working code. My only problem is, this code creates the queries as it goes through the list of people names in the tbllistpeople. And then exports all to 1 excel workbook on separate tabs. (what I want) I just can't get it do delete the queries after the export, and they all end up listed in my query section in the DB. Whenever I use anything related to the query, strName, qdfTemp, strQDF, etc., in the dbs.QueryDefs.Delete line, I get the 3065 error message. Thanks in advance.

Code:
Private Sub cmdtest_Click()
    Dim qdftemp As DAO.QueryDef
    Dim dbs As DAO.Database
    Dim rstLead As DAO.Recordset
    Dim strSQL As String, strLead As String, strName As String, strFileName As String, strQDF As String
    strFileName = "Project and Task Tracking Input" & Format(Date, "mm-dd-yyyy") & ".xls"
    Set dbs = CurrentDb
    'Get list of People ID values
    strSQL = "SELECT PeopleID, Name FROM tblListPeople"
    Set rstLead = dbs.OpenRecordset(strSQL, dbOpenDynaset)
    'Loop through list of LeadID values and create a query
    'for each ID so data can be exported
    If rstLead.EOF = False And rstLead.BOF = False Then
        rstLead.MoveFirst
        Do While rstLead.EOF = False
 
            'set strLead
            strLead = rstLead!Name.value
 
            'set strSQL
            strSQL = "SELECT * FROM qrySubrptTaskDetailOpenExport WHERE LeadID=" & rstLead!PeopleID.value & ";"
 
            strQDF = "_List Export_"
            Set qdftemp = dbs.CreateQueryDef(strQDF, strSQL)
            qdftemp.Name = "List" & strLead
            strName = qdftemp.Name
            qdftemp.Close
            Set qdftemp = Nothing
 
            'send to excel
            DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
                                      strName, "U:\Enterprise Risk\Corporate Risk Committee\Project and Task Tracking\" & strFileName
            rstLead.MoveNext
        Loop
    End If
 
    rstLead.Close
    Set rstLead = Nothing
    'dbs.QueryDefs.Delete ??????
 
 
 
    dbs.Close
    Set dbs = Nothing
 
 
Exit_Procedure:
 
    Exit Sub
Error_Handler:
    MsgBox "An error has occurred in this application." _
         & "Please contact your technical support and" _
         & "tell them this information:" _
         & vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " _
         & Err.Description, _
           Buttons:=vbCritical, Title:="CRC Project DB"
    Resume Exit_Procedure
End Sub
 
Last edited by a moderator:
It's still a copy and paste ;)

I'm sure one of the examples in the link provided doesn't go through the process of creating query defs per person. It simply loops through each person in a recordset, filters down the results and exports the filtered list using Excel's CopyfromRecordset method. This is the route you should be following.
 
So, basically, are you saying that after my 'set strLead code, (of course Dims are already done), I should plug in this code and do away with the rest of mine except for red code for loop (I added) and any other "SETS" I need to set to "nothing" and error handling)? (of course using or commenting out formatting). Can I just cut and paste this whole function as a module and Call it after my Set StrLead code? I always "think" I understand and then, when I get into it I'm like.....Uuuhh??? Thank you for your patience.
(after my set strlead code)
Code:
    Set rst = CurrentDb.OpenRecordset(strTQName)[/FONT]
[FONT=Courier New]    Set ApXL = CreateObject("Excel.Application")[/FONT]
[FONT=Courier New]    Set xlWBk = ApXL.Workbooks.Add[/FONT]
[FONT=Courier New]    ApXL.Visible = True[/FONT]
[FONT=Courier New][/FONT] 
[FONT=Courier New]    Set xlWSh = xlWBk.Worksheets("Sheet1")[/FONT]
[FONT=Courier New]    If Len(strSheetName) > 0 Then[/FONT]
[FONT=Courier New]        xlWSh.Name = Left(strSheetName, 34)[/FONT]
[FONT=Courier New]    End If[/FONT]
[FONT=Courier New]    xlWSh.Range("A1").Select[/FONT]
[FONT=Courier New][/FONT] 
[FONT=Courier New]    For Each fld In rst.Fields[/FONT]
[FONT=Courier New]        ApXL.ActiveCell = fld.Name[/FONT]
[FONT=Courier New]        ApXL.ActiveCell.Offset(0, 1).Select[/FONT]
[FONT=Courier New]    Next[/FONT]
[FONT=Courier New]    rst.MoveFirst[/FONT]
[FONT=Courier New]    xlWSh.Range("A2").CopyFromRecordset rst[/FONT]
[FONT=Courier New]    xlWSh.Range("1:1").Select[/FONT]
[FONT=Courier New]    ' This is included to show some of what you can do about formatting. You can comment out or delete[/FONT]
[FONT=Courier New]    ' any of this that you don't want to use in your own export.[/FONT]
[FONT=Courier New]    With ApXL.Selection.Font[/FONT]
[FONT=Courier New]        .Name = "Arial"[/FONT]
[FONT=Courier New]        .Size = 12[/FONT]
[FONT=Courier New]        .Strikethrough = False[/FONT]
[FONT=Courier New]        .Superscript = False[/FONT]
[FONT=Courier New]        .Subscript = False[/FONT]
[FONT=Courier New]        .OutlineFont = False[/FONT]
[FONT=Courier New]        .Shadow = False[/FONT]
[FONT=Courier New]    End With[/FONT]
[FONT=Courier New]    ApXL.Selection.Font.Bold = True[/FONT]
[FONT=Courier New]    With ApXL.Selection[/FONT]
[FONT=Courier New]        .HorizontalAlignment = xlCenter[/FONT]
[FONT=Courier New]        .VerticalAlignment = xlBottom[/FONT]
[FONT=Courier New]        .WrapText = False[/FONT]
[FONT=Courier New]        .Orientation = 0[/FONT]
[FONT=Courier New]        .AddIndent = False[/FONT]
[FONT=Courier New]        .IndentLevel = 0[/FONT]
[FONT=Courier New]        .ShrinkToFit = False[/FONT]
[FONT=Courier New]        .MergeCells = False[/FONT]
[FONT=Courier New]    End With[/FONT]
[FONT=Courier New]    ' selects all of the cells[/FONT]
[FONT=Courier New]    ApXL.ActiveSheet.Cells.Select[/FONT]
[FONT=Courier New]    ' does the "autofit" for all columns[/FONT]
[FONT=Courier New]    ApXL.ActiveSheet.Cells.EntireColumn.AutoFit[/FONT]
[FONT=Courier New]    ' selects the first cell to unselect all cells[/FONT]
[FONT=Courier New]    xlWSh.Range("A1").Select[/FONT]
[FONT=Courier New]Loop[/FONT]
[FONT=Courier New]End If[/FONT]
[FONT=Courier New]rst.Close[/FONT]
[FONT=Courier New]Set rst = Nothing[/FONT]
[FONT=Courier New]Exit Function[/FONT]
[FONT=Courier New]err_handler:[/FONT]
[FONT=Courier New]DoCmd.SetWarnings True[/FONT]
[FONT=Courier New]MsgBox Err.Description, vbExclamation, Err.Number[/FONT]
[FONT=Courier New]Exit Function[/FONT]
[FONT=Courier New]End Function[/FONT]
[FONT=Courier New]
 
Last edited by a moderator:
Please use code tags when posting large sets of code

codetag001.png
 
Alright, so here's the process:

1. Create a recordset based on LeadIDs
2. For each of the LeadIDs Output the filtered query using one of the methods provided in the link by Bob.
 

Users who are viewing this thread

Back
Top Bottom