K & K Consulting    K & K Consulting

K&K Home VB Guru Home Search VB Site VB Code VB Tips VB Tutorials VB Questions

K&K Home

Word O' The Month


Occasionally, I will pick a word or phrase from the VB language and explain how to use it and why.

Word History

  Feb 2002: Command
Oct 2000: Split() July 2000: IIF Function
June 2000: Enum May 2000: With Statement

Feb 2002

Command Function

The Command Function allows you to pass parameters to a VB application when it is starting, this is all so very stream line when you use the Split() function mentioned in the Oct 2000 Word O’ The Month.

So, if you had an application called MyApp.exe and you wanted to pass it, say a path to your temp dir, you could then create a shortcut that would look like this:

MyApp.exe C:\Temp

If you want to pass more then one Param and you are using the Split() function, your shortcut would look like this:

MyApp.exe C:\Temp,D:\Temp Notice the comma between the two params.

This is also very handy when debugging a Standard EXE project in the VB IDE, if you click on the Make tab of the project properties, you will see a text box called “Command Line Arguments”. You place your parmas in this text box to pass the parameters when you want to debug.

Let’s Try It Out.

Create a new standard EXE project, in the form_load event, paste the following code:

MsgBox Command

Now place any text you want in the “Command Line Arguments” section of the make tab in the project properties.

Run your project and a message box should appear with the text you entered.

OK, lets take it one step further and combine it with the Split() function. Add a label to your form, next, paste the following code in your form_load Event:

Dim CommandString() As String
Dim counter As Long

Label1.Caption = Empty

CommandString = Split(Command, ",")

For counter = 0 To UBound(CommandString)
Label1.Caption = Label1.Caption & CommandString(counter) & vbCrLf

Now, add the following text to the “Command Line Arguments” section of the make tab in the project properties: this,is,a,test

Run your app and see what happens.


Back To Top


Oct 2000

Split Function

The Split function is a fast and efficient way of parsing delimited strings.

The Split function takes a string and converts it into an array of strings. By default, it divides the string into elements by using the space character as a delimiter, so that if you pass in a sentence, each element of the array contains a word. For example, if you pass this string to the Split function

"The VB Guru"

you'll get an array that contains these following  elements:


You can specify that the Split function split the string based on a different delimiter by passing in the delimiter argument.

Once you've split a string into an array, it's easy to work with the individual elements. The Split function sizes the array for you, so you don't have to worry about maintaining the array's size.

The following example uses the Split function to split a string and return the array.

Public Function SplitMyString(ByVal MyString As String, ByVal Delimiter As String) As Variant

  Dim MyBuffer() As String

  MyBuffer = Split(MyString, Delimiter, , vbBinaryCompare)

  SplitMyString = MyBuffer

End Function

To test this function, create a new project, paste in the SplitMyFunction code, add a command button to your form and add the following code:

Private Sub Command1_Click()
 Dim MyString As String
 Dim foo() As String

 MyString = "The,VB,Guru"

 foo = SplitMyString(MyString, ",")
End Sub


Back To Top

July 2000

IIF Function

The IIF() Function is a clean, "Code Neat", mini If statement. It will Return one of two parts, depending on the evaluation of an expression.  The prototype is as follows:

IIf(expr, truepart, falsepart)

The IIf function syntax has these named arguments:

Part Description
expr Required. Expression you want to evaluate.
truepart Required. Value or expression returned if expr is True.
falsepart Required. Value or expression returned if expr is False.

IIf always evaluates both truepart and falsepart, even though it returns only one of them. Because of this, you should watch for undesirable side effects. For example, if evaluating falsepart results in a division by zero error, an error occurs even if expr is True.

Now, lets see how we would use it. Lets look at some code using a standard If Statement:

Option Explicit

Public TestInt As Integer

Public Function StandardIf() As String

   If TestInt > 0 Then
      StandardIf = "Greater Then 0"
   ElseIf test >= 0 Then
      StandardIf = "0 or Less"
   End If

End Function

As you can see, this code is not too bad, but it would be much better if we wrote this instead:

Option Explicit

Public TestInt As Integer

Public Function FastIIf() As String

   FastIIf = IIf(TestInt > 0, "Greater Then 0", "0 or Less")

End Function

Back To Top


June 2000

Enum Statement

You can also use Integer variables to represent enumerated values. An enumerated value can contain a finite set of unique whole numbers, each of which has special meaning in the context in which it is used. Enumerated values provide a convenient way to select among a known number of choices, for example, black = 0, white = 1, and so on.

Enumeration variables are variables declared with an Enum type. Both variables and parameters can be declared with an Enum type. The elements of the Enum type are initialized to constant values within the Enum statement. The assigned values can't be modified at run time and can include both positive and negative numbers. For example:

Enum SecurityLevel
  IllegalEntry = -1
  SecurityLevel1 = 0
  SecurityLevel2 = 1
End Enum

An Enum statement can appear only at module level. Once the Enum type is defined, it can be used to declare variables, parameters, or procedures returning its type. You can't qualify an Enum type name with a module name. Public Enum types in a class module are not members of the class; however, they are written to the type library. Enum types defined in standard modules aren’t written to type libraries. Public Enum types of the same name can't be defined in both standard modules and class modules, since they share the same name space. When two Enum types in different type libraries have the same name, but different elements, a reference to a variable of the type depends on which type library has higher priority in the References.

OK, lets look at ways we can use this. Let's say we declare a function as follows:

Private Function MyFunction(ByVal UserName as string, Level as SecurityLevel) as Boolean

  'Do some stuff

End Function

You are guaranteed that the calling function will only supply SecurityLavel with a value of between -1 & 1.Plus, the developer using this function will be supplied with the nifty, intellitype,  dropdown, with just the words representing the values in your Enum.  This help the developer by not having to remember the valid values for your function.  Now, remember, the Enum is not just limited to Sub's & Functions, you may also use them as propertys, in User Defined types ect.

Back To Top



May 2000

With Statement

You often need to perform several different actions on the same object. For example, you might need to set several properties for the same object. One way to do this is to use several statements.

Private Sub Form_Load()
    Command1.Caption = "OK"
    Command1.Visible = True
    Command1.Top = 200
    Command1.Left = 5000
    Command1.Enabled = True
End Sub

Notice that all these statements use the same object variable, Command1. You can make this code easier to write, easier to read, and more efficient to run by using the With...End With statement.

Private Sub Form_Load()
    With Command1
        .Caption = "OK"
        .Visible = True
        .Top = 200
        .Left = 5000
        .Enabled = True
    End With
End Sub

You can also nest With statements by placing one With...End With statement inside another With...End With statement.

One little known use is the ! (bang operator) on recordsets, it works with the With Statement Too.

Private Sub DoSomething()
    With rsCustomer
        Do While .EOF = False
            Name = !CustName
            CustNum = !CustomerNumber
            If NOT .EOF then .MoveNext
    End With
End Sub

OK Guru, I can see how it makes life easier for the developer, but how is it more efficient code, you may ask.

Every level down in an object you go (like a .SomeThing), the compiler has to look keep looking in memory for the address pointer of that object.  The Object levels are probably not linear in memory, so the OS has to keep looking  up those addresses.  By using the With Statement, the addresses are all cashed in memory, making your code faster and more efficient.

Back To Top





Send mail to WebMaster with questions or comments about this web site.
This website is best viewed with a screen resolution of 800*600 or better.
This website is optimized for Microsoft Internet Explorer 6.x
K&K Consulting, Proud to be a Microsoft Business Partner.
Last modified: January 31, 2002