|Home||Services||Software Approach||Who We Are|
Visual Basic Tips & Tricks #21 - Using DLLs
Starting with VB5.0, developers can create custom DLLs. These work just like any Microsoft or Third-party supplied DLLs. These are several advantages to creating custom DLL:
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
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.
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)
Visual Basic Tips & Tricks #24 Registering Your DLL
Now you can use the DLL just like any Microsoft or Third-Party DLL.
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
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.
Set fsoAny = CreateObject("Scripting.FileSystemObject")
Set txfAny = fsoAny.OpenTextFile(App.Path & ".txt", ForWriting, True, TristateFalse)
txfAny.Write "Hello world!"
Public Sub ReadTextFileTest()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim sRecord As String
Set fsoAny = CreateObject("Scripting.FileSystemObject")
Set txfAny = fsoAny.OpenTextFile(App.Path & "/testfile.txt", ForReading, False)
sRecord = txfAny.ReadLine
(Works on VB 6.0 only)
Visual Basic Tips & Tricks #28 - More on Accessing Text Files
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?
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.Its 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
' 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")
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)
.Subject = sFirstLine
.Body = msText a text file containing the body of the message
Set MyAttachments = objmail.attachments
Add an attachment
Call cloDatabase.NextRecord(sMailAddress, bEof, sLine)
Get the next address
.To = sMailAddress
.Send Move message to Outbox, ready to send
Set objmail = Nothing
Set objOutlook = Nothing
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 todays web environment.
The code uses the API call ShellExecute.
Private Sub txtFields_DblClick(Index As Integer)
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)