Visual Basic Tips #26


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

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

Proudly presents:
Visual Basic

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


*1. IMPROVING TREEVIEW PERFORMANCE                   
          
If you're loading a TreeView control with a large amount of data, the 
default behavior of the TreeView control is to refresh frequently, 
which slows down the data-loading process. Here's a tip provided by 
Rick Fleming as to how to make things go faster by disabling the 
refresh process: 

"I recently ran into a similar situation, with TreeViews, ListViews, 
and Listboxes. The reason it is so slow is a WM_PAINT message gets 
thrown every time you add an item. You can speed this up 
significantly, and do something else cool, and still load everything 
at once. The trick is to do something like the following: 

TreeView1.Nodes.Clear ' I am doing this from memory, can't remember if 
this exists or not. 
TreeView1.Nodes.Add , , "TempRoot", "Loading Data..." 

SendMessage TreeView1.hWnd, WM_SETREDRAW, False, 0&  ' This forces the 
control NOT to proceed on WM_PAINT 
  
TreeView1.Nodes.Clear  ' The "Loading Data..." will still be visible 
  
{Load your tree} 
  
SendMessage TreeView1.hWnd, WM_SETREDRAW, True, 0& 
TreeView1.Refresh 
Form1.Refresh ' Sometimes you might have to do a InvalidateRect to 
force a redraw... 
  
Now your tree is loaded... one screen I did loaded ~12,000 records 
from an Access97 database in 20 seconds with WM_SETREDRAW on, and .12 
seconds (notice the decimal) with WM_SETREDRAW off." 

Thanks for the tip, Rick.  


*2. CREATING GAMES WITH VISUAL BASIC                   
          
While creating games in Visual Basic (and Windows) used to be a bit of 
a joke, DirectX has changed all that. In fact, the next version of VB 
is rumored to have DirectX support. For now, however, you can use the 
DirectX SDK by downloading it from the Microsoft DirectX Developer 
Center: 

http://msdn.microsoft.com/directx/


*3. ARRAYS START AT INDEX ZERO                   
          
In case you didn't know it, the default index for an array starts at 
zero. That is, if you create an array like 
  
Dim a_intValues(10) 

you actually have index values from zero to ten, inclusive. You can 
verify this by looking at the LBound and UBound functions, which 
retrieve the lower and upper bounds of the array.  

You can configure this using the Option Base statement, which lets you 
specify whether the first index should be zero or one. You can also 
provide your own indexes, such as this: 

Dim a_intValues(5 To 10) 

In this case, the indices will go from 5 to 10, inclusive.


*4. DECOMPILING VISUAL BASIC                   
          
A common question I get from readers is whether there is a decompiler 
for Visual Basic--that is, a program that can look at an EXE file and 
extract the source code from it. After doing a bit of research, it 
appears that there no longer is a VB decompiler available for recent 
versions of VB. At one point, a company called AshSoft had one; 
however, the company doesn't seem to exist any longer.


*5. VISIT VBWIRE.COM                    
          
One of the more useful sites that I visit periodically is VBWire.com. 
This site consolidates all the press releases and announcements about 
Visual Basic and add-on products and components. If you're trying to 
keep updated on what is going on in this multi-million dollar 
industry, you can sign up for its newsletter, as well. In addition, if 
you have products or resources for the VB community, you can announce 
them for free using this service. For example, I often use VBWire to 
announce new articles and features at my VBTechniques.com site. 

VBWire
http://www.VBWire.com/


*6. VISUAL STUDIO SERVICE PACK 4 AVAILABLE                   
          
If you're using any part of Visual Studio, be aware that Service Pack 
4 is now available. You can see a list of fixes and features at 

Microsoft Visual Studio 
http://msdn.microsoft.com/vstudio/sp/vs6sp4/default.asp


*7. SINGLE QUOTES AND SQL                   
          
In previous tips, you've learned that single quotes can wreak havoc 
for your SQL statements. Changing the single quotes into two single 
quotes normally will take care of the problem. Another way to 
eliminate the problem is to use parameters with a Command object. If 
you're creating parameters for your stored procedure, there's no need 
to change the data before submitting it to the server.  


*8. GETTING DATA FROM TWO DATABASES                   
          
A user recently asked if it was possible to select data from two 
separate databases. The short answer is no. However, you can reference 
one database's table from another database. Once you've linked the 
tables, you can select from both of them. This may vary if you're 
using databases other than SQL Server or Access, but most databases 
operate in this way.


*9. USING THE RECORDSET SORT PROPERTY                   
          
Once you've created an ADO recordset, you have the option of 
re-sorting the data by any of the fields available to you in the 
recordset. For this to work, the CursorLocation property has to be set 
to adUseClient. This eliminates the ability to use this feature with 
ForwardOnly recordsets, as well.   

To sort a recordset by a field, just set the Sort property, as shown 
here: 

rsData.Sort = "CompanyName" 

The sorting happens immediately. If you want to sort in reverse 
(descending) order, you can add the keyword  

DESC  

following the field name, as shown here: 

rsData.Sort = "CompanyName DESC" 

You can also list multiple columns for sorting, like so: 

rsData.Sort = "CompanyName, ContactTitle DESC"


*10. USING CONTINUATION CHARACTERS                   
          
Even though you can write extremely long lines of code, they're easier 
to read when the lines are narrower than the width of your screen. To 
do this, you can use the continuation character, which is the 
underscore character (_). You can break any line into multiple lines 
by using continuation characters between keywords, as shown here: 

MsgBox "http://www." & "microsoft & ".com" 

This breaks into these lines, for example: 

MsgBox "http://www." _ 
   & "microsoft" _ 
   & ".com" 

The indentations on the second and third lines are not required, but 
they do make it easier to tell when you've separated lines like this. 
Also note that you can't use a continuation character in the middle of 
a string. You have to break the string into multiple, smaller strings, 
as we did in this example.
