ActiveX DLLs

Using an ActiveX DLL

In this exercise, you will create a DLL in Visual Basic and then use the DLL to create an object in Microsoft Access.

* To create a DLL in Visual Basic

  1. Create a new ActiveX DLL project in Visual Basic.
  2. Name the project, SampleDLL .
  3. In the General Declarations section of the Class1 module, type the following code:
     Private UserName As String Private strMessage As String Public Sub SayHello() MsgBox "Hello " & UserName & "!" End Sub Public Property Let Name(Name As String) UserName = Name End Property Public Property Get Message() strMessage = "Hello from DLL." Message = strMessage End Property 
  4. Start the application and leave it running.

Note

Normally you would need to register the DLL before calling it. Leaving it running in Visual Basic allows us to test the DLL because it is currently loaded in memory.


* To use the DLL from a different application

  1. Start Microsoft Access and click Blank Database and then click OK .
  2. Save the new database as Lab8.mdb in the WA\Practice\Ch08 directory.
  3. When the database dialog box appears, click the Forms tab and click New .
  4. Select Design View and click OK .
  5. Add a Command button on Form1 .
  6. When the Command Button Wizard appears, click Cancel .
  7. Add a Label on Form1 .
  8. Type the word nothing for the label's caption.
  9. Right-click Command0 and click Properties .
  10. Click the Events tab and select On Click .
  11. Click the ellipses to the right of the On Click event.
  12. Select Code Builder , and click OK .
  13. From the Tools menu, click References .
  14. Scroll down the list of Available References and put a check mark next to SampleDLL and click OK .

    This will set a reference to your DLL project currently running in Visual Basic.

  15. In the Command0_Click event, type the following code:
     Dim MyObject As New Class1 MyObject.Name = InputBox("What is your name?") Label1.Caption = MyObject.Message MyObject.SayHello 
  16. From the Run menu, click Go / Continue .
  17. Click the Command0 button.
  18. Enter your name and click OK .
  19. Switch to your Visual Basic application.

    A message box will appear with your name in it.

  20. Click OK .
  21. Switch to the Microsoft Access database application.

    Notice that the label caption has changed.

  22. Close Form1 and click Yes to save changes.

    Save the form as frmLab8 .

  23. Exit Microsoft Access.
  24. Switch to Visual Basic.
  25. Stop the application and exit Visual Basic. You can save the project to WA\Practice\Ch08 if you prefer.


Microsoft Windows Architecture Training
Microsoft Windows Architecture for Developers Training Kit
ISBN: B00007FY9D
EAN: N/A
Year: 1998
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net