none
opening winforms RRS feed

  • Question

  • Hi

    Is it possible to open modal/non-modal vb.net winforms from access somehow? If possible also passing parameters to to/from forms.

    Thanks

    Tuesday, October 11, 2016 5:28 PM

Answers

  • There are several ways to do this.

    The interop tool kit 2.1 was supported up to VS2010, but I am currently using it with VS2013, and it works really nice. (you have to copy the templates from the vs2010 folder to the vs2013 folder and edit a config file – easy if you want I will outline how to do this).

    And you can pass values etc. to the .net form, but you have to setup public properties for the vb.net class that holds and launches the form. (so you can't reference controls directly - you have to build property stubs to shuttle the data).

    The above as noted works quite well. You can also create a usercontrol in vb.net and that will let you embed the control as an ActiveX (which can by the way load a vb.net form right inside of Access). This to me is “really” the reason to use the forms interop – it lets you create ActiveX controls that drop into Access forms. This allows one to use .net controls on access forms.

    While the interop forms toolkit is not really being actively developed these days – it still a viable choice IMHO. It uses all of the standard bits and parts built into .net – that’s not going away anytime soon. (you don’t need the kit – it just does some dirty setup work for you - there nothing in the toolkit that is special to .net framework).

    On the other hand a REALLY easy was to do this is in vb.net simply create a class project. It is only a few lines of code.

    So in vb.net, our code would be this:

    Imports System.Runtime.InteropServices

    <ClassInterface(ClassInterfaceType.AutoDual)> _

    Public Class Class1

        Public Sub ShowForm1()

            Dim f1 As New Form1

            f1.Show()

        End Sub

    End Class

    Note in above project, I also added a form1 to the project. That form can do anything a .net form can. The form added was Form1 (the default in vb.net).

    Now simple compile the above net project above (VS will default the register for interop settings for you).

    Note that the above is ALL you need! Please ignore those articles that kill you with details about GUID's and buiding a "interface" for the COM ocnsumer - you don't need all that junk. In fact the above WILL SHOW intel-sense in the VBA editor!

    Now, in Access, the VBA code is this:

    Option Compare Database

    Option Explicit

    Dim vbForm     As New clsVBform.Class1

    Private Sub Command0_Click()

       vbForm.ShowForm1

    End Sub

    When I run the above VBA code, I get this:

    So VERY little code in net is required to create a COM object for use with Access.

    Regards,

    Albert D. Kallal (Access MVP)

    Edmonton, Alberta Canada


    Tuesday, October 11, 2016 9:58 PM

All replies

  • We used to do that with "Interop", but we don't do that anymore since the technology has not been maintained.

    You can certainly run two applications side-by-side. That's no news. Modal: not if these are two separate applications. This is a Windows feature, nothing to do with Access. Passing information back and forth could be done as well, in a variety of ways. Maybe it's good enough to pass information via the command line at startup time? Maybe use the database itself for that? Maybe you want to use some Windows API calls? A lot depends on your skills as a developer.


    -Tom. Microsoft Access MVP

    Tuesday, October 11, 2016 8:21 PM
  • There are several ways to do this.

    The interop tool kit 2.1 was supported up to VS2010, but I am currently using it with VS2013, and it works really nice. (you have to copy the templates from the vs2010 folder to the vs2013 folder and edit a config file – easy if you want I will outline how to do this).

    And you can pass values etc. to the .net form, but you have to setup public properties for the vb.net class that holds and launches the form. (so you can't reference controls directly - you have to build property stubs to shuttle the data).

    The above as noted works quite well. You can also create a usercontrol in vb.net and that will let you embed the control as an ActiveX (which can by the way load a vb.net form right inside of Access). This to me is “really” the reason to use the forms interop – it lets you create ActiveX controls that drop into Access forms. This allows one to use .net controls on access forms.

    While the interop forms toolkit is not really being actively developed these days – it still a viable choice IMHO. It uses all of the standard bits and parts built into .net – that’s not going away anytime soon. (you don’t need the kit – it just does some dirty setup work for you - there nothing in the toolkit that is special to .net framework).

    On the other hand a REALLY easy was to do this is in vb.net simply create a class project. It is only a few lines of code.

    So in vb.net, our code would be this:

    Imports System.Runtime.InteropServices

    <ClassInterface(ClassInterfaceType.AutoDual)> _

    Public Class Class1

        Public Sub ShowForm1()

            Dim f1 As New Form1

            f1.Show()

        End Sub

    End Class

    Note in above project, I also added a form1 to the project. That form can do anything a .net form can. The form added was Form1 (the default in vb.net).

    Now simple compile the above net project above (VS will default the register for interop settings for you).

    Note that the above is ALL you need! Please ignore those articles that kill you with details about GUID's and buiding a "interface" for the COM ocnsumer - you don't need all that junk. In fact the above WILL SHOW intel-sense in the VBA editor!

    Now, in Access, the VBA code is this:

    Option Compare Database

    Option Explicit

    Dim vbForm     As New clsVBform.Class1

    Private Sub Command0_Click()

       vbForm.ShowForm1

    End Sub

    When I run the above VBA code, I get this:

    So VERY little code in net is required to create a COM object for use with Access.

    Regards,

    Albert D. Kallal (Access MVP)

    Edmonton, Alberta Canada


    Tuesday, October 11, 2016 9:58 PM
  • Celeste, I disagree about this being the answer. The proposed solution depends on a library that is not maintained by MSFT anymore since 2010 (to my chagrin), and its System Requirements do not list modern operating systems. At most this can be a qualified recommendation.

    -Tom. Microsoft Access MVP

    Wednesday, October 12, 2016 2:42 AM
  • I going to re-edit this long and in-necessary post.

    No the example does NOT use the forms tool kit.

    The example simply creates a COM object in .net and does NOT create ActiveX control like the forms tool kit allows you to!

    If you fire up any recent version of Visual Studio (including the free express editions), and then simply type in the above code and wack compile (build project). You are done!.  You now be able to use the above code as per the above example in Access. This has ZERO to do with the interop forms tool kit. I would suggest one force the project to x86 if using Access x32, and x64 if using Access x64. (and if using the express editions, then you have to manually do a regasm to register the “tiny” .dll the above code creates). The full editions of VS do this regasm for you.

    Regards,

    Albert D. Kallal

    Edmonton, Alberta Canada


    Wednesday, October 12, 2016 5:23 AM