Results 1 to 6 of 6
  1. #1
    veejay is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Nov 2018
    Location
    Montreal, Canada
    Posts
    46

    Angry Exporting to access VBA Error 3061


    Title should read : Exporting to EXCEL

    While running the following code I run into a 3061 error (expected 2).
    Trying to use the debug mode but it doesn't seems to be working.

    Code:
    Private Sub export2Excel_Click()
    On Error GoTo SubError
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        Dim strSQL As String
        Dim rs1 As DAO.Recordset
        Dim i As Integer
        
        Dim intCounterAVP As Integer
        Dim strAVP As Variant
     
        'Show user work is happening
        DoCmd.Hourglass (True)
     
        '*********************************************
        '              RETRIEVE DATA
        '*********************************************
        strSQL = "SELECT zRangeIndividuals.EXAMINER_S AS Examiner, zRangeIndividuals.[Files Reviewed], zRangeIndividuals.[Total error], zRangeIndividuals.[Files with error], zRangeIndividuals.serror, zRangeIndividuals.[310(a)], zRangeIndividuals.Errors, zRangeIndividuals.Formal, zRangeIndividuals.Clerical, zRangeIndividuals.[sError & 30(a) % ], zRangeIndividuals.[Overall Quality %], zRangeIndividuals.[Team Leaders] " _
        + "FROM zRangeIndividuals " _
        + "ORDER BY zRangeIndividuals.EXAMINER_S "
        
        'Execute query and populate recordset
        Set rs1 = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
        
        'If no data, don't bother opening Excel, just quit
        If rs1.RecordCount = 0 Then
            MsgBox "No data selected for export", vbInformation + vbOKOnly, "No data exported"
            GoTo SubExit
        End If
      
        '*********************************************
        '             BUILD SPREADSHEET
        '*********************************************
        Set xlApp = Excel.Application
       
        xlApp.Visible = False
        Set xlBook = xlApp.Workbooks.Add
        Set xlSheet = xlBook.Worksheets(1)
     
        With xlSheet
            .Name = "Exams"
            .Cells.Font.Name = "Century"
            .Cells.Font.Size = 11
      
            'Set column widths
            .Columns("A").ColumnWidth = 13
            .Columns("B").ColumnWidth = 25
            .Columns("C").ColumnWidth = 10
            .Columns("D").ColumnWidth = 10
            .Columns("E").ColumnWidth = 10
            .Columns("F").ColumnWidth = 10
            .Columns("G").ColumnWidth = 10
            .Columns("H").ColumnWidth = 10
            .Columns("I").ColumnWidth = 10
            .Columns("J").ColumnWidth = 10
            .Columns("K").ColumnWidth = 10
            .Columns("L").ColumnWidth = 10
            
          
            For cols = 0 To rs1.Fields.Count - 1
                .Cells(1, cols + 1).Value = rs1.Fields(cols).Name
            Next
     
            'Copy data from recordset to sheet
            .Range("A2").CopyFromRecordset rs1
           
        End With
     
    SubExit:
    On Error Resume Next
        DoCmd.Hourglass False
        xlApp.Visible = True
        rs1.Close
        Set rs1 = Nothing
        Exit Sub
       
    SubError:
        MsgBox "Error Number: " & Err.Number & "= " & Err.Description, vbCritical + vbOKOnly, _
            "An error occurred"
        GoTo SubExit
    End Sub
    Last edited by veejay; 11-28-2018 at 10:38 AM. Reason: Error in title

  2. #2
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Two things where are you getting the error in your code?

    And really you have a field called [sError & 30(a) % ] in your query?
    That is asking for issues like you wouldn't believe, the & % () are all symbols used for a variety of things in access and if they aren't causing you problems yet they will in the future.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    veejay is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Nov 2018
    Location
    Montreal, Canada
    Posts
    46
    I tried to use the Debug function but I believe I might be using it wrongly as it's not highlighting anything.

    I did have a field called [sError & 30(a) % ] but i've updated all () and % and & and now this part looks like:

    Code:
        strSQL = "SELECT zRangeIndividuals.Examiner, zRangeIndividuals.[Files Reviewed], " _
        + "zRangeIndividuals.[Total discrepancies], zRangeIndividuals.[Files with discrepancy], " _
        + "zRangeIndividuals.Substantive, zRangeIndividuals.[30a], zRangeIndividuals.Nice, zRangeIndividuals.Formal, " _
        + "zRangeIndividuals.Clerical, zRangeIndividuals.[Substantive and 30a], " _
        + "zRangeIndividuals.[Overall Quality], zRangeIndividuals.Manager " _
        + "FROM zRangeIndividuals"
    I still get the 3061 error.
    I'm 100% certain that the field are correctly spelled as I (shamelessly) used the design mode for my query then looked at the SQL.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    You put a break point (in this case, probably at the first line) and cause the procedure to execute - be that a form button or whatever your trigger is. When code stops at the break point line, F8 through it (each F8 press executes a line) and see when/where the code fails.

    Likely a field reference isn't being resolved. The builder is no guarantee because there are times when you have to know that a parent reference/object is required before some other one that you're able to pick. So the names might be correct, but their "address" (reference) isn't. Note that when code breaks on a line, that line has not been executed, so any variables for example, have not been dealt with. Suggest that if the problem is the execution of your sql statement that you insert Debug.Print before Set rs1...

    If the sql output doesn't look correct in the immediate window, you might figure out the problem. If it does, you copy/paste the sql into a new query window and attempt to run it. Access might highlight the problem part in sql view for you. If you're able to do all that but don't get anywhere, maybe posting the immediate window output will give us a clue. BTW, I would not use + to join sql portions together. The proper character is the ampersand (& ) to perform concatenation. + is for addition.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Another point:
    Quote Originally Posted by Minty View Post
    And really you have a field called [sError & 30(a) % ] in your query?
    That is asking for issues like you wouldn't believe, the & % () are all symbols used for a variety of things in access and if they aren't causing you problems yet they will in the future.
    You should remove any and all spaces in object names: this includes field, table, form, query and report names.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    The + character will concatenate strings but if any inputs are Null, the result will be Null. This can be useful in some situations. The & basically treats Null as an empty string.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. What is Error Number 3061?
    By Lou_Reed in forum Access
    Replies: 6
    Last Post: 04-20-2017, 04:15 PM
  2. Replies: 3
    Last Post: 02-26-2016, 12:34 PM
  3. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 AM
  4. Error 3061 in VBA program
    By fbou in forum Programming
    Replies: 5
    Last Post: 10-07-2009, 11:00 AM
  5. Error 3061
    By Shanks in forum Queries
    Replies: 4
    Last Post: 09-16-2009, 07:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums