
			Nod Programing 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!

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

Beware of optional typed parameters:

Starting with Visual Basic 4.0, you could define optional parameters. There
was only one problem: They could be only of type Variant. With VB 5.0, you 
can define typed optional parameters. However, you must be careful when doing
so, because you can't check whether a typed optional parameter was received.
Consider this sample code:

Public Sub SubX(Optional b As Boolean)
    If IsMissing(b) Then
        MsgBox "b is missing"
    Else
        MsgBox "b is not missing"
    End If
End Sub
...
    'Call SubX with no parameters
    SubX

You'd expect to see a message box indicating that b is missing, but no box 
appears. The reason lies in the definition of IsMissing: "Returns a Boolean 
value indicating whether an optional Variant argument has been passed to a 
procedure." If you don't use a Variant argument, IsMissing won't provide the
expected value.

A typed optional parameter is never missing; it's always set to the default
value for each type (False for Boolean parameters, 0 for numbers and 
zero-length strings).

Another option is to add the default value in the declaration of the 
procedure, as follows:

Public Sub SubX(Optional i As Integer = 1)

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

AVOIDING THE [ENTER] BEEP:

When you're entering information into a text box and press [Enter], you'll 
hear a beep. You can easily avoid this behavior. To do so, place a text box
on your form (Text1). Enter the following code in the KeyPress event:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc(vbCr) Then
  KeyAscii = 0
End If
End Sub

When you run the form, pressing [Enter] will no longer produce a beep.

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

Prevent partially painted windows:

Sometimes when you display a form, only some of the controls appear. After a 
pause, the remaining controls appear. Such partial painting doesn't look 
professional. (Fortunately, this problem is much less apparent in VB 5.0 
because of dramatic improvements in screen painting.)

To avoid partially painted windows when showing a non-modal form, use the 
following code:

frmPerson.Show vbModeless
frmPerson.Refresh

The Refresh method will ensure that the form repainting is complete before 
executing any other code in the routine.

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

Centering a form:

To center a form on the screen in VB3 or VB4, you can write a CenterForm 
subroutine. Then, call CenterForm in the form's Load event. The code is as 
follows:

Public Sub CenterForm(frmTarget As Form)
     frmTarget.Move (Screen.Width - frmTarget.Width) / 2, _
          (Screen.Height - frmTarget.Height) / 2
End Sub

Private Sub Form_Load()
     CenterForm Me
End Sub

Editor's Note:
In VB5, you can center a form on the screen by setting the StartUpPosition 
property of the form to CenterScreen or CenterOwner.

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

Case-conversion on the fly:

If you want to convert text to uppercase as it's entered in a text box, just
create an Upper function and call it from the text box's keypress event, as 
shown here:

     Private Sub Text1_KeyPress(KeyAscii As Integer)
     	KeyAscii = Upper(KeyAscii)
     End Sub

     Function Upper(KeyAscii As Integer)
     	If KeyAscii > 96 And KeyAscii < 123 Then
		KeyAscii = KeyAscii - 32
	End If
     	Upper = KeyAscii
     End Function

This technique eliminates the need to "UCase" entered data. It also makes 
"hotseek" data searches much easier.

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

Trapping dropdown list errors:

In VB, the Text property of a Combo box whose Style property is set to 
'2 - Dropdown List' is read-only. This means that a statement like:

MyCombo.Text = "The Third Item"

will return an error if "The Third Item" is not part of the list. Wouldn't
it be nice if VB just set the Combo box's ListIndex property to -1 
(blanking it out) instead of bombing out? Well, here's some code that will
do just that:

Function SetComboText(MyCombo as ComboBox, MyItem as String) as Integer
  Dim I as Integer

  For I = 0 to MyCombo.ListCount - 1
    If MyCombo.List(I) = MyItem Then
      SetComboText = I
      Exit Function
    End If
  Next I

  ' If the program reaches this point, the string is not in the
  ' list.
  SetComboText = - 1
End Function

Use the function like this:

AnyCombo.ListIndex = SetComboText(AnyCombo, "Any String")

If "Any String" is in the list, then the combo box's ListIndex will be set 
to the correct index; if not, it will be blanked out. The great thing about 
this code is that if you want to do something else other than blanking out 
the combo box, all you have to do is replace the line:

SetComboText = - 1

with whatever you wish.

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

Speed up string buffers:

Sometimes you need to write a program that builds up a large amount of data 
in a string variable. You'd normally use a statement such as:

strBuffer = strBuffer & strNewData

during every loop. The problem with this approach is that the bigger your 
string buffer becomes, the slower your program runs.

A neat and very simple way around this problem is to use another buffer. 
Just fill the temporary buffer with data, and when it's big enough, append 
it to the main buffer. Then, clear the temporary buffer and continue. The 
code will look like this:

Public Sub NewBuildBuffer()
    Dim strBuffer As String, strTemp As String
    Dim l As Long, dStart As Date

    'Set start time
    dStart = Now

    'Build the buffer
    For l = 1 To 10000
        strTemp = strTemp & "New Line" & vbCrLf
        'Append to the main buffer every 100 times
        If l Mod 100 = 0 Then
            strBuffer = strBuffer & strTemp
            strTemp = ""
        End If
    Next
    'Append the last temp buffer
    strBuffer = strBuffer & strTemp

    'Report total time
    MsgBox "Seconds taken = " & DateDiff("s", dStart, Now)
End Sub

For programs that use very large string buffers, you'll see a huge 
improvement.

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

Preventing multiple instances of VB apps:

You can easily prevent users from running multiple instances of your 
programs by taking advantage of the PrevInstance property of the App object.

To do so, enter the following code in your application's opening form:

If App.PrevInstance Then
   MsgBox ("Cannot load program again."), vbExclamation, "The requested " _
     & "application is already open"
   Unload me
End If

This technique will also prevent multiple users from accessing single-user 
applications.

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

Retrieving the network logon name:

You can easily retrieve a user's network logon name by using the following 
API call:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _ 
              (ByVal lpBuffer As String, nSize As Long) As Long

To retrieve a "clean" version of the name, use this function:

Public Function NTDomainUserName() As String
Dim strBuffer As String * 255
Dim lngBufferLength As Long
Dim lngRet As Long
Dim strTemp As String

	lngBufferLength = 255
	lngRet = GetUserName(strBuffer, lngBufferLength)
	strTemp = UCase(Trim$(strBuffer))
	NTDomainUserName = Left$(strTemp, Len(strTemp) - 1)

End Function

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

Customizing a text box's pop-up menu:

In Windows 95, right-clicking any text box brings up a context menu with 
basic edit commands on it. If you want to change this menu, put the 
following code in the MouseDown event of the text box.

If Button = vbRightButton Then
	Text1.Enabled = False
	Text1.Enabled = True
	Text1.SetFocus
	PopUpMenu Menu1
End If

where Text1 is the text box and Menu1 is the pop-up menu.

Disabling and re-enabling the control causes Windows to lose the MouseDown 
message, SetFocus tidies things up a bit, and PopUpMenu shows the menu.

Left clicks will work as always, allowing the user to edit the text in the 
text box.

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

Selecting all text when a TextBox gets focus:

When you present the user with default text in a TextBox, you'll often want
to select that text when the TextBox gets focus. That way, the user can 
easily type over your default text.

The function below will do the trick. The first click on the TextBox will 
select all the text; the second click will place the cursor.

Public Sub TextSelected()
Dim i As Integer
Dim oMyTextBox As Object

Set oMyTextBox = Screen.ActiveControl
    If TypeName(oMyTextBox) = "TextBox" Then
        i = Len(oMyTextBox.Text)
        oMyTextBox.SelStart = 0
        oMyTextBox.SelLength = i
    End If
End Sub

Just add the function to your project and call it from the TextBox's 
GotFocus event.

Private Sub Text1_GotFocus()
    TextSelected
End Sub

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

Preventing Add-Ins from loading at launch:

When you launch Visual Basic 4 or 5, any active Add-Ins also launch. If 
there's an error in one of the Add-Ins, however, you could encounter a 
global protection fault.

To prevent this from happening, you can turn off Add-Ins before launching 
VB. To do so, launch Notepad or WordPad and open the file VBAddin.INI in 
your Windows directory. You'll see a series of entries like this:

AppWizard.Wizard=1

Just change the "1" to a "0" in each entry. Then save the file and launch 
VB. The program will launch without any Add-Ins.

Of course, to add and remove Add-Ins while you're in Visual Basic, just 
choose Add-In Manager from the Add-Ins menu.

			End of Help 1 of how many I do!!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               