Visual Basic Tips #36


----------------------------------------------

TipWorld - http://www.tipworld.com
The Internet's #1 Source for Computer Tips, News, and Gossip

Proudly presents:
Visual Basic

----------------------------------------------


*1. READING A FILE                   
          
Previous tips have mentioned the use of the FileSystemObject 
component. However, we haven't discussed a simple version of opening a 
file and putting it into a control. Here's how you do it: 

   Dim objFSO As New Scripting.FileSystemObject 
   Dim objStream As Scripting.TextStream 
   Set objStream = objFSO.OpenTextFile("C:\TextFile.txt", _ 
      ForReading, False) 
   txtData.Text = objStream.ReadAll 
   objStream.Close 

This opens the text file named C:\TextFile.txt and puts it into a box 
called txtData. It's that simple... 


*2. SAVING A FILE                   
          
The reverse of our previous tip (reading a file) is to write content 
to a different file. Here's how you do it: 

   Dim objFSO As New Scripting.FileSystemObject 
   Dim objStream As Scripting.TextStream 
   Set objStream = objFSO.OpenTextFile("C:\TextFile.txt", _ 
      ForWriting, True) 
   objStream.Write txtData.Text 
   objStream.Close 

This writes all the contents of the text box into the file opened 
earlier in the code. Again, it's that simple...


*3. LEARNING EVENT HANDLING                   
          
I get questions every so often about how to learn when various events 
are triggered during the run of a Visual Basic program. One of the 
easiest ways to find out when events occur is to add Debug.Print 
statements to your event handlers. These messages will be printed to 
the Immediate window as the events are triggered. Don't make the 
mistake of using the MsgBox function. The MsgBox function will 
interrupt the normal event flow and give you inaccurate results 
regarding when events happen.


*4. SEARCHING FOR TEXT IN FILES                   
          
A user asked me how to search the files in a directory for a piece of 
text. The most basic way to do this manually is to loop through the 
files in a directory, open each one, and search the text using a 
function like InStr. Here's a quick example: 

Dim strTemp As String 
Dim objStream As Scripting.TextStream 
Dim objFSO As New Scripting.FileSystemObject 

Set objFolder = objFSO.GetFolder("C:\") 
For Each objFile in objFolder.Files 
   Set objStream = objFile.OpenAsTextStream(ForReading) 
   strTemp = objStream.ReadAll 
   If InStr(strTemp, "texttofind") > 0 Then 
      Debug.Print "Found text in " & objFile.Path 
   strTemp = objFile 
Next  objFile


*5. USING THE ADO RECORDSET FIND METHOD                   
          
Once you open a recordset (excluding a ForwardOnly recordset), you can 
use the Find method to search for records within that recordset. The 
Find method uses the same syntax as a SQL WHERE clause. If it finds a 
record matching your criteria, the record pointer will be left on the 
matching record. If the Find method doesn't match any records in the 
recordset, the recordset's EOF property will be True. This is a handy 
way to be able to manipulate more than one recordset at a time without 
having to requery the database each time. Just move through the 
recordset, and you can do some of the work yourself. 

There are a number of additional options for use with the Find 
method--refer to MSDN for more information.


*6. WINDOWS 2000 SERVICE PACK 1 NOW AVAILABLE                   
          
It's not exactly Visual Basic, but if you're using any version of 
Windows 2000, the long-awaited service pack is now available. This 
service pack contains a whole bunch of fixes that are all documented 
on the Microsoft Service Pack Web site. The service pack is also 
available for download, or you can order it on CD. 

Windows 2000 Service Pack 1 
http://www.pcworld.com/r/pcw/1%2C2061%2Cpcw-vb8-3%2C00.html


*7. PARSING A FILE                   
          
A reader asked about easy ways to parse files that are delimited. In a 
delimited file, a particular character, such as a comma or tab, 
separates each field. If you're using Visual Basic 6.0, there's an 
easy way to do this in one step: Use the Split function. This function 
will break a line into an array of fields, based on the location of a 
particular character. Add this function to the FileSystemObject and 
you have an easy-to-use parser. Here's an example: 

File Contents: 

Eric#Smith#123 Main St#Anytown#VA#12345 
Erica#Smith#234 Main St#Anytown#VA#12345 
Erick#Smith#345 Main St#Anytown#VA#12345 

Each field is separated using a pound sign. This could just as easily 
be a tab character. The character really doesn't matter. Here's the 
code that will read the file line by line and break each line into a 
number of fields: 

Sub Main() 
   Dim objFSO As New Scripting.FileSystemObject 
   Dim objStream As Scripting.TextStream 
   Dim strLine As String 
   Dim a_strFields() As String 
 
   Set objStream = objFSO.OpenTextFile("C:\MyFile.txt", ForReading) 
   Do Until objStream.AtEndOfStream 
      strLine = objStream.ReadLine 
      a_strFields = Split(strLine, "#") 
   Loop 
   objStream.Close 
End Sub 

Note that the Split function is called right after populating the 
a_strFields array. The Lbound and Ubound functions can tell you how 
many fields are in the array.


*8. CHANGING FORM TITLE BAR COLOR                   
          
A user asked if there was a way to programmatically change the color 
of a form's title bar. The short answer to this is no. The color of 
the title bar is controlled by the user in Control Panel, which means 
you don't have access to it within Visual Basic. Several books are 
available that can teach you how to manipulate Control Panel, but this 
is not a simple task. Dan Appleman's VB API book has some of this 
information, as do several books from Wrox Press that cover the 
Windows API.  

Dan Appleman's Visual Basic Programmer's Guide to the Win32 API 
http://www.amazon.com/exec/obidos/ASIN/0672315904/tipworld


*9. USING COM OBJECTS FROM VB                   
          
I often receive questions about using COM objects or components within 
Visual Basic. Any object you use within Visual Basic (leaving out DCOM 
and COM+ for now) is a COM object. COM is simply a specification for 
how objects talk to one another within the Windows environment. For 
instance, if you create a FileSystemObject, that is a COM object.  

In more general terms, you have to first reference the object library 
in the References dialog box under the Project menu. For the 
FileSystemObject, for instance, you need the Microsoft Scripting 
Runtime library. The Object Browser (located on the View menu) will 
show you all the parts of that library. You can then declare instances 
of those objects as shown in the documentation provided with VB.  

In short, don't let the buzzwords trip you up. COM objects are really 
easy to use, and believe it or not, you're already using them!


*10. CHANGING LISTBOX STYLES                   
          
The Visual Basic 6.0 ListBox control has a new property: Style. This 
property allows you to create a ListBox that uses check boxes for each 
item. A user recently asked how to change the Style property at 
runtime. The answer is that you can't do it. The documentation for the 
Style property indicates that this property is read-only at runtime, 
which means you have to stick with one style or the other while your 
program is running.
