
		       Nod Programming Inc. VB Help Index

This is intended for free use.  The code here is for various skill levels, 
anyone from beginers to advanced programers can use these.  Do what you wish 
with the code it is free for you to use and manipulate!

****************************************************************************

Tip for finding a leap year:

Here is a nice function for finding a leap year.  The trick is to not only
dividing the year by 4, but also test for division by 100 and 400.

Public Function IsLeapYear(iYear As Integer)
    '-- Check for leap year
    If (iYear Mod 4 = 0) And _
    ((iYear Mod 100 <> 0) Or (iYear Mod 400 = 0)) Then
        IsLeapYear = True
    Else
        IsLeapYear = False
    End If
End Function

***************************************************************************

Using Base Address for In-Process Components to speed up the loading phase:

When you create an in-process component and load it in your Visual Basic
application, the process loads at a base address in memory.

How can you change the Base Address of your In-Process Component?
To enter the base address for your component, open the Project Properties
dialog box and select the Compile tab. The address is entered in the DLL
Base Address box as an unsigned decimal or hexadecimal integer.
The default value is &H11000000 (285,212,672). If you neglect to change
this value, your component will conflict with every other in-process
component compiled using the default. Staying well away from this address
is recommended.

Choose a base address between 16 megabytes (16,777,216 or &H1000000) and
two gigabytes (2,147,483,648 or &H80000000). The base address must be a
multiple of 64K. The memory used by your component begins at the initial
base address and is the size of the compiled file, rounded up to the next
multiple of 64K.

Your program cannot extend above two gigabytes, so the maximum base
address is actually two gigabytes minus the memory used by your component.
Executables will usually load at the 4 megabyte logical address. The
region below 4 megabytes is reserved under Windows 95, and regions above
two gigabytes are reserved by both Windows 95 and Windows NT.

A good way to make sure all your components have a different base address
is to keep track of all them, and create a random selective tool to create
new unique base addresses. This way your components will not conflict with
each other.

***************************************************************************

Using the Win32 API to write to the NT EventLog:

How to write to the NT EventLog using the App object.
This method has 2 limitations:

1) You cannot use the code during a debug session.
2) The source entry in the Event Log is always VBRuntime

Using the Win32 API alleviates these problems.  Enter the following code
in the General Declarations of a module:

    Declare Function RegisterEventSource Lib "advapi32.dll" Alias _
        "RegisterEventSourceA" (ByVal lpUNCServerName As String, _
        ByVal lpSourceName As String) As Long

    Declare Function DeregisterEventSource Lib "advapi32.dll" ( _
        ByVal hEventLog As Long) As Long

    Declare Function ReportEvent Lib "advapi32.dll" Alias _
      "ReportEventA" (ByVal hEventLog As Long, ByVal wType As Integer, _
        ByVal wCategory As Integer, ByVal dwEventID As Long, _
        ByVal lpUserSid As Any, ByVal wNumStrings As Integer, _
        ByVal dwDataSize As Long, plpStrings As Long, _
        lpRawData As Any) As Boolean

    Declare Function GetLastError Lib "kernel32" () As Long

    Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        hpvDest As Any, hpvSource As Any, _
        ByVal cbCopy As Long)

    Declare Function GlobalAlloc Lib "kernel32" ( _
         ByVal wFlags As Long, _
         ByVal dwBytes As Long) As Long

    Declare Function GlobalFree Lib "kernel32" ( _
         ByVal hMem As Long) As Long

    '-- Public Constants
    Public Const EVENTLOG_SUCCESS = 0
    Public Const EVENTLOG_ERROR_TYPE = 1
    Public Const EVENTLOG_WARNING_TYPE = 2
    Public Const EVENTLOG_INFORMATION_TYPE = 4
    Public Const EVENTLOG_AUDIT_SUCCESS = 8
    Public Const EVENTLOG_AUDIT_FAILURE = 10

Public Function WriteToEventLog(sMessage As String, _
                           sSource As String, _
                           iLogType As Integer, _
                           vEventID As Integer) As Boolean

    Dim bRC              As Boolean
    Dim iNumStrings      As Integer
    Dim hEventLog        As Long
    Dim hMsgs            As Long
    Dim cbStringSize     As Long
    Dim iEventID         As Integer

    hEventLog = RegisterEventSource("", sSource)
    cbStringSize = Len(sMessage) + 1
    hMsgs = GlobalAlloc(&H40, cbStringSize)
    CopyMemory ByVal hMsgs, ByVal sMessage, cbStringSize
    iNumStrings = 1

    '-- ReportEvent returns 0 if failed,
    '-- Any other number indicates success
    If ReportEvent(hEventLog, _
       iLogType, 0, _
       iEventID, 0&, _
       iNumStrings, cbStringSize, _
       hMsgs, hMsgs) = 0 Then
        '-- Failed
        WriteToEventLog = False
    Else
        '-- Sucessful
        WriteToEventLog = True
    End If

    Call GlobalFree(hMsgs)
    DeregisterEventSource (hEventLog)
End Function

An example of how to write to the NT EventLog:

Call WriteToEventLog("Warning, file exceeded recommended limit.", _
"Test App", _
EVENTLOG_WARNING_TYPE, 1003)

***************************************************************************

A function for numbers beginning with 0:

Here is a function that is very helpful when you need some number that 
begins with 0's and has to be a certain number of digits: 

Function PadToString(intValue, intDigits) 
PadToString = String(intDigits - Len(intValue), "0") & intValue 
End Function 

Usage: 
myNewStr = PadToString(702, 6) 
myNewStr would be "000702"

***************************************************************************

Launch your default web browser from a VB app:

Here is a nice function to launch the default web browser from a VB 
application. 

Add the following code to the general section of a module: 

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 As Long = 1 
Public Const SW_SHOWMAXIMIZED As Long = 3 
Public Const SW_SHOWDEFAULT As Long = 10 

Add the following code to a form, which spawns off the default browser: 

Public Sub RunBrowser(strURL As String, iWindowStyle As Integer) 
Dim lSuccess As Long 

'-- Shell to default browser 
lSuccess = ShellExecute(Me.hwnd, "Open", strURL, 0&, 0&, iWindowStyle) 
End Sub 

To launch the Yahoo.com web site, use the following function call: 
Call RunBrowser ("www.yahoo.com", SW_SHOWNORMAL) 

***************************************************************************

Convert NULL values to empty strings to avoid errors:

When retrieving NULL values from a recordset object, errors can occur. One
way to avoid this is to inspect the value of the field, and if it's NULL,
convert it to an empty string or zero. For example:

If isnull(rs("Field")) then tmp="" else tmp=rs("Field")
form.textfield=tmp

An even simpler way is to use the format function, which will convert a
NULL value to an empty string automatically, avoiding any error messages.
It will look like this:

form.textfield=format(rs("Field"))

***************************************************************************

Creating professional documents with code:

Have you ever wanted to create polished, professional documents, like 
those created in Microsoft Word, through the use of code? Follow these 
easy steps to make it happen: 

1.)  Add a reference to your project for "Microsoft Word 8.0 Object 
Library" (MSWORD8.OLB). 

2.)  Add the following code to create an instance of Word and add text to 
a new document: 

Dim objWord As New Word.Application 

'-- Show Microsoft Word 
objWord.Visible = True 

'-- Add new Document 
objWord.Documents.Add 

'-- Add text to Document 
objWord.Selection.TypeText "Visual Basic!" 

'-- Select all Text 
objWord.Selection.WholeStory 

'-- Change Font Size 
objWord.Selection.Font.Size = 50 

Set objWord = Nothing 

3.)  Be sure to check out the Object Browser for more properties and 
methods exposed by the Word Object.

***************************************************************************

Parsing using the SPLIT function:

Parsing functions are one of the most commonly over-written string
manipulation functions. VB6 has answered this problem by adding a
SPLIT function. The function is very easy to use and, with only 1
line of code, you can parse any string using a specific delimiter.
The code looks like this:

Dim strAnimals        As String
Dim iCounter          As Integer
Dim arrAnimals()      As String

strAnimals = "Cats,Dogs,Horses,Birds"

'-- Parse String
arrAnimals = Split(strAnimals, ",")

'-- Loop through array
For iCounter = LBound(arrAnimals) To UBound(arrAnimals)
MsgBox arrAnimals(iCounter)
Next

***************************************************************************

Set your leading zeros or round to a decimal point:

Here's a quick tip that allows you to set the number of leading
zeros, or to round to a certain decimal point. Let's say you want
to print '1.234000' in '001.234' format. To do this, use

format(1.234000,"000.######")

***************************************************************************

A new Format function:

VB 5 has the Format command that almost works the same as Print.
The difference is that Format shortens the output string length
if all the format characters are not used. To work around this I
wrote a Public Function called FormatNum.

Public Function FormatNum(MyNumber As Double, FormatStr As String)

'This Function returns number formated as a string with the desired
'minimum number of characters
'MyNumber - Use CDbl(MyNumber) in the function call to prevent type
'mismatch error.
FormatNum = Format(MyNumber, FormatStr)
If Len(FormatNum) < Len(FormatStr) Then FormatNum = Space(Len(FormatStr) - Len(FormatNum)) & FormatNum
End Function

Use this function like this:

Print #FileNumber, FormatNum(CDbl(MyVariable), " #### ")

***************************************************************************

Performing the Windows shutdown operation:

Ever wondered how programs that you install automatically perform
the Windows shutdown operation? Well, it's actually a simple API
call to do this. Add the following API Declares and Constants to
a BAS Module:

Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags&, ByVal
wReserved&)

Global Const EWX_FORCE = 4 'constants needed for exiting Windows
Global Const EWX_LOGOFF = 0
Global Const EWX_REBOOT = 2
Global Const EWX_SHUTDOWN = 1

Then you Shutdown/reboot/logoff windows with the following call:

lresult = ExitWindowsEx(EWX_SHUTDOWN, 0&) 'shut down the computer

Note: Replace the first parameter of the ExitWindowsEx function call
with the appropriate CONSTANT.

***************************************************************************
    	               End of Help 8 of how many I do!!