Visual Basic Tips #40


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

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

Proudly presents:
Visual Basic

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


*1. CHECKING TEXT BOXES FOR CHANGES                    
           
If you've ever needed to do validation on a text box, you've 
probably run into the fact that any change, no matter how 
small, causes a Change event to be triggered on a TextBox 
control. A better way to check the value when the user is done 
is to use the LostFocus event. This event will be triggered 
only when the user leaves the field. However, don't make the 
mistake of putting the cursor back into the same box using the 
SetFocus event. Doing so will create an infinite loop, out of 
which the user can't escape.


*2. USING THE VALIDATE EVENT                    
           
In our previous tip, I described the difference between the 
Change and LostFocus events and when to use the latter. Visual 
Basic 6.0 introduced an additional event to confuse the issue: 
the Validate event. This event works in conjunction with the 
CausesValidation property, also added in VB 6.0. Here's how 
it works:

- Each control that needs to be validated has its 
  CausesValidation property turned to True. This event, like 
  QueryUnload, has a parameter called Cancel that can cancel
  whatever action caused the validation to start.

- In a command button, you can specify that validation is not 
  to be done, such as for a Cancel button.

- When you click a button where CausesValidation is marked True,
  all the validation routines in the form are triggered. If none
  of them mark their Cancel flag, the action goes through. 

To try it out, draw a text box on a form and add two command 
buttons. Mark the second command button's CausesValidation 
property to False, and then add this code: 

Option Explicit 

Private Sub Command1_Click() 
   Unload Me 
End Sub 

Private Sub Text1_Validate(Cancel As Boolean) 
   If Text1.Text = "" Then 
      MsgBox "You must enter data." 
      Cancel = True 
   End If 
  
End Sub 

If you don't type data into the box before clicking the first 
button, the form won't unload and you'll get a message. If data 
is present, the form will unload.


*3. ESTABLISHING A CONNECTION TO AN ISP

The Visual Basic Internet Programming site recently featured an 
article that shows how to use RAS to connect to an ISP. The API 
is fairly simple to use, and the article includes a sample 
download file: 

WinInet API: Establishing Connection to ISP 
http://www.vbip.com/wininet/wininet_dialup.asp 


*4. COPYING PROJECT REFERENCES

A reader recently wrote to me with problems in the libraries 
that his project was referencing. He couldn't find the library
that another developer was using and was wondering if there was 
a way to copy the project references from one project to another.

The easy answer to this is to share the project file, but if 
that isn't an option, open the project (VBP) file in Notepad 
and copy the lines at the top that begin with the word 
Reference. The lines will look something like this: 

Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:
\WINNT\System32\stdole2.tlb#OLE Automation

Reference=*\G{00000201-0000-0010-8000-00AA006D2EA4}#2.1#0#C:
\Program Files\Common Files\system\ado\msado21.tlb#Microsoft 
ActiveX Data Objects 2.1 Library

Reference=*\G{74C08640-CEDB-11CF-8B49-00AA00B8A790}#1.0#0#C:
\WINNT\System32\COMSVCS.DLL#Microsoft Transaction Server 
Type Library 

Drop these into the other project file, and assuming the 
libraries are installed on the other machine, things will work 
fine. Again, however, the best approach is to copy the project 
file itself, even if you have to remove all the modules from it.


*5. REMEMBER TO CALL THE UPDATE METHOD                    
           
A user wrote me asking about some ADO code that she had in her 
application. She called the AddNew method, added all the data, 
and then closed the recordset. When she then opened another 
recordset to find the record, it wasn't there.

The problem was that while she had a dynamic recordset with an 
appropriate lock on it, she forgot to call the Update method to 
save the new record. The same goes for making changes to a 
record, but in that case, the AddNew method is not necessary.


*6. NESTING FORMS

If you want to build an application that has several panes (as 
in Microsoft Outlook), you can use a component just released 
from Data Dynamics called Active Sizer. This product has a 
subform control that allows you to nest one form within another. 
Without a control like this, you can't easily nest one form 
inside another. Typically, the alternative is to use a Splitter 
or another similar control, available from several different 
vendors. However, those controls still use just a single form 
with one or more Splitters on it. 

Data Dynamics Active Sizer 
http://www.datadynamics.com/products/ 


*7. DAT FILES, REVISITED 
 
Previously, we've discussed DAT files and how they're often 
used to hide the source of the application that created them. 
Since this concept was misunderstood by several people, I 
figured I'd better clarify: 

The idea here is that if you're storing data files locally, you
don't necessarily want people opening them outside your 
application. For instance, if you're storing data in an Access 
database from your application, saving the data file as an MDB 
file gives this away. However, if you rename the file DAT or 
something else, you disguise the format of the data, making it 
tougher to figure out which application to use. Of course, you 
can always password-protect your Access databases and hard-code 
the password within your application, but that's a tip for 
another day.


*8. MULTIPLE KEYWORD SEARCHES

A reader asked how he could search a database using two LIKE 
clauses to allow for multiple word searches at the same time. 
This is easy to do using SQL. Here's an example: 

Dim strWord1 As String 
Dim strWord2 As String 
Dim strSQL As String 

strSQL = "SELECT * FROM tblCustomers " _ 
   & "WHERE LastName LIKE '%" & strWord1 _ 
   & "%' OR LastName LIKE '%" & strWord2 & "%'" 

As you can see, you simply OR the conditions together, which 
will give you the union of the results from both parts of the 
query. Be sure to get all the single and double quotes in the 
right place, or you'll have a SQL statement that isn't correct.


*9. ORACLE AND MTS 
 
If you're doing development using Oracle, OLE DB, and MTS, 
you'll be interested in the "Oracle OLE DB and MTS" article at 
the VB2TheMax.com site. Written by Bruce Sanderson, the article 
shows how to integrate ADO, OLE DB, Oracle, and VB components 
into a middleware solution. 

VB2TheMax: Oracle OLE DB and MTS 
http://www.vb2themax.com/HtmlDoc.asp?Table=Articles&ID=220


*10. PRINT GENERATED SQL

When I'm dynamically building a long SQL statement, I often find
it handy to use Debug.Print to print the SQL to the Immediate 
window. This makes it easier to find errors as things are 
running. At runtime, however, it's often helpful to write the 
text to a text file for logging and debugging later.
