Visual Basic Tips #19


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

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

Proudly presents:
Visual Basic

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

*1. USE OPTION EXPLICIT                 
        
This may be obvious to you old-timers, but one of the biggest newbie 
mistakes is to not declare variables. Visual Basic, by default, still 
allows you to simply use a variable without declaring it. A simple 
typo doesn't get flagged as an error; instead, it creates a new 
variable and becomes a bug that can be quite nasty to track down. If 
you choose Tools, Options, you can select the Require Variable 
Declaration option in the Options dialog box so that any new modules 
you add (forms, code modules, classes, etc.) automatically get Option 
Explicit added to them. Hopefully, the next version of VB will have 
this option turned on by default.


*2. CUSTOMIZE YOUR MENUS AND TOOLBARS                 
        
One of the most overlooked features of the Visual Basic environment is 
its ability to let you customize your toolbars and menus. When you 
right-click on the menu bar, you'll see a pop-up menu with the 
Customize choice. Once you select that option, you can drag commands 
from the dialog box to any of your menus or toolbars. Since some 
helpful commands (like Comment Block and Uncomment Block in the Edit 
group) aren't added to the VB menus by default, this is a handy way to 
put them there.


*3. 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 multimillion-dollar industry, 
you can sign up for their 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 use VBWire to announce new 
articles and features as I post them on my VB Techniques site. 

VBWire--Visual Basic News and Information 
http://www.vbwire.com


*4. SWITCHING FROM ACCESS TO SQL SERVER                 
        
Some of you may have on your Web servers Access databases that provide 
some sort of dynamic content or storage facilities for data. A reader 
asked me when you should convert to SQL Server. There are several 
reasons for converting to SQL Server. First, SQL Server can better 
handle large amounts of data. (Access databases tend to bog down when 
the files are getting over 50 to 100 MB, whereas SQL Server databases 
can easily handle that much data and far more.) Another reason to 
convert to SQL Server is performance. SQL Server operates in a 
different manner than Access and can handle requests much faster and 
more efficiently. SQL Server databases can also be backed up without 
having to take down the server, which means you'll get better uptime. 

SQL Server is a more reliable platform for critical applications like 
e-commerce because it has the capability to commit and roll back 
transactions at any point. If your system crashes, you can get all the 
transactions that had committed to that point without losing a great 
deal of data. 

Finally, SQL Server is a lot easier to administer remotely than is 
Access. Instead of having to download and upload the entire database 
every time, SQL Server lets you make all your changes via Enterprise 
Manager. For that reason, if nothing else, I prefer SQL Server to 
Access in almost all cases. 

If you can't afford SQL Server, you might want to look into 
Microsoft's Data Engine (MSDE). This is a good combination of SQL 
Server's reliability with a low cost (actually, free). For more 
information on MSDE, you can download this white paper from 
Microsoft's Web site. The paper discusses the differences between 
Access and MSDE: 

http://www.pcworld.com/r/tw/1%2C2061%2Ctw-vb4-25%2C00.html 

Note to all you Oracle fans: Oracle has all the same capabilities as 
SQL Server that I mentioned above. In fact, it is still the choice for 
most major e-commerce sites on the Web. SQL is catching up, but Oracle 
has always been able to deal with big databases better than SQL Server 
does.


*5. ACCESSING OBJECT PROPERTIES                 
        
If you're using properties of objects, such as recordsets or custom 
COM/DCOM objects, be sure to store the value in a variable so it can 
be used multiple times. It is much faster to access a local variable 
than it is to read the property of an object. (This relates to how the 
object is stored in memory.) As a rule of thumb, if I'm using a 
property more than once, I create a variable for it.


*6. CREATING PROJECT TEMPLATES                 
        
If you're like me, you'll always start a project the same way: adding 
controls, adding components and libraries, and so on. By adding your 
project to the Visual Basic templates folder, you can create your own, 
custom project, which in turn can be used in the future instead of 
making you do the same steps over and over again. Just save your 
project file, once you've gotten all the components and controls in 
it, into the Template\Projects directory in your VB installation 
directory. You'll then see the project come up in the New Project 
dialog box. You can also do this with forms, as well as some other 
types of documents. Look in the Template directory to see all the 
types of objects you can use as templates.


*7. USING TRANSPARENT GIFS IN VB                 
        
I've gotten this question a few times lately: How can I use a 
transparent GIF with Visual Basic? The short answer is that you can't 
. . . not with the controls that come with VB. While you can now use 
GIFs and JPEGs in VB, the controls aren't that smart and don't know 
how to handle transparency. If you need a control like this, you'll 
need to look at using a third-party product to do it.


*8. NO NEED TO WRITE CODE WHEN YOU'VE GOT THE DATA CONTROL

Databases make up the majority of Visual Basic applications. If you're
still coding to get the job done, stop! VB's Data control makes
accessing a database almost code free.

First, open VB and create a new form. Then, add a Data control to the
form. (You'll find the Data Control icon on the toolbar.) Next, create
the appropriate number of field controls. The number and type will
depend on the fields you want to access and the types of data you're
accessing.

After positioning the field controls, select the Data control so you
can set a few properties. Specifically, you need to identify the
database you're working with in the DatabaseName property. Be sure to
enter the entire path. The next step is to identify the table to which
your form is linked. Do so by specifying that table in the Data
control's RecordSource property.

Once you've established the linked database and data object (table),
you'll need to customize the field controls a bit. Just as you
established a link between the VB form and the database, you'll need
to link the field controls to the Data control. To do so, select each
control and change its DataSource property to the name of your Data
control. Next, in the DataField property, specify the field that you
want that particular control to display. You'll probably want to
update each control's label to reflect the field as well.

You're done, and you didn't write one line of code. Run your program
and watch the form move through the records in the linked table.


*9. WORKING WITH APIS                 
        
Finding the correct declaration information for an API can be a 
challenge. The easiest way is to use the API Viewer that comes with 
the Professional version of Visual Basic. If you're working with VB5, 
you'll find the API viewer under VB's Basic Start Menu group. VB6 
makes the API Viewer available as an add-in. Unfortunately, the 
declarations listed in the API Viewer aren't always correct, so you 
may still need an alternative source. However, many of the 
declarations are correct, and since the Viewer is so handy, it's the 
logical place to start.


*10. DISABLING THE WINDOWS CLOSE BUTTON                 
        
If you're like many developers, you want to control how and when a 
user exits a form. Unfortunately, Visual Basic forms include the 
Windows Close button (X), and there's no property that disables it. 
Don't let that limit your developing talents, however. Use the 
GetSystemMenu and RemoveMenu API functions to disable the Close 
button. First, open a module and enter the following declarations: 

Declare Function GetSystemMenu Lib "User32" (ByVal hWnd As Integer, 
ByVal bRevert As Integer) As Integer 

Declare Function RemoveMenu Lib "User32" (ByVal hMenu As Integer, 
ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer 

Global Const MF_BYPOSITION = &H400 

Then, open your form and enter the following code in the form's Load 
event: 

Private Sub Form_Load() 
SystemMenu% = GetSystemMenu(hWnd, 0) 
Res% = RemoveMenu(SystemMenu%, 6, MF_BYPOSITION) 
End Sub  

It's that simple--when you run the form, the Close button will be 
missing.
