Results 1 to 3 of 3

Thread: Office VBA automation

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    3

    Office VBA automation

    Hi everyone,
    I'm trying to make a MS Access add-in using VB.NET, the solution will be
    used for a maternal care company. The add-in works fine, however now the
    VB.NET add-in needs to call VBA code in the MS Access database and also
    pass parameters (i.e. can't use macros).


    Does anyone know if it's possible to do this? If so, how?

    As an example I would like to do the following:

    VB.NET add-in sample code:
    VB Code:
    1. Dim mappAccess As Microsoft.Office.Interop.Access.Application
    2.         Dim OrderArgs As String
    3.         Const strConPathToSamples = "C:\Documents and Settings\Anders\" _
    4.                     & "My Documents\"
    5.  
    6.         strDB = strConPathToSamples & "MyAccessDatabase.mdb"
    7.        [COLOR=Lime] ' Gets current instance of MyAccessDatabase.[/COLOR]        
    8.         mappAccess = _
    9.            GetObject(strDB).Application
    10.  
    11.  [COLOR=Lime]       'Open a form[/COLOR]
    12.         OrderArgs=<something>
    13.         mappAccess.DoCmd.OpenForm("Orders", , , , , , OrderArgs)
    14. [COLOR=Lime]        'Run a macro[/COLOR]
    15.         mappAccess.DoCmd.RunMacro("Order_macro")
    16.  
    17. [B]'Can you call a VBA sub-procedure located in a Class module or a standard module, and if so how?[/B]
    18.         Dim OrderNr as String = "A0001"
    19.         Dim OrderStatus as Integer = 0
    20.         mappAccess.<what should be written here?> ("UpdateOrder", OrderNr, OrderStatus)


    Sample MS Access VBA code from MyAccessDatabase.mdb, inside standard module "Module1":

    VB Code:
    1. 'This function is located in a MyAccessDatabase class module or standard
    2. 'VBA module and should be called by MyAccessAddin.dll:
    3.         Public Function UpdateOrder(OrdNr As String, OrdStatus As Integer) As Integer
    4.         'Do what you gotta do
    5.  
    6.         'Return
    7.         UpdateOrder = 0
    8.         End Function

    I have been unable to find any information about this. Does anyone know
    anything about this?

    Thanks for any help,
    Anders, programmer

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Office VBA automation

    It may be easier to just write the fuinction in .net instead of running it inside of Access. Not to mention that this way your code will be centralized in your .net project.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    3

    Re: Office VBA automation

    Well, thanks for the help, why didn't I think about that!!

    Excuse me for the irony *no harm ment , but my example naturally assumes that the call from the add-in back into MS Access is needed, and that a macro isn't sufficient since it won't allow passing any parameters (or least I think so).

    I guess the answer is that one has to use some ugly solution with dummy forms using openargs as parameters, together with the DoCmd.Openform method.

    Cheers,
    Anders

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width