PC Review


Reply
Thread Tools Rate Thread

Creation & Implementation of VB6 DLL in VBA to raise events

 
 
Trip
Guest
Posts: n/a
 
      20th Nov 2006
Hello All,

I'm struggling a bit and would appreciate any direction anyone might be
able to provide...

I created the following simple ActiveX DLL in VB6, registered the DLL
in windows, and included the references in the two workbooks (described
below). This code resides in "Class1" of the DLL:
____________________________________________________
Public Event DataRecived(Price As String, MarketIndex As Byte)

Public Sub DateRec(Price As String, MarketIndex As Byte)

RaiseEvent DataRecived(Price, MarketIndex)
MsgBox "data received by DLL: " & Price & ", " & MarketIndex

End Sub
___________________________________________________

I then created a workbook to send data via the DLL. I placed the
following code in Sheet1..
__________________________________________________
Public WithEvents SendData As DataEvent.Class1

Sub DataSend()

Set SendData = New DataEvent.Class1
Call SendData.DateRec("12345", 1)
Set SendData = Nothing

End Sub
_________________________________________________

and I created a second workbook to receive the data. This code was
placed in Sheet1 of this workbook:
_______________________________________________
Public WithEvents ReceiveData As DataEvent.Class1

Sub initialize()

Set ReceiveData = New DataEvent.Class1

End Sub

Sub ReceiveData_DataRecived(Price As String, MarketIndex As Byte)

MsgBox Price & ", " & MarketIndex

End Sub
_______________________________________________

After initializing the the "ReceiveData" object in the "data receiving"
workbook, when I raise the event in the "data sending" workbook by
issuing the command:
Call SendData.DateRec("12345", 1)
the DLL manages to give me the shown msgbox, indicating that it is in
fact receiving the data. Yet, the second workbook does not respond to
the:
RaiseEvent DataRecived(Price, MarketIndex)
issued by the DLL. It is as though the event never fired.

Any ideas as to why??

TIA

Trip

 
Reply With Quote
 
 
 
 
NickHK
Guest
Posts: n/a
 
      21st Nov 2006
If I understand you correctly, you are misunderstanding the way these DLL
object work in VB/VBA.
Each of your :
Public WithEvents SendData As DataEvent.Class1
creates a separate instance of the class. As such there is no connection
between the instances, so your attempt to trigger an event in a different
instance will not work.

Depending on what you are trying to achieve, you could pass a reference to
the recieving WB to your DLL class. Although it can't raise a custom event,
it could then interact with that WB.

Or maybe you can work with GetObject on the send data instance.

NickHK

"Trip" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello All,
>
> I'm struggling a bit and would appreciate any direction anyone might be
> able to provide...
>
> I created the following simple ActiveX DLL in VB6, registered the DLL
> in windows, and included the references in the two workbooks (described
> below). This code resides in "Class1" of the DLL:
> ____________________________________________________
> Public Event DataRecived(Price As String, MarketIndex As Byte)
>
> Public Sub DateRec(Price As String, MarketIndex As Byte)
>
> RaiseEvent DataRecived(Price, MarketIndex)
> MsgBox "data received by DLL: " & Price & ", " & MarketIndex
>
> End Sub
> ___________________________________________________
>
> I then created a workbook to send data via the DLL. I placed the
> following code in Sheet1..
> __________________________________________________
> Public WithEvents SendData As DataEvent.Class1
>
> Sub DataSend()
>
> Set SendData = New DataEvent.Class1
> Call SendData.DateRec("12345", 1)
> Set SendData = Nothing
>
> End Sub
> _________________________________________________
>
> and I created a second workbook to receive the data. This code was
> placed in Sheet1 of this workbook:
> _______________________________________________
> Public WithEvents ReceiveData As DataEvent.Class1
>
> Sub initialize()
>
> Set ReceiveData = New DataEvent.Class1
>
> End Sub
>
> Sub ReceiveData_DataRecived(Price As String, MarketIndex As Byte)
>
> MsgBox Price & ", " & MarketIndex
>
> End Sub
> _______________________________________________
>
> After initializing the the "ReceiveData" object in the "data receiving"
> workbook, when I raise the event in the "data sending" workbook by
> issuing the command:
> Call SendData.DateRec("12345", 1)
> the DLL manages to give me the shown msgbox, indicating that it is in
> fact receiving the data. Yet, the second workbook does not respond to
> the:
> RaiseEvent DataRecived(Price, MarketIndex)
> issued by the DLL. It is as though the event never fired.
>
> Any ideas as to why??
>
> TIA
>
> Trip
>



 
Reply With Quote
 
Trip
Guest
Posts: n/a
 
      21st Nov 2006
Ahhhh! ok, I see. Thanks Nick! Well, I've got to do a bit more
research and then it's back-to-the-drawing-board.

Any further suggestions you may have in how to implement this would be
great.

Thanks!

Trip


NickHK wrote:
> If I understand you correctly, you are misunderstanding the way these DLL
> object work in VB/VBA.
> Each of your :
> Public WithEvents SendData As DataEvent.Class1
> creates a separate instance of the class. As such there is no connection
> between the instances, so your attempt to trigger an event in a different
> instance will not work.
>
> Depending on what you are trying to achieve, you could pass a reference to
> the recieving WB to your DLL class. Although it can't raise a custom event,
> it could then interact with that WB.
>
> Or maybe you can work with GetObject on the send data instance.
>
> NickHK
>
> "Trip" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hello All,
> >
> > I'm struggling a bit and would appreciate any direction anyone might be
> > able to provide...
> >
> > I created the following simple ActiveX DLL in VB6, registered the DLL
> > in windows, and included the references in the two workbooks (described
> > below). This code resides in "Class1" of the DLL:
> > ____________________________________________________
> > Public Event DataRecived(Price As String, MarketIndex As Byte)
> >
> > Public Sub DateRec(Price As String, MarketIndex As Byte)
> >
> > RaiseEvent DataRecived(Price, MarketIndex)
> > MsgBox "data received by DLL: " & Price & ", " & MarketIndex
> >
> > End Sub
> > ___________________________________________________
> >
> > I then created a workbook to send data via the DLL. I placed the
> > following code in Sheet1..
> > __________________________________________________
> > Public WithEvents SendData As DataEvent.Class1
> >
> > Sub DataSend()
> >
> > Set SendData = New DataEvent.Class1
> > Call SendData.DateRec("12345", 1)
> > Set SendData = Nothing
> >
> > End Sub
> > _________________________________________________
> >
> > and I created a second workbook to receive the data. This code was
> > placed in Sheet1 of this workbook:
> > _______________________________________________
> > Public WithEvents ReceiveData As DataEvent.Class1
> >
> > Sub initialize()
> >
> > Set ReceiveData = New DataEvent.Class1
> >
> > End Sub
> >
> > Sub ReceiveData_DataRecived(Price As String, MarketIndex As Byte)
> >
> > MsgBox Price & ", " & MarketIndex
> >
> > End Sub
> > _______________________________________________
> >
> > After initializing the the "ReceiveData" object in the "data receiving"
> > workbook, when I raise the event in the "data sending" workbook by
> > issuing the command:
> > Call SendData.DateRec("12345", 1)
> > the DLL manages to give me the shown msgbox, indicating that it is in
> > fact receiving the data. Yet, the second workbook does not respond to
> > the:
> > RaiseEvent DataRecived(Price, MarketIndex)
> > issued by the DLL. It is as though the event never fired.
> >
> > Any ideas as to why??
> >
> > TIA
> >
> > Trip
> >


 
Reply With Quote
 
NickHK
Guest
Posts: n/a
 
      22nd Nov 2006
What are you trying to achieve ?

Consider that worksheets modules are basically class modules, with some
signatures already written. That means you can add your own Public routines
and call them from another worksheet. e.g.
However, routines in a .bas module may be sufficient.

<Worksheet(1)>
Public Function Calc(Num1 As Long, Num2 As Long) As Long
Calc = Num1 + Num2
End Function

Public Function ChangeMe(WS As Worksheet, Num2 As Long) As Long
WS.Range("A1").Value = Num2
End Function
</Worksheet(1)>

<Worksheet(2)>
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox Worksheets(1).Calc(10, 10)
Call Worksheets(1).ChangeMe(Me, 100)
End Sub
</Worksheet(2)>

NickHK

"Trip" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Ahhhh! ok, I see. Thanks Nick! Well, I've got to do a bit more
> research and then it's back-to-the-drawing-board.
>
> Any further suggestions you may have in how to implement this would be
> great.
>
> Thanks!
>
> Trip
>
>
> NickHK wrote:
> > If I understand you correctly, you are misunderstanding the way these

DLL
> > object work in VB/VBA.
> > Each of your :
> > Public WithEvents SendData As DataEvent.Class1
> > creates a separate instance of the class. As such there is no connection
> > between the instances, so your attempt to trigger an event in a

different
> > instance will not work.
> >
> > Depending on what you are trying to achieve, you could pass a reference

to
> > the recieving WB to your DLL class. Although it can't raise a custom

event,
> > it could then interact with that WB.
> >
> > Or maybe you can work with GetObject on the send data instance.
> >
> > NickHK
> >
> > "Trip" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > Hello All,
> > >
> > > I'm struggling a bit and would appreciate any direction anyone might

be
> > > able to provide...
> > >
> > > I created the following simple ActiveX DLL in VB6, registered the DLL
> > > in windows, and included the references in the two workbooks

(described
> > > below). This code resides in "Class1" of the DLL:
> > > ____________________________________________________
> > > Public Event DataRecived(Price As String, MarketIndex As Byte)
> > >
> > > Public Sub DateRec(Price As String, MarketIndex As Byte)
> > >
> > > RaiseEvent DataRecived(Price, MarketIndex)
> > > MsgBox "data received by DLL: " & Price & ", " & MarketIndex
> > >
> > > End Sub
> > > ___________________________________________________
> > >
> > > I then created a workbook to send data via the DLL. I placed the
> > > following code in Sheet1..
> > > __________________________________________________
> > > Public WithEvents SendData As DataEvent.Class1
> > >
> > > Sub DataSend()
> > >
> > > Set SendData = New DataEvent.Class1
> > > Call SendData.DateRec("12345", 1)
> > > Set SendData = Nothing
> > >
> > > End Sub
> > > _________________________________________________
> > >
> > > and I created a second workbook to receive the data. This code was
> > > placed in Sheet1 of this workbook:
> > > _______________________________________________
> > > Public WithEvents ReceiveData As DataEvent.Class1
> > >
> > > Sub initialize()
> > >
> > > Set ReceiveData = New DataEvent.Class1
> > >
> > > End Sub
> > >
> > > Sub ReceiveData_DataRecived(Price As String, MarketIndex As Byte)
> > >
> > > MsgBox Price & ", " & MarketIndex
> > >
> > > End Sub
> > > _______________________________________________
> > >
> > > After initializing the the "ReceiveData" object in the "data

receiving"
> > > workbook, when I raise the event in the "data sending" workbook by
> > > issuing the command:
> > > Call SendData.DateRec("12345", 1)
> > > the DLL manages to give me the shown msgbox, indicating that it is in
> > > fact receiving the data. Yet, the second workbook does not respond to
> > > the:
> > > RaiseEvent DataRecived(Price, MarketIndex)
> > > issued by the DLL. It is as though the event never fired.
> > >
> > > Any ideas as to why??
> > >
> > > TIA
> > >
> > > Trip
> > >

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to raise async events nadeem_far@yahoo.com Microsoft C# .NET 11 10th Dec 2007 03:54 AM
Raise the events in VB 6.0 where the events are defined in VB.Net Rethish R Microsoft VB .NET 1 25th Nov 2004 11:15 AM
How to Raise events from a form? Graham Blandford Microsoft VB .NET 4 16th Jun 2004 07:06 PM
How to raise events programatically? asr Microsoft Dot NET 3 3rd Nov 2003 05:24 PM
Why don't events raise in Sub New? IcedCrow Microsoft VB .NET 2 19th Sep 2003 03:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:55 PM.