What's New
Q & A
Tip Jar
Tips & Tricks
These are brief tips and tricks that can make Visual Basic programming easier.
New Control Usage Numerical Stuff
Starting & Stopping I/O Bug Alerts
Software Engineering Printing System Issues
Graphics Coding Techniques Other Programs
Databases Misc Forms

Numerical Stuff

Find Ordinal Extensions for Strings
(By Martin Allen)
Examine the last character in the string and add the correct extension.

Public Function DateExtension(ByVal DateNum As String) As String
Dim date_extension As String

    date_extension = ""
    If Len(DateNum) > 1 Then
        Select Case Right$(DateNum, 2)
            Case "11", "12", "13"
                date_extension = DateNum & "th"
        End Select
    End If

    If Len(date_extension) = 0 Then
        ' We still haven't found the answer.
        Select Case Right$(DateNum, 1)
            Case "1"
                date_extension = DateNum & "st"
            Case "2"
                date_extension = DateNum & "nd"
            Case "3"
                date_extension = DateNum & "rd"
            Case Else
                date_extension = DateNum & "th"
        End Select
    End If

    DateExtension = date_extension
End Function

(By Maurice Calvert)
To round to the nearest 5 cents you can use the easy formula
    CInt(amount * 20) / 20

Combining Characters Using Xor
(By Ken Ives)
Have you ever wondered about the Xor command and how it works? Of course, if you really want to get confused, read the VB help file. This will will ruin your day. :-)

When you use the Xor command, you are making a bitwise comparison of the binary representation of two values. Xor stands for "exclusive or." A bit in A Xor B is true is the corresponding bit in A is set or the corresponding bit in B is set but not both.

Remember that a binary values consists of bits, each representing a power of 2. For example, the binary value 63 is represented by 00111111 = 32 + 16 + 8 + 4 + 2 + 1.

When working with text, one 8-bit byte represents one character with a value between 0 and 255. For example:

A650100000164 + 1 = 65
?630011111132 + 16 + 8 + 4 + 2 + 1 = 63

If you combine these binary values one bit at a time using Xor, you get 01111110 which represents 64 + 32 + 16 + 8 + 4 + 2 = 126 which is the ASCII code for the "~" character. Therefore "A" Xor "?" = "~"

Encrypting With Xor
(By Marc van Steijn)
'Date       : 25-01-2001
'Functie     : String crypter
'Description: Call this function to crypt and call
'    it also to decrypt a string
'Url: http://vb.netmenu.nl
Public Function CryptString(ptSource As String, _
    ptPassword As String) As String

   Dim tdest As String
   Dim lteller As Long
   Dim lPasswTeller As Long

   tdest = ptSource
   For lteller = 1 To Len(ptSource)
      lPasswTeller = lPasswTeller - 1
      If lPasswTeller < 1 Then lPasswTeller = Len(ptPassword)

      Mid$(tdest, lteller, 1) = _
          Chr$(Asc(Mid$(ptSource, lteller, 1)) Xor _
              Asc(Mid$(ptPassword, lPasswTeller, 1)))
   Next lteller
   CryptString = tdest

End Function

[Note that this function may produce non-printable characters and that can have some bad side effects depending on what you do with the results. For example, if the results contain a formfeed character (ASCII 12), a TextBox will truncate the results at that character. -- Rod]

Take Roots
To take a root, use:
    root_value = number ^ (1 / root)
For example, the cubed root of X is:
    root_value = X ^ (1 / 3)

Seed the Random Number Generator
You can use Rnd -1 followed by Randomize to seed Visual Basic's random number generator. Whenever you use the same seed, Rnd will produce the same sequence of random numbers.
    Rnd -1
    Randomize seed

Encrypt Code Easily
You can use random number generator seeds to quickly encrypt information. Seed the generator with a key value. Then for each character in the plaintext message, Xor the character with the output of Rnd to get the cypertext character.

To decrypt, seed the generator with the same key value as before. Then for each character in the encrypted message, Xor the character with the output of Rnd to get the plaintext character.

Send your Tips and Tricks to feedback@vb-helper.com.

Subscribe to the VB Helper newsletter
Copyright © 1997-2001 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
www.vb-helper.com/tips2.htm Updated