C# library with events for use in Access procedure vba code (1 Viewer)

amorosik

Member
Local time
Tomorrow, 00:48
Joined
Apr 18, 2020
Messages
379
Hello everyone, I follow the post to be able to complete the work started, but not finished yet

The goal is to create a software library capable of communicating with Access to allow it to perform operations not available with the standard features of the development environment

Just to understand, suppose the library is for the management of the rs232 port of the pc

So once loaded in the References, it can be initialized, and used by calling methods and properties

In my case I am following some examples to create the library in C #, and in particular this one

The example basically consists in calling the FiretestEvent() function from vba code and seeing the response that returns, in practice the TestEvent is launched which is the routine defined as 'event'
Trying that example everything works, apart from the fact that the start of the TestEvent is subordinated to a command that starts from vba

I modified the code, inserting a timer on the dll that every 5 seconds starts the TestEvent routine (the last line of the C # code) and simplifying the vba to a minimum


The dll compiles correctly, in the Access references it loads correctly, the intellisense 'sees' the available classes, and when I start the Access form both the initial ( 5000 Hz) and subsequent beeps (1000 Hz) are heard, so everything seems fine
I would have expected to see the sub in the vba code evtObj_TestEvent () being executed, but it doesn't, the code never goes beyond

To make sure that the code in the library is really executed I inserted some beeps, and actually you can hear both the initial beep (5000,1000) at the form load, and the following ones (1000,100) at 5 seconds from each other, and therefore I am reasonably sure that the TestEvent ("hello ....") line in the dll is executed

The question is: why the routine that receives the events on the vba, the
private Sub evtObj_TestEvent (..) (the one with the breakpoint)
is never called back?
 

sonic8

AWF VIP
Local time
Tomorrow, 00:48
Joined
Oct 27, 2015
Messages
998
The question is: why the routine that receives the events on the vba, the
private Sub evtObj_TestEvent (..) (the one with the breakpoint)
is never called back?
I guess, despite the advanced topic of your project, you made a rookie mistake. ;-)

Your Access form class module is missing Option Explicit. - This is not the actual cause of the problem, but it would've made it more obvious.

Your instance of EventCls is (implicitly) declared local to the Form_Load procedure. It is destroyed once the procedure completes. So, there is no event handler present when your timer fires the event.
 

amorosik

Member
Local time
Tomorrow, 00:48
Joined
Apr 18, 2020
Messages
379
I guess, despite the advanced topic of your project, you made a rookie mistake. ;-)

Your Access form class module is missing Option Explicit. - This is not the actual cause of the problem, but it would've made it more obvious.

Your instance of EventCls is (implicitly) declared local to the Form_Load procedure. It is destroyed once the procedure completes. So, there is no event handler present when your timer fires the event.

No, i don't think is so
Also with EXPLICIT directive on vba code, is the same
Dll is correctly compiled, correctly loaded on Access References, vba code run withou error, but 'event' from dll to vba not arrive
I hear a beep every 5 sec, then code on dll is executed, but only one time evtObj_Evento1 on vba code is called


aaa_vba_test2_event.jpg
 

sonic8

AWF VIP
Local time
Tomorrow, 00:48
Joined
Oct 27, 2015
Messages
998
Your instance of EventCls is (implicitly) declared local to the Form_Load procedure. It is destroyed once the procedure completes. So, there is no event handler present when your timer fires the event.
Regarding the problem described above, you changed from implicit to explicit declaration but didn't fix the actual problem.
Declare your EventCls variable on class (form) level to make it persistent beyond the Form_Load procedure.
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:48
Joined
May 7, 2009
Messages
19,230
i thought you are dumping the Timer thing and yet
you have it in your code?:unsure:
 

amorosik

Member
Local time
Tomorrow, 00:48
Joined
Apr 18, 2020
Messages
379
Regarding the problem described above, you changed from implicit to explicit declaration but didn't fix the actual problem.
Declare your EventCls variable on class (form) level to make it persistent beyond the Form_Load procedure.

Yes, form level declaration of the class was the problem
Now the code is exactly what i need
Thank you very grazie

aaa_vba_test3_event.jpg
 

amorosik

Member
Local time
Tomorrow, 00:48
Joined
Apr 18, 2020
Messages
379
In this case the dll code is extremely simple
But it could also have been considerably more complex, and in this case how do you go about debugging the dll?
Is it possible to run it inside the Visual Studio development environment, setting breakpoints and displaying variables?
I mean, the library provided to be loaded as a 'reference' for Access, is it possible to debug it inside the development environment?
 

sonic8

AWF VIP
Local time
Tomorrow, 00:48
Joined
Oct 27, 2015
Messages
998
Is it possible to run it inside the Visual Studio development environment, setting breakpoints and displaying variables?
I mean, the library provided to be loaded as a 'reference' for Access, is it possible to debug it inside the development environment?
1.) Write unit tests in Visual Studio for everything inside your DLL that can be tested. In your current rs232 port project, there will be parts that are hard or impossible to test, but cover everything else in your DLL.
2.) Create a very simple WinForms project in Visual Studio to test your DLL. This loads quicker and can be easily debugged.
3.) Only after options 1. and 2. are exhausted debug in Access/VBA. Try if "Debug" -> "Attach to process" in Visual Studio allows you to debug the DLL loaded into the Access process. - It should work, but I didn't use in this particular context yet.
 

Users who are viewing this thread

Top Bottom