
		       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!

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

Add and remove images in an imagelist assigned to a toolbar:

Keep the flexibility to add and remove images in a imagelist
that is assiged to a toolbar while a project is still in
design mode. Because you can't alter images in a imagelist
control while it's assigned to a toolbar, you can use this
alternative way to bypass this limitation.

1. Add bitmaps to the imagelist control and assign a unique
key to every added bitmap.

2. Add the buttons to the toolbar and assign the key from
the bitmap image to the button key. Each button with a
bitmap should have the same key as the image key in the
imagelist. Each button without a bitmap, i.e. tbrSeparator
or tbrPlaceholder, should have NO key.

3. In the form load event assign the imagelist to the toolbar:

Set ToolBar1.ImageList = ImageList1

4. Assign the bitmap images to the buttons on the toolbar:

Dim myButton as Variant

For Each myButton in ToolBar1.Buttons
If myButton.Key <> Empty Then
myButton.Image = myButton.Key
'if the key name is human readable
'use it for description and tooltiptext
myButton.Description = myButton.Key
myButton.ToolTipText = myButton.Key
Endif
Next

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

Using the DEFAULT property with multiple-line text boxes:

Don't use the DEFAULT property on a data entry form on which you have 
a multiple-line text box. If you do, the user will not be able to hit
enter to make "hard returns" in their text, because that will
automatically click the default button rather than moving to a new 
line in the text box. Instead, provide an access or "accelerator" key 
to the button. See the VB help on the Caption property for details on 
how to do this.

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

Using the Date type with an ADO source:

The first thought to handle a date in VB is to use a Date type variable. 
In fact, this will not work for Null dates coming from an ADO source. 
The reason is that the date internal type has a different behavior than 
the adDate ADO type.

To see the differences between Date and adDate take a look at the
following code (under VB6 with Microsoft ActiveX Data Objects 
Recordset 2.0 Library, but the same would happen with Microsoft 
ActiveX Data Objects 2.0 Library):

Private Sub Date_handling()
Dim lDate As Date
Dim lDateVar As Variant
Dim lRs As ADOR.Recordset

'Initialization:
Set lRs = New ADOR.Recordset
lRs.Fields.Append "MyDate", adDate, , adFldIsNullable
lRs.Open
lRs.AddNew
lRs!MyDate = Date
MsgBox lRs!MyDate

'Storing:
lDate = lRs!MyDate
lDateVar = lRs!MyDate

'Setting:
'lDate = Null 'This would not work
lDateVar = Null 'This will work

lRs!MyDate = lDateVar
'lRs!Update
lRs.Close
End Sub

A String is not more appropriate: a Null string does not represent a
Null date. Unfortunately, the default setting in VB6's DataEnvironment
puts a TextBox on the form when you drag and drop a date field.

To have a good internal representation of the date, you should use a
Variant.

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

Using WithEvents to add features to a textbox:

Have you ever felt that certain standard Windows controls were lacking
features?  Would you like to add features to a textbox which you wouldn't
have to code every time you created a new textbox?  Typically, you would
create a generic subroutine and call it from the event of every control.
With VB5 and higher, a command named "WihEvents" provides a simpler
solution.

Let's say you wanted a textbox that would only accept uppercase letters as
input. If lowercase letters were entered they would be automatically
converted to uppercase. Numbers and all symbols would be rejected. 
And whenever the mouse pointer passed over it, you wanted to show the 
mouse position in the textbox. Finally, let's say you didn't want
the fourth textbox to accept the letter 'Z'.

Create a standard EXE project, add four textbox controls onto the
default form, and add a class module. Enter the following code to Form1:

'General Declarations
Private clsTextBox1 As Class1
Private clsTextBox2 As Class1
Private clsTextBox3 As Class1
Private clsTextBox4 As Class1

Private Sub Form_Load()
  Set clsTextBox1 = New Class1
  Set clsTextBox1.TextBoxCtl = Text1
  Set clsTextBox2 = New Class1
  Set clsTextBox2.TextBoxCtl = Text2
  Set clsTextBox3 = New Class1
  Set clsTextBox3.TextBoxCtl = Text3
  Set clsTextBox4 = New Class1
  Set clsTextBox4.TextBoxCtl = Text4
End Sub

Private Sub Text4_KeyPress(KeyAscii As Integer)
   '-- This code executes before the class keypress
   If KeyAscii = Asc("a") Then Beep
End Sub


In Class1, enter the following code:

Private WithEvents txt As TextBox

Public Property Set TextBoxCtl(OutsideTextBox As TextBox)
  Set txt = OutsideTextBox
End Property

Private Sub txt_KeyPress(KeyAscii As Integer)
  '-- Convert to Uppercase
  KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
End Sub

Private Sub txt_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
  txt.ToolTipText = "X:" & X & " Y:" & Y
End Sub


To add new functionality to all four textboxes, simply enter code into the
class.  Remember, the keypress event for textbox4 executes first when a
key is pressed.  Then, the Classes keypress event fires next.

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

Getting the backslashes right when using App.Path:

In order to retrieve the current directory path for an application's
executeable, the App.Path property can be used.  Be aware, however, of a
major gotcha when doing that. If your application is executing in the root
directory, a backslash will be added to the end of the directory. If your
application is executing in a sub-directory, then the result will not have
a backslash on the end.  Using the following function will clear up this
problem:

Public Function AppPath(sFileName As String) As String
  If Right$(App.Path, 1) = "\" Then
    AppPath = App.Path & sFileName
  Else
    AppPath = App.Path & "\" & sFileName
  End If
End Function

For example, AppPath("test.txt") will append the filename correctly no
matter what directory the EXE resides in.

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

Creating a ScreenSaver:

If you've ever wanted to create your own ScreenSaver in Visual Basic,
you're in luck!  Start off by creating a new standard EXE project.  Add a
label to your form with text. In addition, add a Timer control with the
interval property set to 1000.  Then add the following code to your form:

Private Sub Form_Click()
  '-- End Screen Saver when form is clicked
  Unload Me
End Sub

Private Sub Form_Load()
  '-- Do not allow more than 1 instance of the Screen Saver
  If App.PrevInstance Then Unload Me
End Sub

Private Sub Timer1_Timer()
  '-- Make label blink once a second
  Label1.Visible = Not (Label1.Visible)
End Sub

Make sure your Form's Maximized property is set, and that it's Border
Style is set to None.  Most screen savers are full screen, without a title
bar.

In the File | Make EXE File dialog, under the Options button, type the
string SCRNSAVE: (all in upper case) at the beginning of the Application
Title textbox. (For example, we could call our application
SCRNSAVE:TestApp1.) For the application executable filename, specify that
the extension of the application be .SCR instead of .EXE.  (For example,
we could call our executable TestApp1.scr.)  Make sure you put the .SCR
file in your Windows Directory.

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

Create templates from existing forms:

Here's a way for you to create a template from an existing form so you can
use it to design future forms. Once you've created the 'perfect' form,
place it in the ..\Template\Forms\ subdirectory.  Now, the next time you
want to add a new form to your project, you'll see that the newly created
form is in the list of available form templates! Just select it and you're
on your way.  This tip also applies to modules, classes, etc.

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

Implementing a "wait" function in VB:

Here's a simple way of implementing an accurate "wait" function in VB.

1) Add a timer called timer1 to a form.  Set its Interval property to 0
and it's enabled property to FALSE.

2) Add two labels (label1 and label2) and a command button (command1) to
the form.

3) Add the following subroutine and Timer1 event code:


Public Sub Wait(seconds)

    '-- Turn timer on
    Timer1.Enabled = True

    '-- Set Timer Interval
    Me.Timer1.Interval = 1000 * seconds
    While Me.Timer1.Interval > 0
        DoEvents
    Wend

    '-- Turn timer off
    Timer1.Enabled = False
End Sub


Private Sub Timer1_Timer()
    Timer1.Interval = 0
End Sub


That's it! Use the Wait function anywhere a delay is required, for
example:

Private Sub Command1_Click()
    Label1.Caption = Now
    Wait (5)
    Label2.Caption = Now
End Sub

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

Keeping track of VB source code builds:

Keeping track of source code builds in VB can be easy, if you use the
Version Numbering feature provided in EXE creations.  Click on the options
button when creating an EXE file.  Then turn on the "Auto Increment"
checkbox.

Versioning supports a three-segment version number: Major, Minor and
Revision. The Auto Increment feature, if selected, will automatically
increase the Revision number by one each time you run the Make Project
command for this project.

Typically, build information will go on an About form.  Just add a label
called, "lblVersion" and add the following code to your form:

lblVersion.Caption = "Version: " & App.Major & "." & App.Minor & "."
& App.Revision

If my Major version number is 2, the Minor number is 1 and the Revision is
12, the label will display: "Version: 2.1.12"

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

Writing to the Windows NT event log:

Windows applications typically write to the NT event log to provide the
user with useful information.  In VB5/6, the App object now provides
methods to make writing to the event log in Windows NT a snap:

'-- Start Event Logging
Call App.StartLogging("", vbLogToNT)

'-- Log Events to NT
Call App.LogEvent("Info", vbLogEventTypeInformation)
Call App.LogEvent("Error", vbLogEventTypeError)
Call App.LogEvent("Warning", vbLogEventTypeWarning)

Be aware though, these functions will only work in the compiled EXE.  They
will be ignored in design mode. Check out the Microsoft knowledge base
article Q161306 for more information.

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