
			Nod Programing VB Help Index
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
This is intended for free use.  The code here is for various skill levels, 
anyone from beginers to advanced programers can use these.  Do what you wish 
with the code it is free for you to use and manipulate!

****************************************************************************

Clearing all fields and combo boxes on a form:

Sometimes you want to clear all the fields and combo boxes on a data-entry 
form. If your form contains many controls, this could become tedious and 
error prone. The following subroutine clears the contents of such fields on 
your form automatically:

Public Sub ClearAllControls(frmForm As Form)
Dim ctlControl As Object

    ' Initialize all controls that can be initialized
    ' Any control with a text property or a list-index property
    On Error Resume Next
    For Each ctlControl In frmForm.Controls
        ctlControl.Text = ""
        ctlControl.ListIndex = -1
        DoEvents
    Next ctlControl

End Sub

Just call this procedure from your code like this:

Call ClearAllControls(Me)

****************************************************************************

Quickly switching an object's Enabled property:

You can easily switch an object's Enabled property with a single line of
code:

optSwitch.enabled = abs(optSwitch.enabled) - 1

Here's how the technique works: When Enabled is True, its numeric value is
-1. The absolute value of -1 is 1, so subtracting 1 from 1 would yield 0,
which is False. When Enabled is False, its numeric value is 0; 0 - 1 
would then yield -1, or True.

This technique is an enhancement of the common usage

fraOption.enabled = optSwitch.enabled

to have an object follow the value of any other object's Enabled property.

Note: This technique depends on VB's definition of True and False.
To make this technique less dependent on that definition, you can use the
following code:

OptSwitch.enabled = NOT OptSwitch.enabled

This code works for any Boolean data type.

****************************************************************************

Dealing with Null strings in Access database fields:

By default Access string fields contain NULL values unless a string value
(including a blank string like "")  has been assigned. When you read these
fields using recordsets into VB string variables, you get a runtime
type-mismatch error.

The best way to deal with this problem is to use the built-in & operator to
concatenate a blank string to each field as you read it. For example,

Dim DB As Database
Dim RS As Recordset
Dim sYear As String

Set DB = OpenDatabase("Biblio.mdb")
Set RS = DB.OpenRecordset("Authors")
sYear = "" & RS![Year Born]

****************************************************************************

Specifying maximum lengths in a ComboBox:

The ComboBox control doesn't have a MaxLength property like a TextBox does.
You can add some code to emulate this property, however. Just add the
following code to the KeyPress event of your ComboBox:

Private Sub Combo1_KeyPress(KeyAscii As Integer)
    'If the user is trying to type the eleventh key and...
    ' ...this key is not the Backspace Key, cancel the event!
    Const MAXLENGTH = 10
    If Len(Combo1.Text) >= MAXLENGTH And KeyAscii <> vbKeyBack Then
KeyAscii = 0
    '
End Sub

You can change the MaxLength value to any number you want. As you can see,
the code allows the user to use the [Backspace] key; you could enable other
keys by simply adding their KeyAscii values the way we did with [Backspace].

****************************************************************************

Sharing resource files between VB and C projects:

Suppose you want to use a resource file (RES) in your Visual Basic project,
but some of the file's resource indexes are greater than 0x8000. The VB
function LoadResString(index) receives an integer argument Index in the
range -32,768 to 32,767, so you can't pass values that are larger than
0x8000. You can solve this problem by passing the corresponding negative
index value, as follows (with 0 <=X < 0x8000):

RES            Visual Basic
0xFFFF - X     -X - 1=
0x8000+X       X-0x8000

For example, suppose you have the following RC file:

STRINGTABLE DISCARDABLE=
 BEGIN
 0xFFFF-0x0000  "resource string 1 with VB index -1 -0 = -1"
 0x8000+1       "resource string 2 with VB index - 32,768 + 1 = -32,767"
 END

To load string 1, you'll use LoadResString(-1). Similarly, to load string 2
you'll use LoadResString(-32767).

****************************************************************************

The CDbl function versus Val:

The Val() function is familiar, and it's useful for converting text box
numeric values to numbers. But if you use formatters to display large
numbers (with commas, for instance), there's a better function for your
purpose. The following examples illustrate the use of Val versus CDbl:

Code: print Val("12345")
Result: 12345

Code: print Val("12,345")
Result: 12

Code: print CDbl("12,345")
Result: 12345

Code: print CDbl("12345")
Result: 12345

Why are these functions different? The Visual Basic Help file offers
several hints. You should use the CDbl function instead of Val to provide
internationally aware conversions from any other data type to a Double. For
example, CDbl will recognize different decimal separators and thousands
separators properly depending on your system's locale.

Also, if you want your display and input routines to be automatically
reversible, you may want to consider using named numeric formats for
FORMAT(). Doing so helps guarantee a reversible process, given the LOCALE
setting of the user's machine.

****************************************************************************

Command me, oh great one:

Suppose you want to use Visual Basic to create an EXE that takes an input
value in a format like test.exe 2. Depending on the input value, you'll
perform certain tasks. In this situation, you can make use of the Command
function, which returns the argument portion of the command line you use to
launch VB or an EXE you develop in VB.

It's easy to send command-line information to an application. For instance,
to send information to an application called HappyApp, you could use the=
 line

HappyApp /CMD 1972

Now, within the application--probably in the Sub Main--you can use the
Command function to capture that command-line information.

To see this technique work, place a text box on a form. In the Form_Load
event, place the following line:

Text1.Text = Command

While still in VB, place some code on the command line. To do this in VB
3.0, choose Options | Project; in VB 4.0, choose Tools | Options..., then
click the Advanced tab; in VB 5.0, choose Project | Project Properties,
then click the Make tab. Next, type This is my argument in the Command Line
Arguments section and click OK. Run the application, and your command-line
text will appear in the text box.

Note that if you're working with 32-bit VB, I suggest creating an ActiveX
EXE or ActiveX DLL (formerly OLE Automation servers). By doing so, you
simply deal with property settings.

****************************************************************************

Displaying and processing a message box

The following code sample demonstrates an easy way to display and process a
message box (MsgBox) in any version of Visual Basic:

     Select Case MsgBox("Would you like to save the file somefile.txt?", _
     vbApplicationModal + vbQuestion + YesNoCancel, App.Title)
       Case vbYes
          'Save then file
       Case vbNo
          'Do something for No
       Case vbCancel
          'Do something else for Cancel
     End Select

This method works well, unless you need to save the answer from your Select
Case for later use. If you do, you'll need to use the more standard form of
prompting for the answer in a variable.

****************************************************************************

Passing strings to a DLL:

I recently came across a serious inefficiency in the way Visual Basic sends
strings to a DLL. The problem occurs when you want to get back a large
string field (32 KB) from a DLL written in C/C++. VB interacts somehow with
this string, causing significant overhead.

In order to call a DLL and get back a string-type data field, you must pass
a string and initialize it for as many bytes as you expect to be returned.
If you pass this function a small string, it will run quickly. But if you
pass it a large string (32 KB), the time will be significantly slower.
You'll see this slower performance even when no data is being returned,
meaning that the extra time results from some sort of VB overhead. As a
result, if speed is an issue when you're calling a DLL and passing a string
variable, you should pass a string that's only as large as you need.

You can find a sample project that demonstrates this problem in the file
Speed.zip at ftp.cobb.com/ivb/tipcode. The project simply loops for a
predetermined number of times and issues the standard windows API call
GetPrivateProfileString, which gets data from an INI file.

****************************************************************************

Making a text box read-only:

Here's a quick and easy way to make a text box read-only. Simply enter the
line

     keyascii = 0

in the textbox_keypress event.

The easiest way to make a text box read-only (in VB 4.0 and higher) is to 
set the text box's Locked property to True. If you want to use our original
technique, you'll need to enter the code in the KeyDown event | KeyPress 
doesn't trap the [Delete] key. However, if you don't set the Locked 
property to True, Windows 95 will let you right-click on the text box to 
open a context menu that gives you access to the Cut and Paste options.

****************************************************************************

Creating a formless application:

To create a VB program that has only console input and output--that is, no
dialog boxes or forms--you can use the Main procedure. Begin by creating a
new project. Open a code window, then choose Insert | Procedure.... In the
Insert Procedure dialog box, Select the Sub and Public options and enter
Main in the Name box. Click OK to create a new Main subroutine in the
General object. All your code will go in this routine; if you have any
useful BAS modules, you can add those to the project as well.

VB needs to know what code to execute when your application is called.
Since you're not using a form, you need to tell VB to start execution with
Sub Main. To do so, choose Tools | Options.... Click the Project tab and
select Sub Main from the Startup Form list. To remove the project's default
form, right-click on it in the Project window and choose Remove File from
the speed menu.

Testing a formless application can be a headache, so plan ahead: Use a log
file to get debug messages from your application. You'll want to read about
the Print # statement in VB's Help file, along with Open and Close.

Note that you can use this method to create a VB application that will run
as a service on NT. (Services can't have any forms or dialog boxes.)

****************************************************************************

Case sensitivity in DLL calls:

Use the Alias keyword to help convert non-case-sensitive VB 3.0 function
calls to their case-sensitive 32-bit counterparts.

When you declare or call a DLL in 32-bit Visual Basic, the name of the
function is case sensitive. To convert non-case-sensitive VB 3.0 calls to
case-sensitive calls, use the Alias keyword to hold the case-sensitive
function name. Place the name you want to call the function after the
Declare Sub/Function statement. (The Win32API.TXT file Aliases all function
calls, eliminating the case-sensitivity problem.)

****************************************************************************

			End of Help 2 of how many I do!!