Results 1 to 12 of 12
  1. #1
    vvwstcat is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2021
    Posts
    12

    Microsoft Access cannot find the object

    I am a casual user, have only limited knowledge in the tools. I created a MS Access db module called GenStockReports. What it does is to run 2 queries and output the results to excel workbooks.
    I did not write the code which was actually generated thru a few clicks. See the code below:


    Public Sub GenStockReports() ' I replaced Sub by Function, the same error message.
    Dim Outfile As String


    On Error GoTo GenStockReports_Err


    Outfile = Application.CurrentProject.Path & "" & "DailyStockReport US.xls"
    DoCmd.OutputTo acOutputQuery, "QryStockReport", "Excel97-Excel2003Workbook(*.xls)", Outfile, False, "", , acExportQualityPrint



    Outfile = Application.CurrentProject.Path & "" & "DailyOptionReport US.xls"
    DoCmd.OutputTo acOutputQuery, "QryOptionReport", "Excel97-Excel2003Workbook(*.xls)", Outfile, False, "", , acExportQualityPrint




    GenStockReports_Exit:
    Exit Sub


    GenStockReports_Err:
    MsgBox Error$
    Resume GenStockReports_Exit


    End Sub

    ----------------------------------------

    I can run it under the design view.
    I can also run it under the tools | macros | run button (GenStockReports appears under the Macro name inbox)

    However, if I create a macro in the main Access menu that runs the module, I then get an error messages “Microsoft Access cannot find the object “GenStockReports”
    Can somebody help me to run the module under the create macro main page?

    Click image for larger version. 

Name:	error screen.JPG 
Views:	34 
Size:	35.2 KB 
ID:	45481

    the macro list.
    Click image for larger version. 

Name:	access main page.JPG 
Views:	31 
Size:	76.8 KB 
ID:	45482


    Many thanks.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,910
    You would have to show us a picture of the macro list?

    OK, from your description and my 2007,that runs CODE which in Excel terms is called a macro, but is just VBA code.?
    A macro in Access is something completely different?

    If your code is a Function, you can use the RunCode macro command.
    Last edited by Welshgasman; 06-12-2021 at 05:58 AM. Reason: Looked at Tools/Macro
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,563

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,910
    I have never used that option and can see that it emulates Excel, but still only writes VBA code into a module.? Very confusing.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    vvwstcat is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2021
    Posts
    12
    Thanks. I have done a bit of readings. It seems that Access modules have to be associated with forms or reports before they can be referenced. Mine one is a general module, not assoicated with any object. it could be the reason Access cannot find it. Anyway, it is very disappointed to see such limitation. I meant to do automation. if theere is no alternative, I then continue to handle it mannually.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,910
    Quote Originally Posted by vvwstcat View Post
    Thanks. I have done a bit of readings. It seems that Access modules have to be associated with forms or reports before they can be referenced. Mine one is a general module, not assoicated with any object. it could be the reason Access cannot find it. Anyway, it is very disappointed to see such limitation. I meant to do automation. if theere is no alternative, I then continue to handle it mannually.
    No, it is perfectly normal to have vba code in modules, very common in fact.
    Now you have posted what you have, that confirms what I suspected and said?

    If you make that sub a function, then you use the actual Macro RunCode command to run that NOT RunMacro.?

    You could just have a simple form with one button to run that as it is as well?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    Quote Originally Posted by vvwstcat View Post
    Thanks. I have done a bit of readings. It seems that Access modules have to be associated with forms or reports before they can be referenced. Mine one is a general module, not assoicated with any object. it could be the reason Access cannot find it. Anyway, it is very disappointed to see such limitation. I meant to do automation. if theere is no alternative, I then continue to handle it mannually.
    A function or sub within a code module cannot have the same name as the code module.

    Click image for larger version. 

Name:	func.png 
Views:	27 
Size:	289.6 KB 
ID:	45485

    The above macro Runcode argument must be Function Name =GenStockReports()
    The = is required

    Then make sure the code procedure in that renamed module actually is a Function:
    Code:
    Public Function GenStockReports() 
        Dim Outfile As String
        On Error GoTo GenStockReports_Err
        Outfile = Application.CurrentProject.Path & "" & "DailyStockReport US.xls"
        DoCmd.OutputTo acOutputQuery, "QryStockReport", "Excel97-Excel2003Workbook(*.xls)", Outfile, False, "", , acExportQualityPrint
        Outfile = Application.CurrentProject.Path & "" & "DailyOptionReport US.xls"
        DoCmd.OutputTo acOutputQuery, "QryOptionReport", "Excel97-Excel2003Workbook(*.xls)", Outfile, False, "", , acExportQualityPrint
    GenStockReports_Exit:
        Exit Function
    GenStockReports_Err:
        MsgBox Error$
        Resume GenStockReports_Exit
    End Function
    Last edited by davegri; 06-12-2021 at 08:31 AM. Reason: note for =

  8. #8
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    You have the path wrong (missing slash):
    Code:
    Outfile = Application.CurrentProject.Path & "\" & "DailyStockReport US.xls"
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  9. #9
    vvwstcat is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2021
    Posts
    12
    @davegri, thanks a lot for your advice. It works after taking your words. Thanks a lot for pointing out the subtle difference, your advice saves me hours of headless trial and error. Appreciate your help very much.

  10. #10
    vvwstcat is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2021
    Posts
    12
    Quote Originally Posted by Gicu View Post
    You have the path wrong (missing slash):
    Code:
    Outfile = Application.CurrentProject.Path & "\" & "DailyStockReport US.xls"
    Cheers,
    thanks Gicu for pointing out. it is a slash in my code, but dont know why it got changed when pasted there.

  11. #11
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    Quote Originally Posted by vvwstcat View Post
    @davegri, thanks a lot for your advice. It works after taking your words. Thanks a lot for pointing out the subtle difference, your advice saves me hours of headless trial and error. Appreciate your help very much.
    You're quite welcome. Glad to help and see that the project is progressing.

  12. #12
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by vvwstcat View Post
    I am a casual user, have only limited knowledge in the tools. I created a MS Access db module called GenStockReports. What it does is to run 2 queries and output the results to excel workbooks.
    I did not write the code which was actually generated thru a few clicks. See the code below:


    Public Sub GenStockReports() ' I replaced Sub by Function, the same error message.
    Dim Outfile As String
    You should NOT have a procedure the same name as the Module name. Remember, the module name is a Container that holds the VBA code. And you do not "Run" modules, you Run the procedures inside the module.

    I use a 3 letter prefix for my object names:
    tbl for tables (tblEmployees)
    frm for forms (frmEmployees)
    qry for queries (qryEmployeeList)
    rpt for reports (rptActiveEmployeeList)
    and mod for standard modules (modStringRoutines)


    You should get in the habit of using Debug.Print statements in code to check that values are what you expect.
    Code:
    Dim Outfile As String
    
    On Error GoTo GenStockReports_Err
    
    Outfile = Application.CurrentProject.Path & "" & "DailyStockReport US.xls"
    Debug.Print Outfile
    
    DoCmd.OutputTo acOutputQuery, "QryStockReport", "Excel97-Excel2003Workbook(*.xls)", Outfile, False, "", , acExportQualityPrint
    Set a breakpoint on the debug statement to see what the value is ....




    Instead of
    Code:
    Outfile = Application.CurrentProject.Path & "\" & "DailyStockReport US.xls"
    I would use
    Code:
    Outfile = Application.CurrentProject.Path & "\DailyStockReport US.xls"
    No need to concatenate the slash (in this case)

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

Similar Threads

  1. Replies: 7
    Last Post: 05-23-2019, 07:18 AM
  2. Replies: 12
    Last Post: 09-04-2018, 04:48 PM
  3. Replies: 8
    Last Post: 05-16-2018, 06:04 PM
  4. Replies: 13
    Last Post: 11-23-2015, 09:00 AM
  5. Replies: 1
    Last Post: 09-03-2011, 07:01 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