MetaPro Systems Inc.

Home Services Software Approach Who We Are
Resources Testimonials Contact Us Products

VB Tips & Tricks 21 - 30

Visual Basic Tips & Tricks #21 - Using DLL’s

Starting with VB5.0, developers can create custom DLL’s. These work just like any Microsoft or Third-party supplied DLLs. These are several advantages to creating custom DLL:

  1. Functions can be used in multiple projects without sharing or copying source code.
  2. When you change the logic of a DLL, you can replace the DLL without having to recompile all the application which access the DLL. An exception to this is when the interface changes.
  3. DLL can be accessed from languages other than Visual Basic.

In the next few hints, I will show you how to create, test, install and use DLLs.

Visual Basic Tips & Tricks #22 — Creating a DLL

Continuing our series about Custom DLLs, this tip will cover how to create a DLL.

You create a DLL by opening VB and choosing Active X DLL under New. You now have a new project.A DLL project can have classes and module but not forms.

For a DLL to work, it must have one or more public sub or function. They will be the exposed methods that are available when you call the DLL. You can have additional private subs and functions that are called by the public functions andsubs, and each other.

This example will find the average of two numbers. Add a class module to your project, then add this code:

Public Function dblGetAverage (pdblSide1 As Double, pdblSide2 As Double)

dblGetAverage = (pdblSide1 + pdblSide2) / 2

End Function

Name the class: clsAverage, name the project: msiAverage.

Visual Basic Tips & Tricks #23 – Testing Your DLL

Before you register a DLL, you want to test it. The best way to do this is in a project group. This will allow you to step through your DLL to debug it.

In this example, we will use the DLL we created last time.

  1. Open the project Average. From the File menu, choose Add project, New, Standard EXE.
  2. Change Project1 Name to TestAverage
  3. Right Click on TestAverage and choose Set As Start Up
  4. Under Project Pull Down, find References. You will notice msiAverage. If it is not checked, check it. It should be pointing to average.vbp. This mean that clsAverage can be use just as if you has added it to the project as a class module
  5. Add this code to the form load event of TestAverage

Dim cloAverage As clsAverage

Dim dblFirst As Double

Dim dblSecond As Double

Dim dblAnswer As Double

Set cloAverage = New clsAverage

dblAnswer = cloAverage.dblGetAverage(3, 6)

Debug.Print dblAnswer

  1. Step through to see that dblAnswer is set to 4.5
  2. Save Project as TestAverage
  3. Save form as frmTestAverage
  4. Save project group as TestAverage

Visual Basic Tips & Tricks #24 – Registering Your DLL

  1. Open the project Average.
  2. Under File, choose Make Average.Change the name to msiAverage.
  3. Determine where you want to place the DLL.Use the Explorer-like function to find the directory. Usually it is placed in the Windows/System or Winnt/System directory
  4. Register the DLL by entering the command Regsrv32 msiAverage. I find it easier to associate RegSRV32 with the DLL suffix.That way, I just click on the DLL to register it.

Now you can use the DLL just like any Microsoft or Third-Party DLL.

  1. Open the Project TestAverage.
  2. Save as TestAverageAlone. This is so that you can still use TestAverage to test with if you change the DLL.
  3. Under References, check msiAverage

One final point, if you change your DLL after you compile it, be sure to use Binary Compatibility with the prior version. This option can be found under Project, Properties, Component. Click on binary compatibility and use the Explorer-like process to find your prior version. If you do not do this, you may have to re-link all projects that use the DLL. Note: binary compatibility is not possible if the parameters for any Public Subs or Function change.

Visual Basic Tips & Tricks #25 - Microsoft Announce VB7.0 Features

Microsoft recently announced many of features that it will be include in VB7.0 which is scheduled to be released at the end of this year.

VB has long been criticized by its detractors for not being a true object-oriented language. It is exciting to hear that many of the missing oriented features are being added including inheritance, encapsulation , and polymorphism

For more details see msdn.microsoft.com/vstudio/nextgen/language.asp

The new VB will also enable us to create web pages in a way very similar to how we now create forms.

For more details see msdn.microsoft.com/vstudio/nextgen/webforms.asp

Visual Basic Tips & Tricks #26 - Changing Button Colors

When changing the color of command buttons, it is not enough to change only the BackColor property. You must also change the Style property from Standard(0) to Graphical (1).

Visual Basic Tips & Tricks #27 - Accessing Text Files

Use the File System Object to read and write text file. This is preferable to the Open Statement as in - Open "TESTFILE" For Input As #1

Examples:

Public Sub OpenTextFileTest()

Const ForReading As Integer = 1

Const ForWriting As Integer = 2

Const ForAppending As Integer = 3

Const TristateUseDefault As Integer = -2 'Opens the file using the

' system default.

Const TristateTrue As Integer = -1 'Opens the file as Unicode.

Const TristateFalse As Integer = 0 'Opens the file as ASCII.

Dim fsoAny

Dim txfAny

Set fsoAny = CreateObject("Scripting.FileSystemObject")

Set txfAny = fsoAny.OpenTextFile(App.Path & ".txt", ForWriting, True, TristateFalse)

txfAny.Write "Hello world!"

txfAny.Close

End Sub

Public Sub ReadTextFileTest()

Const ForReading = 1, ForWriting = 2, ForAppending = 3

Dim fsoAny

Dim txfAny

Dim sRecord As String

Set fsoAny = CreateObject("Scripting.FileSystemObject")

Set txfAny = fsoAny.OpenTextFile(App.Path & "/testfile.txt", ForReading, False)

sRecord = txfAny.ReadLine

Debug.Print sRecord

txfAny.Close

End Sub

(Works on VB 6.0 only)

Visual Basic Tips & Tricks #28 - More on Accessing Text Files

Bob,

Why do you say that using the File System Object is preferable to the Open Statement? Using the Open Statement is a simple, straightforward process. Using the FSO to do something simple like reading a text file seems to unnecessarily complicate matters. Am I missing something here?

Tim Rude

__________________________________________________________________________

The File System Object is a feature new to Visual Basic 6. It is an object-oriented method of access files; whereas, the Open Statement is not.It’s a matter of preference because the Open Statement will work in the example I have shown. I prefer using the File System Object even for simple examples like those shown in Tip #27.

File System Object has many new features. Please check on-line help or MSDN Library for details.

Here are a couple of examples:

Dim fso As FileSystemObject

Dim ts As TextStream

' using early binding here and be sure to include Microsoft Scripting

Runtime as a reference

Set fso = New FileSystemObject

Set ts = fso.OpenTextFile("c:\boot.ini", ForReading, False)

' read a file, check for end of file

Do While ts.AtEndOfStream <> True

Debug.Print ts.ReadLine

Loop

ts.Close

' Copy a file

Call fso.CopyFile("c:\boot.ini", "c:\boot.old")

Visual Basic Tips & Tricks #29 - Using VB with Outlook

It is easy to integrate VB and Outlook. For example the following code allows you to send the same message to a group of people without using a distribution list.

Note that this code is not complete. It will be necessary to write the missing routines.

------------------------------------------------------------------------------------------------------------Dim Dim objOutlook As Object

Dim objmail As Object

Dim MyAttachments As Object

Dim MyItem As Object

Dim cloDatabase As clsDatabase

Dim bEof As Boolean

Dim sMailAddress As String

Dim sUser2 As String

Dim sFirstLine As String

Dim sLine As String

Dim iFile As Integer

Dim iIndex As Integer

msText = sBuildText(sFirstLine) ‘ text for subject line

Set objOutlook = CreateObject("Outlook.Application")

cloDatabase.OpenDatabase

cloDatabase.OpenRecordset

‘ you can read the address from a database or a flat file.

‘ In this example, I am reading from an MDB file

Do Until bEof = True

' Create Mail message

Set objmail = objOutlook.CreateItem(0)

With objmail

.Subject = sFirstLine

.Body = msText ‘ a text file containing the body of the message

Set MyAttachments = objmail.attachments

MyAttachments.Add "e:\doc\resumes\ResumeJanuary2000.rtf"

‘ Add an attachment

Call cloDatabase.NextRecord(sMailAddress, bEof, sLine)

‘ Get the next address

.To = sMailAddress

.Send ‘Move message to Outbox, ready to send

End With

Set objmail = Nothing

Loop

Set objOutlook = Nothing

End Sub

Visual Basic Tips & Tricks #30 - Using Hyperlinks with VB

This example is a good first step, if you have never done any internet programming in VB.

Let us say you are designing a contacts management system. One of the fields on the screen is the web address of the company or individual represented by the current record. When the user double-clicks on that field, the system should take them to that web address. This assumes that the computer running the application has a web browser,

Generally true in today’s web environment.

The code uses the API call ShellExecute.

------------------------------------------------------------------------------------------------------------

Private Sub txtFields_DblClick(Index As Integer)

Call GoToWeb(txtFields(Index).Text)

End Sub

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Public Const SW_SHOWNORMAL = 1

Private Sub GoToWeb(psWebAddress As String)

Dim lSuccess As Long

lSuccess = ShellExecute(0&, vbNullString, psWebAddress, vbNullString, "C:\", SW_SHOWNORMAL)

End Sub