Answered Calling C# dll from Access VBA

  • Friday, May 18, 2007 2:42 PM
     
     

     I was wondering if anyone has tried calling a c# dll from Access VBA code ?

     

     

Answers

  • Friday, May 18, 2007 6:13 PM
     
     Answered

     

    Its not hard to do.  Essentially you have to create a COM wrapper for the C# library.  To do this

    compile the C# dll in VS.NET 2005 using the “Register for COM Interop” compiler option.  Once the library is compiled you run regasm to create a type library for the dll like so: 

     

    regasm TestThunk1.dll /tlb:TestThunk1.tlb 

     

    Then you can add a reference to the tlb file from Access VBA.

     

All Replies

  • Friday, May 18, 2007 6:13 PM
     
     Answered

     

    Its not hard to do.  Essentially you have to create a COM wrapper for the C# library.  To do this

    compile the C# dll in VS.NET 2005 using the “Register for COM Interop” compiler option.  Once the library is compiled you run regasm to create a type library for the dll like so: 

     

    regasm TestThunk1.dll /tlb:TestThunk1.tlb 

     

    Then you can add a reference to the tlb file from Access VBA.

     

  • Monday, May 21, 2007 7:38 AM
     
     

     

    First of all thanks for your help,

     

    I added reference to my Access but can you help me about how I can use my function in my VBA code. For example I will use it in a Form. When I press a button at the Form, function must work and it must write the result on the screen. You can see my dll code below.

     

    Thanks in advance

     

     

    using System;

    using System.IO.Ports;

    namespace GD

    {

    public class PortConnection

    {

    private static string data;

    private static double result;

     

    public static double tart()

    {

    SerialPort Terazi = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);

    Terazi.Open();

    Terazi.Write("A");

    Terazi.ReadByte();

    data = Terazi.ReadExisting();

    result = double.Parse(data);

    Terazi.Close();

    return result;

    }

    }

    }

  • Monday, May 21, 2007 5:46 PM
     
     

    Yes essentially you just add a button to the Access form and then add a click event to the button.

    In the click event you create a new object of type PortConnection and then call the tart method.

     

    Dim objPortConnection as GD.PortConnection

    Dim d as double

    set objPortConnection = New GD.PortConnection

     

    d = objPortConnection.tart

     

    txtDisplayValue.Text = d ' <- This would be some text box to display the value

  • Wednesday, May 23, 2007 7:14 AM
     
     

    Dear Matt;

    I solved my problem with the way you described.Thanks a lot for your kind help.

    Best wishes.

     

    Berk