Results 1 to 6 of 6
  1. #1
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283

    Error 91 Object Variable or Block Variable Not Set

    I have the following code that I am trying to get to do the following: Query the main table (which has manager as a field as well as several other data fields) for a unique list of managers, then query and return the data for each manager and send it to an excel spreadsheet named for the manager.

    so if the main table had 2 rows for manager = Jones and 4 records for manager = Smith I would get 2 spreadsheets saved one named Jones and the other Smith each containing just the rows aligned to them in the table

    I get Error 91 on this line Set rs = dbs.OpenRecordset(strSQL, dbOpenDynaset)


    Code:
    Option Compare Database
    Private Sub Command0_Click()
     Dim dbs As DAO.Database
     Dim strSQL As String
     Dim rs As DAO.Recordset
     
     Setdbs = CurrentDb
     strSQL = "SELECT DISTINCT [Manager] FROM qry_Monthly_Export " & "ORDER BY [Manager]"
     Set rs = dbs.OpenRecordset(strSQL, dbOpenDynaset)
     While Not rs.EOF
     strSQL = "SELECT * FROM qry_Monthly_Export " _
     & "WHERE [Manager] = '" & rs("Manager") & "'"
     dbs.QueryDefs("qry_ExportCopy").SQL = strSQL
     DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _
     "qry_ExportCopy", _
     "C:\Temp\" & rs("NameField") & ".xls"
     rs.MoveNext
     Wend
    rs.Close
     Set rs = Nothing
     End Sub


  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If that's a copy paste, you need a space after Set:

    Setdbs = CurrentDb
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    Man I stared at that for hours.....thanks so much.

    Now that I am past that step I get the following error 3265 - Item not found in this selection

    and it highlights this section

    Code:
     DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _
     "qry_ExportCopy", _
     "C:\Temp\" & rs("NameField") & ".xls"

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The only field you selected for the recordset was Manager, so it's going to error if you specify any other field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    Figured it out. I had the namefield wrong for the file creation step.

    It is working perfect now.

    Thanks for all your help

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 6
    Last Post: 02-11-2016, 02:05 PM
  2. Object variable or With block variable not defined
    By PorscheMan in forum Programming
    Replies: 3
    Last Post: 01-16-2013, 01:53 PM
  3. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  4. Object variable or With block variable not set
    By walter189 in forum Programming
    Replies: 1
    Last Post: 07-28-2011, 08:51 AM
  5. Replies: 4
    Last Post: 08-05-2010, 01:26 PM

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