| Home | Previous Lesson: Running the Example Next Lesson: The RemoteExec Event |
The previous examples have used PowerBuilder as a client in the DDE conversation. Our final example shows how to use PowerBuilder as the server and extract information from it with Word.
For this example, we'll use a different window and another Word macro to initiate the conversation. There's quite a bit of setting up necessary, so once again, we'll go through the various components.
Invoke MS-Word and create a new document PBSERVER.DOC. We need to record a macro TalkToPBServer by selecting Tools > Macro.� Once you record that empty macro, edit the macro and replace the macro definition with the following code:
Sub MAIN
channel = DDEInitiate("PBApp", "DDEServerModule")
DDEPoke channel, "3", "Testing data,(DDE from Word)"
a$ = DDERequest$(channel, "product_no,product_description,product_balance")
Insert a$
DDETerminate channel
End Sub
There are four DDE specific commands:
An Insert command simply inserts the retrieved data into the Word document. The data deals with product information, so we want to retrieve the product_no, product_description and product_balance fields from the database.
Paint a window w_dde_server as shown below. The top one is the DataWindow control dw_1. Assign d_products_maint DataWindow object to dw_1. Other controls are CommandButtons. Leave the names to their defaults. Change the window title to "PB as DDE Server".
Write the following code in the w_dde_server's open event.
dw_1.SetTransObject( SQLCA )
dw_1.Retrieve()
Write the following code to the clicked event for the "Start PowerBuilder DDE Server" CommandButton.
integer lRetVal
lRetVal = StartServerDDE( "PBApp", "DDEServerModule")
If lRetVal <> 1 Then
MessageBox( "Starting DDE server Error", lRetVal )
End If
Even before we can use PowerBuilder as a DDE server, we have to set up an Application and Topic. The code in the clicked event does this.
We use the StartServerDDE() function to set up the Application and Topic. When PowerBuilder executes this command, it informs the operating system, "I am ready to serve as a DDE server. My (Server) name is <First Parameter> and clients can ask data from the <second parameter>".
When PowerBuilder acts as a server, it is necessary to check the commands sent by the client and trigger events explicitly. For this, we have to write code for three window level events:
Let's look at the code for them and see what they do.
| Home | Previous Lesson: Running the Example Next Lesson: The RemoteExec Event |