Visual Basic Tips #38


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

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

Proudly presents:
Visual Basic

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


*1. USING A VARIABLE WITH THE RECORDSET.SORT PROPERTY

In a previous tip, you learned about the recordset's sort 
property and how it can be used to automatically sort the data 
in the recordset. A user asked if it was possible to use a 
variable to specify the field name, as shown here: 

   Dim rsData As ADODB.Recordset 
   Dim strField As String 
   strField = "CompanyName" 
   rsData.Sort = strField 

As with anything else Visual Basic, you can use a variable 
wherever you might use a constant or literal piece of text.


*2. MCSD RECERTIFICATION REMINDER

If you are an MCSD who certified under the old track, which 
included two Windows Architecture tests, you need to get 
recertified using the new track. The new track requires two 
developer tools, a test (70-100) about creating solution 
architectures, and then an elective. As of this writing, the 
deadline for recertification was fast approaching... but it had 
already been extended once, so maybe it will be extended again.


*3. CRYPTOGRAPHY IN VISUAL BASIC

One of the more important topics in application security is the 
handling of encryption. There are a group of API calls known as
the Cryptography API built into the Windows operating system. 
Along with a new book by Richard Bondi, there is an open source 
set of COM wrappers for these API calls. These objects are much 
easier to use than the actual API sets. 
To download this code, go to 

http://www.geocities.com/richardbondi/wcco1.0.zip 

For the documentation, visit 

http://www.geocities.com/richardbondi/wcco_manual.zip


*4. MULTIPLE CLASSES PER DLL

A user recently asked about combining classes into a DLL. He 
had previously set up separate projects for each class file, 
which really wasn't necessary. As long as the amount of code 
is manageable, a single DLL would be more efficient and easier 
to manage. I try to break up my DLLs by function or application 
group, but you can divide them as you see fit. As usual, the 
fewer libraries and executables, the better. It's easier to 
update a single file than it is to change multiple files in 
multiple locations. However, this topic wades into personal 
opinion pretty quickly, so use your best judgment.


*5. NO RECORDCOUNT IN RECORDSET

A user sent me a snippet of code showing that the RecordCount 
property for his ADO recordset was coming back as -1. The 
solution to this was to use a type of recordset different from 
the ForwardOnly recordset he had used. The forward-only 
recordset doesn't include the navigation and positioning 
information that would provide a correct RecordCount value.


*6. COMBINING TEXT FILES

A user asked if it was possible to combine multiple text files 
into a single file using Visual Basic. The short answer is yes. 
There are several ways to do this. My favorite involves the use 
of FileSystemObject. For instance, let's say that you wanted to 
merge all the files with the extension .log into a new file 
named master.txt. 


Sub Main() 
   Dim objFSO As New Scripting.FileSystemObject 
   Dim strText As String 
   Dim objSource As Scripting.TextStream 
   Dim objDest As Scripting.TextStream 
   Dim objFile As Scripting.File 
   Dim objFolder As Scripting.Folder 
  
   Set objFolder = objFSO.GetFolder("D:\LogFiles") 
   Set objDest = objFSO.OpenTextFile("D:\master.txt", _
         ForAppending, True) 
  
   For Each objFile In objFolder.Files 
      If Right(objFile.Path, 4) = ".log" Then 
         Set objSource = objFile.OpenAsTextStream(ForReading) 
         objDest.Write objSource.ReadAll 
         objSource.Close 
      End If 
   Next objFile 
   objDest.Close 
  
End Sub 


This code will loop through all the files in the specified 
directory looking for files with the .log extension. It reads 
them and then writes them to the file specified by the 
objDest object.


*7. COMPARING STRINGS

I'm commonly asked questions dealing with checking passwords 
against each other (in a confirmation mode, for instance) or 
against a stored password. One key thing you have to decide 
when doing this is whether case is important. Some systems I've 
used are case sensitive; others aren't. If you have decided to 
not check case when checking a password, be sure to indicate 
either lowercase values (LCase) or uppercase (UCase). This way, 
you're comparing "apples to apples," so to speak. Otherwise, a 
mix of capital letters will cause the values to never 
compare properly.

Keep in mind, too, that you should let users know whether case 
is factored as they enter their password.


*8. REMOVING COMPONENTS

If you need to remove a DLL from your computer, there are two 
steps to the process. First, you have to unregister the 
component. This is done using the regsvr32.exe utility, via the 
/u switch. To unregister foo.dll, for example, you might type 
this line:

regsvr32.exe /u C:\WinNT\System32\foo.dll

The second step would be to remove the actual file. I'd also 
suggest removing all copies of the DLL so that users aren't 
confused as to whether the component is available. (This will 
also stop users from trying to re-register an older copy of 
the library.)


*9. CONCATENATING TEXT

When concatenating strings, be sure to use the ampersand instead 
of the plus sign. While the plus sign works, it has some nasty 
side effects when dealing with strings that contain numeric 
data. If you need to do math on your strings, use one of the 
various numeric conversion functions--such as CDbl, CLng, or 
CInt, among others--first.


*10. MULTIDIMENSIONAL CONTROL ARRAY                    
           
A user creating a game program asked about creating a 
multidimensional control array for his input controls 
(CommandButtons, I believe). The short answer is that control 
arrays in Visual Basic currently support only a single 
dimension. For his game, he needed a 10x10 grid. Using a simple 
coordinate system of (row, column), it's easy to map to a 
one-dimensional array of controls. For instance, row 2, column 6 
could calculate to the second row of controls, which would be 
controls 11-20, if you're using one as the first control index. 
If you're using zero, the controls would be 10-19. The 
calculation would be ((row - 1) - 10) + column. If you start at 
zero, you would need to subtract one from the column value. Once 
you have the control index number, you could store your data in 
the control's Tag property or separately in another array.
