Results 1 to 5 of 5
  1. #1
    dccjr3927 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    130

    Error7866 When RunCode a Function

    Good Afternoon:



    I normally do not use macros, and code everything in VBA. However, I am setting up a scheduled action in Windows and was told that the best thing to do was create a macro for the action. I have a split db, both front- and backends are .accdb. I have a macro called ArchiveData, and in this macro is a RunCode for ExecArchive(). The code in the function is:

    Code:
    Function ExecArchive()
    On Error GoTo Error_Handler
        Dim oAccess As Access.Application
        Dim sDb, SMacroName As String
        Dim bCloseExtDb As Boolean
            
        sDb = "J:\Databases-DO NOT MOVE\XX Tracking System\LarmData\Larm Data.accdb"
        SMacroName = "CopyArchiveData"
        bCloseExtDb = True
            
        Set oAccess = CreateObject("Access.Application")
        
        oAccess.OpenCurrentDatabase Chr(34) & sDb & Chr(34)
        oAccess.DoCmd.RunMacro SMacroName
        
    Error_Handler_Exit:
        On Error Resume Next
        If bCloseExtDb = True Then oAccess.CloseCurrentDatabase
        Set oAccess = Nothing
        Exit Function
        
    Error_Handler:
        MsgBox "The Following error has occured." & vbCrLf & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Source: ExecExternalMacro" & vbCrLf & _
               "Error Description: " & Err.Description, _
               vbCritical, "An Error has Occured!"
            Resume Error_Handler_Exit
    End Function
    This code is meant to run a macro (CopyArchiveData) in the backend. Not sure if this is relevant but this CopyArchiveData calls a RunDataMacro in the source table to copy records to another table. If has a ForEachRecord from the source and a CreateRecord to the archive table.

    The backend is not open when I run it and have double checked the file path and name. I must be missing something here. I am thoroughly and utterly confused with this macro to call a function to call a macro structure. I would appreciate any assistance that might be offered.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    You call a macro which calls a function which calls a macro which calls a Data Macro?

    I schedule a VBScript to run. It executes a VBA procedure or a macro. Example:
    Code:
    Dim ObjAccess
    Set ObjAccess = CreateObject("Access.application"
    ObjAccess.visible = false
    ObjAccess.OpenCurrentDatabase("C:\Users\Owner\June\LL\Umpires.accdb") 
    ObjAccess.UserControl = False
    ObjAccess.Run ("test")
    ObjAccess.Run "MySub", "argument"
    ObjAccess.DoCmd.RunMacro "MyMacro" 
    ObjAccess.Quit
    Set ObjAccess = Nothing
    Why are you using Data Macros? I never have.

    Why are you copying records?
    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.

  3. #3
    dccjr3927 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    130
    It seems that I went down the wrong rabbit hole. It started with learning how to create a scheduled event in Windows Task Scheduler to export and email a file at a given time each day. All of the examples that I came used macros. Without thinking, I just followed the examples in the forums. Once I began, I started trying to run multiple functions during the same scheduled task, and the chain got longer and longer.

    All of the examples that I found used something like:

    C:\Path To\MSACCESS.EXE C:\Path To\Database.ACCDB /x MacroName

    to run the macro in Task Scheduler. How would I call the VBScript in Task Scheduler? Will I need a .bat file? I have never created one. Can you point me to a resource?

    I like the idea of doing it all in VBA as I know how to code for what I need. Macros confuse me. LOL.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    That is command line code using switches.

    I use a VBScript file. It ends in .vbs. Can create it with Notepad then change .txt extension to .vbs and that makes it an executable, like a .bat.

    I don't have a particular resource, just Google it.
    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.

  5. #5
    dccjr3927 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    130
    As always, thanks for the assistance. I am going that route vs. Too-Many-Macros. This just got much less confusing. I should have come here first.

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

Similar Threads

  1. RunCode - Module to print PDF
    By shank in forum Modules
    Replies: 4
    Last Post: 05-15-2019, 04:30 PM
  2. Replies: 2
    Last Post: 02-26-2017, 11:31 AM
  3. Replies: 8
    Last Post: 11-04-2014, 10:44 PM
  4. Using RunCode Macro
    By boywonder in forum Macros
    Replies: 5
    Last Post: 01-13-2014, 06:22 AM
  5. RunCode in macro not working
    By LilMissAttack in forum Access
    Replies: 0
    Last Post: 05-27-2010, 05: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