
		       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!
****************************************************************************

Display a directory browser:

Here's some easy to use code for displaying a directory browsing
window. This allows a user to select a directory.

Private Type BrowseInfo
  hWndOwner      As Long
  pIDLRoot       As Long
  pszDisplayName As Long
  lpszTitle      As Long
  ulFlags        As Long
  lpfnCallback   As Long
  lParam         As Long
  iImage         As Long
End Type

'Browsing for directory.
Private Const BIF_RETURNONLYFSDIRS = &H1      'For finding a folder to
start document searching
Private Const BIF_DONTGOBELOWDOMAIN = &H2     'For starting the Find
Computer
Private Const BIF_STATUSTEXT = &H4
Private Const BIF_RETURNFSANCESTORS = &H8

Private Const BIF_BROWSEFORCOMPUTER = &H1000  'Browsing for Computers.
Private Const BIF_BROWSEFORPRINTER = &H2000   'Browsing for Printers
Private Const BIF_BROWSEINCLUDEFILES = &H4000 'Browsing for Everything

Private Const MAX_PATH = 260

Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal _
lpString1 As String, ByVal lpString2 As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As _
BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList
As _
Long, ByVal lpBuffer As String) As Long

Public Function BrowseForFolder(hWndOwner As Long, sPrompt As String)
As String

   '=================================================
   'Opens the system dialog for browsing for a folder
   '=================================================
   Dim iNull As Integer
   Dim lpIDList As Long
   Dim lResult As Long
   Dim sPath As String
   Dim udtBI As BrowseInfo

  With udtBI
     .hWndOwner = hWndOwner
     .lpszTitle = lstrcat(sPrompt, "")

     .ulFlags = BIF_RETURNONLYFSDIRS
  End With

  lpIDList = SHBrowseForFolder(udtBI)
  If lpIDList Then
     sPath = String$(MAX_PATH, 0)
     lResult = SHGetPathFromIDList(lpIDList, sPath)
     Call CoTaskMemFree(lpIDList)
     iNull = InStr(sPath, vbNullChar)
     If iNull Then
        sPath = Left$(sPath, iNull - 1)
     End If
  End If

  BrowseForFolder = sPath

End Function

Private Sub Form_Click()
    Dim MyStr As String
    MyStr = BrowseForFolder(hWnd, "Hello")
    MsgBox MyStr
End Sub

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

Create an Excel object in VB

A few weeks ago, a tip was sent out discussing how to create a
Word object in Visual Basic. Many questions were sent to me asking
how to do this same procedure with Excel.

First, assign a new reference to your VB app, "Microsoft Excel 8.0
Object Library" - Excel8.olb


    Dim objExcel As New Excel.Application

    '-- Display Excel Application
    objExcel.Visible = True

    '-- Add new Workbook
    objExcel.Workbooks.Add

    '-- Set Text
    objExcel.ActiveCell(1, 1) = "Row 1 Col 1"
    objExcel.ActiveCell(1, 3) = "Row 1 Col 3"

    '-- Release the object variable
    Set objExcel = Nothing

Be sure to check out the Object Browser in VB for more properties/
methods of the Excel object.

****************************************************************************
Use the Timer control for longer than 1 minute:

As you know, the Timer control provides a great way to schedule
events in a VB project. When you enable the control, it fires off
its Timer event every n milliseconds, as determined by the
TimeInterval property. However, the TimeInterval property only
accepts numbers up to 65,535, or just over one minute. As a result,
you may have wondered how to use this control for periods longer
than that.

To do so, use a form, or project level, variable to keep track of
how many times the Timer event fires. Then, in the Timer event,
re-enable the control if enough time hasn't passed. For example,
consider the code below that we attached to a standard form.

Option Explicit
Dim iElapsedMin As Integer
Const cMax_Min As Integer = 2

Private Sub Form_Load()
Timer1.Enabled = True
iElapsedMin = 1
End Sub

Private Sub Timer1_Timer()
lblText.Visible = (iElapsedMin = cMax_Min)
Timer1.Enabled = (iElapsedMin < cMax_Min)
iElapsedMin = iElapsedMin + 1
End Sub

Here, the iElapsedMin variable maintains the elapsed minutes. We
also created a constant to hold the maximum time we want to wait
before turning the lblText control visible, (in this case 2
minutes). After one minute, the Timer event fires and disables the
Timer control. However, if the elapsed time is less than the maximum
time, then the procedure enables the control once more, starting
the process all over again.

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

Delete all selected items in a multiselect listbox:

To delete selected items from a multiselect listbox, loop backwards
through the items, then remove those where the Selected property
tests True. For example, suppose you have a list box with item1,
item2, item3, item4, item5. You've set the control's MultiSelect
property to Extended or Simple and have selected item1, item3, and
item5. To remove them from the listbox use code similar to the
following:

Private Sub cmdDeleteListItems_Click()
Dim i As Integer

For i = List1.ListCount - 1 To 0 Step -1
    If List1.Selected(i) Then List1.RemoveItem i
Next i

End Sub

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

Enabling the horizontal scrollbar in a RichTextbox control:

By default, when you add a RichTextbox control to a form, VB sets
the RightMargin property to 0. This means that the text you enter
wraps in the control. To display the horizontal scroll bar, you
must set the RightMargin property to a value greater than the
control's width. Otherwise, even if you set the ScrollBars property
to 1-rtfHorizontal, the RTB won't display the scrollbar.

As an example, add a 3200 wide RTB to a form, then set the
RightMargin to 3300 and the ScrollBars to 1-rtfHorizontal. Run the
project and type text into the control until it extends beyond the
RTB's boundaries. When you do, VB displays the horizontal scroll bar.

****************************************************************************
Force VB 6.0 to open maximized code windows:

While VB 5.0 was very good at remembering how you preferred the IDE
windows-maximized or normal, VB 6.0 isn't. It always opens the Code
and Object windows in normal view. Fortunately, you can modify this
behavior with a minor tweak to the Windows Registry so that the IDE
always opens these two windows maximized. Unfortunately, when you
make these changes, VB 6.0 will ALWAYS open them maximized-it still
won't remember your preferences between sessions.

Before we begin, take note that altering the Registry is risky
business and you should always make a back-up copy of the settings so
that you can restore them if something untoward happens. That said, to
force VB 6.0 to open a maximized Code or Object window, you add a new
value called MDIMaximized to the following Registry key:

HKEY_CURRENT_USER/Software/Microsoft/Visual Basic/6.0/MDIMaximized = "1"

To do so, in Windows click the Start button and select Run. Enter
RegEdit in the Run dialog box, then click OK. When Windows displays
the system registry, navigate through the keys until you've found the
VB 6.0 folder. Next, right-click anywhere in the right-hand pane and
select New/String Value from the shortcut menu. Enter MDIMaximized as
the name and press [Enter]. Now, right-click on MDIMaximized and
select Modify from the shortcut menu. Finally, in the Edit String dialog
box, enter 1 for the value and click OK. When you do, Windows assigns
the value to MDIMaximized. That's all there is to it! Close the
registry and open a Code or Object window in a VB 6.0 project. The IDE
displays them maximized.

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

Type-Declaration characters:

In addition to declaring a variable as its explicit data type,
VB also lets you declare certain data types using a single type-
declaration character. For example, instead of using

Dim MyString As String

you can use

Dim MyString$

Here's a complete list of the data-types and their corresponding characters:

String ($), Integer (%), Long (&), Single (!), Double (#), and Currency (@)

Of course, you should use these characters with caution, as they do
reduce your code's readability.

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

Add a new line to existing textbox text:

Often, you may want to append additional information to existing
text within a multiline textbox. For instance, suppose you want to
add the string "Updated: " followed by the current date. To do so,
you can take advantage of the SelStart and SelText properties.
As you probably know, the SelStart property returns or sets the
beginning of a selection. The SelText returns or sets the actual
selected text. If there isn't a selection, then both properties
return the insertion point. So, to insert a new line of text in a
multiline textbox, use code similar to:

Dim strNewText As String
With Text1
strNewText = "Updated: " & Date
.SelStart = Len(.Text)
.SelText = vbNewLine & strNewText
End With

This code snippet moves the insertion point to the end of any
existing text in Text1, then inserts a new line followed by the
additional information.

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

Let DateDiff() determine if two dates are in the same month:

To determine if two dates are in the same month, your first instinct
may be to simply use the Month() function on each date, then compare
the two resulting numbers.  However, under these circumstances, the
comparison would equate 1/1/2000 with 1/1/1999. Instead, the
DateDiff() function provides one quick way to make this
determination, like so:

DateDiff("m", Date1, Date2)

In this expression, the DateDiff() function finds the difference in
calendar months between the two dates. If the expression returns a
zero, then the two dates are in the same calendar month. To include
this feature in an conditional expression, you could use the
following in a query:

If DateDiff("m", Date1, Date2) Then
	'Month's are different
Else
	'Month's are same
End If

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

Use the VB6 Split function to count substrings:

As we mentioned in a previous tip, VB6 introduced the Split
function, which provides a new way to parse strings. With it,
you indicate a delimiter within a string, and VB fills a one-
dimensional array with the substrings.

However, in addition to the functionality described above, you can
also use the Split function as a quick way to determine the number
of substrings within a larger string. For example, suppose you want
to determine the number of times 'sea' appears in the string
"She sells seashells by the seashore."  To do so, simply perform
the split, and use the UBound function to count the number of
elements in the resultant array, as in

strTungTied = "She sells seashells by the seashore."
arySea = Split(strTungTied, "sea")
MsgBox "'Sea' appears in '" & strTungTied _
& "' " & UBound(arySea) & " times."

When you run this code snippet, Visual Basic informs you that 'sea'
appears in the phrase twice.

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

Add controls to a VB control array at run-time:

As you probably know, a control array lets you create controls that
share the same name and events. They also use fewer resources than
do the same number of controls not part of a control array. Often,
you may want to add a control, such as a button, to a control array
at runtime. To do so, you use the Load statement, which takes the
following syntax

Load object(index)

where object is the name of the control array, and index is the
index number of the new control you want to add. In order to add
controls to a control array at runtime, however, you must have at
least one control already in the array, (with it's index property
set-most likely to 0). VB only allows 32,767 controls in an array.

For example, suppose you have a form with a button control array
named cmdBtn. On the button's Click event, you want to add another
button to the form. To illustrate, open a new project and add a
command button to the default form. In the Properties Window,
enter 0 for the control's Index. When you do, VB transforms the
button into a control array. Now, add the following code to the form:

Private Sub cmdBtn_Click(Index As Integer)
Dim btn As CommandButton
Dim iIndex As Integer
iIndex = cmdBtn.Count
If iIndex <= 32767 Then
    Load cmdBtn(iIndex)
    Set btn = cmdBtn(iIndex)
    With btn
        .Top = cmdBtn(iIndex - 1).Top + 620
        .Caption = "Command" & iIndex + 1
        .Visible = True
    End With
    Set btn = Nothing
End If
End Sub

When you run the form, and click the button, the procedure adds a
new button to the form.

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

Retrieve subitem values from a VB ListView control:

When you use the ListView control in Report view, the control
shows a table-like list. It's the same view that appears in the
file dialog boxes (Open, Save, etc.) when you click the Details
button. Each row displays additional subitem information about
the main item. For example, in the file dialog boxes, the main
item is the file itself, while the Size and Type columns are the
subitems. When you use the ListView control in your own
applications, VB makes it easy to determine which item you've
selected. When you select an item in Report view, VB triggers the
ItemClick event, which passes a ListItem object (the item you just
clicked). From this object, you can determine it's value. So, for
example, the entire event procedure might look like this

Private Sub lstvwFiles_ItemClick _
  ByVal Item As MSComctlLib.ListItem)

strFileSelected = Item

End Sub

This statement would pass the selected item's value (Value being
the object's default property) into the strFileSelected variable.
However, you may have wondered how to retrieve the Item's associated
subitem values. Fortunately, VB provides the ListSubItems collection,
which contains all the subitems of an individual item. So, continuing
with the example above, the Size and Type items are both subitems of
the main File item. To ascertain the values of these subitems, you
simply loop through the collection, like so

Private Sub lstvwFiles_ItemClick _
  (ByVal Item As MSComctlLib.ListItem)
Dim itm As ListSubItem
Dim strSubs As String

MsgBox "ItemClicked: " & Item
For Each itm In Item.ListSubItems
  strSubs = strSubs & itm & ": "
Next itm
MsgBox "SubItems: " & strSubs

End Sub

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