|
|
VB Tips and Tricks
|
|
|
1 |
SORTING
AN ARRAY
Dim
blnSorted As Boolean
Dim
intTemp As Integer
Dim
X As Integer
blnSorted
= False
Do
While Not blnSorted
blnSorted
= True
For
X = 0 To UBound(intTheArray) - 1
If
intTheArray(X) > intTheArray(X + 1) Then
intTemp
= intTheArray(X + 1)
intTheArray(X
+ 1) = intTheArray(X)
intTheArray(X)
= intTemp
blnSorted
= False
End
If
Next
X
Loop
SortArray =
intTheArray()
End
Function
|
|
|
2 |
INSERTING A DELAY
API:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sleep
(nSecond)
End
Sub
|
|
|
3 |
ALLOW
KEYING IN OF ONLY HEX CHARACTERS
Private
Sub txtData_KeyPress(KeyAscii As Integer)
On
Error GoTo ErrtxtData_KeyPress
KeyAscii =
OnlyHex(KeyAscii)
Exit
Sub
ErrtxtData_KeyPress:
errMsg
End
Sub
Public
Function OnlyHex(KeyAscii As Integer, Optional strValidString As Variant) As Integer
On
Error GoTo ErrOnlyDigits
Dim
StrValid As String
If
IsMissing(strValidString) Then
StrValid
= "1234567890ABCDEFabcdef"
Else
StrValid
= strValidString
End
If
If
KeyAscii > 26 Then
If
InStr(StrValid, ChrW$(KeyAscii)) = 0 Then
Beep
OnlyHex
= 0
Else
OnlyHex
= KeyAscii
End
If
Else
OnlyHex
= KeyAscii
End
If
Exit
Function
ErrOnlyDigits:
MsgBox
"Error", vbInformation, "Error"
End
Function |
|
|
4 |
ALLOW ONLY NUMERIC DIGITS TO BE KEYED
In
the KeyPress event of the text box, use the KeyAscii value to check for numeric characters.
Private
Sub txtData_KeyPress(KeyAscii As Integer)
On
Error GoTo ErrtxtData_KeyPress
KeyAscii =
OnlyHex(KeyAscii)
Exit
Sub
ErrtxtData_KeyPress:
errMsg
End
Sub
Public
Function OnlyDigits(KeyAscii As Integer, Optional strValidString As Variant) As Integer
On
Error GoTo ErrOnlyDigits
Dim
StrValid As String
If
IsMissing(strValidString) Then
StrValid
= "1234567890."
Else
StrValid
= strValidString
End
If
If
KeyAscii > 26 Then
If
InStr(StrValid, ChrW$(KeyAscii)) = 0 Then
Beep
OnlyDigits
= 0
Else
OnlyDigits
= KeyAscii
End
If
Else
OnlyDigits
= KeyAscii
End
If
Exit
Function
ErrOnlyDigits:
MsgBox
"Error", vbCritical, "Error"
'
End
Function
|
|
|
5 |
LEFT SHIFT
|
|
|
6 |
RIGHT SHIFT
Public
Function RShift(ByVal lThis As Long, ByVal lBits As Long) As Long
If
(lBits <= 0) Then
RShift =
lThis
ElseIf
(lBits > 63) Then
RShift
= 0
Exit
Function ' ... error ...
ElseIf
(lBits > 31) Then
RShift
= 0
Else
If
(lThis And m_lPower2(31)) = m_lPower2(31) Then
RShift
= (lThis And &H7FFFFFFF) \ m_lPower2(lBits) Or m_lPower2(31 - lBits)
Else
RShift
= lThis \ m_lPower2(lBits)
End
If
End
If
RShift
= GetLoWord(RShift)
End
Function
|
|
|
7 |
ROUNDING NUMBERS
|
|
|
8 |
UNLOADING ALL FORMS IN A VB6 PROJECT
|
|
|
9 |
CHECK
IF A FILE EXISTS
On
Error GoTo ErrHandler
Call
FileLen(FullPathAndFile)
ErrHandler:
If
err = 0 Then FileExists = True
End
Function
|
|
|
10 |
PRINTING TO A FILE
Public
Sub Print_To_File(strFileName, Optional strDetails As String)
Dim
iFile As Integer
Screen.MousePointer =
vbHourglass
strPrint =
GetPrintInfoIntoArray
iFile
= FreeFile
If
Len(Trim$(strFileName)) <> 0 Then
Open
strFileName For Output As #iFile
'Print
#iFile, String(65, "-")
Print
#iFile, "Test Print"
Print
#iFile, "Register"; Tab(17); "TestCol1"; Tab(39); "TestCol2"; Tab(58); "TestCol3"
Print
#iFile, printing
a blank line
Print
#iFile, printing
a blank line
Close
End
If
Screen.MousePointer =
vbDefault
End
Sub
|
|
|
11 |
FLEX GRID: COPY CUT PASTE
Private
Sub flxArray_KeyUp(KeyCode As Integer, Shift As Integer)
If
((flxArray.Col = 2) Or (flxArray.Col = 3)) Then
Select
Case KeyCode
'
Copy
Case
vbKeyC And Shift = 2 ' Control + C
Clipboard.Clear
Clipboard.SetText
flxArray.Text
KeyCode
= 0
'
Paste
Case
vbKeyV And Shift = 2 'Control + V
flxArray.Text =
Clipboard.GetText
KeyCode
= 0
'
Cut
Case
vbKeyX And Shift = 2 'Control + X
Clipboard.Clear
Clipboard.SetText
flxArray.Text
flxArray.Text
= ""
KeyCode
= 0
'
Delete
Case
vbKeyDelete
flxArray.Text
= ""
End
Select
End
If
End
Sub
|
|
|
12 |
FLEXGRID:
ALLOW TYPING OF TEXT, MOVING TO THE NEXT ROW WHEN THE ENTER KEY IS PRESS ONLY FOR
SELECT ROWS.
In
this example, the cursor moves to the next row only for cols 2 and 3.
Private
Sub flxArray_KeyPress(KeyAscii As Integer)
If
((flxArray.Col = 2) Or (flxArray.Col = 3)) Then
Select
Case KeyAscii
Case
vbKeyReturn
'
When the user hits the return key
'
this code'll move the next cell or row.
With
flxArray
If
.Row + 1 <= .Rows - 1 Then
.Row
= .Row + 1
If
.Row > 10 Then .TopRow = .Row - 9
Else
.Row
= 1
End
If
End
With
Case
vbKeyBack
'
Delete the previous character when the
'
backspace key is used.
With
flxArray
If
Trim$(.Text) <> "" Then _
.Text
= Mid$(.Text, 1, Len(.Text) - 1)
End
With
Case
Is < 32
'
Avoid unprintable characters.
End
Select
End
If
End
Sub |
|
|
13 |
OPENING
PDF FILES FROM VB CODE
Private
Sub mnuViewFile_Click()
On
Error GoTo err
If
ShellExecute(Me.hWnd, "Open", App.Path & "\Application.pdf", "", "c:\", 1) Then
End
If
Exit
Sub
err:
MsgBox
"Cannot open file, vbOKOnly, strErrTitle
End
Sub |
|
|
14 |
PUTTING HTML LINKS ON A VB FORM
Private
Sub lblLink_Click()
On
Error GoTo err
If
ShellExecute(Me.hWnd, "Open", "http://www.rashmirahul.com", "", "c:\", 1) Then
End
If
Exit
Sub
err:
MsgBox
"Cannot reach hyperlink", vbOKOnly, strErrTitle
End
Sub
|
|
|
15 |
Opening
a splash form.
Should
be at the centre of the screen
Private
Sub Form_Load()
Dim
OnTop
Const
SWP_NOMOVE = 2
Const
SWP_NOSIZE = 1
Const
Flags = SWP_NOMOVE Or SWP_NOSIZE
Const
HWND_TOPMOST = -1
Const HWND_NOTOPMOST =
-2
'Centre
the form
Me.Move
(Screen.Width / 2) - (Me.Width / 2), (Screen.Height / 2) - (Me.Width / 2)
'show
it immediately
Show
'give
it time to show
DoEvents
'put
it ontop
OnTop =
SetWindowPos(Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, Flags)
End
Sub
|
|
|
16 |
GET THE SCREEN RESOLUTION
Private Sub Form_Load() |
|
|
17 |
MAKE THE ENTER KEY ACT LIKE THE TAB KEY
Private Sub Text1_KeyPress(KeyAscii
As Integer)
|
|
|
18 |
ALLOW ONLY ONE INSTANCE OF YOUR PROGRAM
Private Sub Form_Load()
|
|
|
19 |
CONVERT HEX TO DECIMAL
Private Sub Form_Load()
|
|
|
20 |
CONVERT DECIMAL TO HEX
|
|
|
21 |
DETERMINE WHICH OPTION BUTTONS HAVE BEEN SELECTED
When
attempting to determine which option buttons a user has selected
from an array of option buttons, use this code instead
intSelectedItem
= Option(0).Value * O - _
Option(1).Value*1
- Option(2).Value * 2
If you
have more than a few items, put this code in a loop:
intSelectedItems
= 0
For
iCount = 0 to N
'N
= total number of option boxes minus one
intSelectedItem =
Option(iCount).Value * iCount
Next
intSelectedItem
is the index of the option button that the user
|
|
|
22 |
CONVERT
A TEXT FILE
INTO ACCESS MDB
It can
be troublesome to convert a text file into an Access database.
the
Text ISAM driver and SQL to do the job for you. First, create
Dim
db As Database, tbl as TableDef
Set
db = DBEngine.CreateDatabase(App.Path & "\mymdb.mdb", dbLangGeneral, dbVersion_0)
Set
tbl = db.CreateTableDef("Temp")
tbl.Connect
= "Text;database=c:\vbpj\data"
tbl.SourceTableName
= "Customer#txt"
db.TableDefs.Append
tbl
db.Execute
"Select Temp.* into NewTable from Temp"
db.TableDefs.Delete
tbl.Name
db.Close
Set
tbl = Nothing
Set
db = Nothing
Now,
you only need to create indexes. You can use this method
|
|
|
23 |
SWAP TWO INTEGER VARIABLES
Use
this algorithm to swap two integer variables:
a =
a Xor b
b =
a Xor b
a =
a Xor b
|
|
|
24 |
READ
AND WRITE ARRAYS QUICKLY
You
can read and write arrays quickly from files using Put and
Dim
arr(1 To 100000) As Long
Dim
fnum As Integer
fnum
= FreeFile
Open
"C:\Temp\xxx.dat" For Binary As fnum
Put
#fnum, , arr
Close
fnum
|
|
|
25 |
CANCEL
A PENDING PRINT JOB
If you
use PrintManager,
the
printer. This code uses the Windows API AbortDoc to prevent
Declare
Function AbortDoc% Lib "GDI" ( ByVal hDC As Integer)
Add
this code to the cCancel buttons Click event procedure:
Sub
cCancel_Click ()
dim
i%
AbortPrintFlag=True
On
Local Error Resume Next
i
= AbortDoc(Printer.hDC)
End
Sub
Add
this code to the print routine:
AbortPrintFlag=False
Do
While ......
Printer.Print
......
DoEvents
If
AbortPrintFlag then exit do
Loop
On
Local Error resume next
Printer.EndDoc
|
|
|
26 |
RETURN
TO A PREVIOUS LOCATION
IN YOUR CODE
To return
to a previous location in your code, press Ctrl+Shift+F2.
feature
is available in design time and in break mode.
|
|
|
27 |
A
FAST WAY
TO FIND ROUTINES
To quickly
go to the code for a called function, subroutine, or
|
|
|
28 |
FORCE UPPERCASE CHARACTERS
IN A TEXT BOX
To change
text in a text box to uppercase as soon as the user
Private
Sub txtname_KeyPress(KeyAscii As Integer)
'Puts
in uppercase as soon as typed
KeyAscii =
Asc(UCase(Chr(KeyAscii)))
End
Sub
|
|
|
29 |
REGISTER ACTIVEX
COMPONENTS MANUALLY
Not
all ActiveX DLL and OCX files include installation programs
a shortcut
in your WINDOWS\SENDTO folder called Register
|
|
|
30 |
TEST
FOR FALSE INSTEAD
OF ZERO
If youre
checking a numeric value against 0, one option is to use
If iNumber
<> 0 Then
...
End
If
It is
faster, however, to check the variable with an If
If iNumber
Then
...
End
If
These
two statements are equivalent, but the second example
|
|
|
31 |
PROGRAMMATICALLY SELECTING MULTIPLE ROWS
IN A DATA GRID
To select
multiple, nonconsecutive rows (a tagged list) in
Sub
SelectSomeRows()
Dim
i As Integer
For
i = 1 To 10 Step 2
SSDataGrid1.EvalRowNumber
= i
SSDataGrid1.EvalRowIsSelected
= True
Next
i
|
|
|
32 |
SET AUTOREDRAW
TO FALSE
If AutoRedraw
is set to True, VB keeps a bitmap of the form in
AutoRedraw
to False if you are not using any graphics methods.
|
|
|
33 |
PREVENTING INVALID USE
OF NULL ERRORS
If your
app uses database or other functions that can return Null
string
and a variant to your target variable or control:
Text1.Text
= "" & SomeDynaset("SomeField")
|
|
|
34 |
MOVE CONTROLS
INTO A FRAME
To move
controls into a frame, select one or more controls and
also
can be used when moving controls onto other controls
|
|
|
35 |
USE
THE IMAGE CONTROL INSTEAD
OF THE PICTURE CONTROL
Because
the picture control has more overhead than the image
other
controls, align the picture either to the top or bottom,
most
critical of the so-called system resources).
|
|
|
36 |
CALL
THE CLICK EVENT
If you
need to fire a command buttons Click event, you can set
cmdAdd.Value
= True
The
real benefit of this is that you can call code in other form
Call
cmdAdd_Click
This
is true for all controls, not just the command button.
|
|
|
37 |
GET RID OF UNUSED DECLARE STATEMENTS
When
you use the Declare statement, approximately 11 bytes
|
|
|
38 |
REDEFINE YOUR TAB ORDER
When
you need to reset your tab indexes quickly (without buying
to the
very last control on the page that will receive a tab stop.
type
zero, click the third from the last control, type zero, and so
|
|
|
39 |
KEEPING ACCURATE TIME
IN VB
The
problem with the VB timer is that it
GetCurrentTime(),
which records the milliseconds since Windows
Place
a Timer control on the form that starts the application
Dim
Start&, Elapsed&
Declare
Function GetCurrentTime& Lib "User" ()
The
Form_Load event of this form must also contain this code:
Start
= GetCurrentTime
'This
routine sets the Timer interval to about a minute.
Sub
Timer1_Timer ()
Dim
MsgText$
Elapsed
= GetCurrentTime()
' if
10 minutes has elapsed since
' the
program was started
' or
the last msgbox was displayed
If Elapsed
- Start >= 600000 Then ' 10 minutes
MsgText = "Give your eyes a rest. _
Take a 5 minute break."
MsgBox MsgText, 16, "Take A Break"
' however long the msgbox is on the screen
' the timer is effectively set to 0 when the
' user presses OK
Elapsed = GetCurrentTime()
Start = Elapsed
Elapsed = 0
End
If
End
Sub
|
|
|
40 |
GET
THE LENGTH
OF THE LONGEST WORD
IN A STRING
LenLongestWord
finds the length of the longest word in a string
to spread
a column heading over multiple lines:
Function
LenLongestWord (ByVal src As String) As Integer
Dim
i As Integer, j As Integer
i
= InStr(src, " ")
If
i > 0 Then
j =
LenLongestWord(Mid$(src, i + 1))
If
j > i - 1 Then LenLongestWord = j Else LenLongestWord = i - 1
Else
LenLongestWord
= Len(src)
End
If
End
Function
|
|
|
41 |
WHEN
TO USE SENDKEYS
Use
the SendKeys function to exploit the delete functionality of
row
if your form has an option that deletes all highlighted rows.
Remember
not to use SendKeys to send a key value without
'Pass
the Key value to send and the
'Control
to which the value to be sent
Sub
SendKeyTo(KeyValue As String, cCnt As Control)
cCnt.SetFocus
SendKeys KeyValue
End
Sub
|
|
|
42 |
CALCULATE AGE USING DATEDIFF
Use
the function DateDiff to calculate an individuals exact age based
on birth date. DateDiff first calculates the total number of
account
for leap years. The Int function truncates the division
Function
CalcAge(datEmpDateOfBirth as Variant) as Integer
CalcAge =
Int(DateDiff(y,datEmpDateOfBirth,Date())/365.25)
End
Function
|
|
|
43 |
TEST
FOR LEAP YEAR
Simply
pass in the year you are testing, append that year to
Function
IsLeap(sYear As String) As Integer
If IsDate("02/29/" & sYear) Then
IsLeap = True
Else
IsLeap = False
End If
End
Function
|
|
|
44 |
QUICK TEST
FOR WEEKEND
To test
whether a date falls on a weekend, you might be inclined
nDay
= weekday (sDate)
If (nDay
= 1) or (nDay = 7) Then
'It's
a weekend
End
If
However,
you can use VBs MOD operator to perform the test
If (Weekday
(sDate) MOD 6 = 1) Then
'Its
a weekend
End
If
|
|
|
45 |
USED BYTES
IN A DIRECTORY
This
function returns the number of bytes used on the directory:
Function
DirUsedBytes(ByVal dirName As String)
As Long
Dim FileName As String
Dim FileSize As Currency
' add a backslash if not there
If Right$(dirName, 1) <> "\" Then
dirName = dirName & "\"
Endif
FileSize = 0
FileName = Dir$(dirName & "*.*")
Do While FileName <> ""
FileSize = FileSize + _
FileLen(dirName & FileName)
FileName = Dir$
Loop
DirUsedBytes = FileSize
End
Function
You
can call the function passing the name of a directory:
MsgBox
DirUsedBytes("C:\Windows")
|
|
|
46 |
GET USEFUL DISK INFORMATION
This
function returns the hard disk free bytes, total bytes, percentage
Dim
dsi As DISKSPACEINFO
dsi.RootPath
= "C:\"
GetDiskSpace
dsi
The
function returns all its results in the other field of the
' ***
Declaratiosn Section ******
Declare
Function GetDiskFreeSpace Lib _"kernel32"
Alias "GetDiskFreeSpaceA" (ByVal
lpRootPathName As String, lpSectorsPerCluster
As Long, lpBytesPerSector
As Long, _
lpNumberOfFreeClusters
As Long, lpTotalNumberOfClusters
As Long) As
Long
Type
DISKSPACEINFO
RootPath
As String * 3
FreeBytes
As Long
TotalBytes
As Long
FreePcnt
As Single
UsedPcnt
As Single
End
Type
' ******
Code Module ******
Function
GetDiskSpace(CurDisk As DISKSPACEINFO)
Dim
X As Long
Dim
SxC As Long, BxS As Long
Dim
NOFC As Long, TNOC As Long
X& =
GetDiskFreeSpace(CurDisk.RootPath, SxC, BxS, NOFC, TNOC)
GetDiskSpace
= X&
If
X& Then
CurDisk.FreeBytes
= BxS * SxC * NOFC
CurDisk.TotalBytes
= BxS * SxC * TNOC
CurDisk.FreePcnt =
((CurDisk.TotalBytes CurDisk._
FreeBytes)
/ CurDisk.TotalBytes) * 100
CurDisk.UsedPcnt =
(CurDisk.FreeBytes / CurDisk.TotalBytes) * 100
Else
CurDisk.FreeBytes
= 0
CurDisk.TotalBytes
= 0
CurDisk.FreePcnt
= 0
CurDisk.UsedPcnt
= 0
End
If
End
Function
As is,
this routine works with drives with a capacity of 2GB
|
|
|
47 |
LIMIT
USER TYPING IN COMBO BOX
The
standard textbox has a MaxChars property that lets you limit
simple
API call:
Private
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd _
As Long,
ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private
Const CB_LIMITTEXT = &H141
Private
Sub Form_Load()
Const
Max_Char = 24
Call
SendMessage(Combo1.hWnd, CB_LIMITTEXT,
Max_Char, 0&)
End
Sub
|
|
|
48 |
A
FORM THAT WONT CLOSE
If you
set a forms ControlBox property to False, the Minimize
the
form, but not to close the form using the control box. Simply
' uncomment
next line in VB3
' Const
vbFormControlMenu = 0
Private
Sub Form_QueryUnload(Cancel As Integer,
UnloadMode As Integer)
If UnloadMode =
vbFormControl_Menu Then
Cancel =
True
End
If
End
Sub
|
|
|
49 |
ROUND
AND FORMAT
THE EASY WAY
Do you
sometimes need to format rounded numbers to a specific
n =
12.345
Format(n,
"0.00\0")
'returns
"12.350"
Format(n,
"0.\0\0")
'returns
"12.00"
Format(0.55,
"#.0\0") 'returns ".60"
|
|
|
50 |
AVOID
THE FLICKERING
Developers
often need to load forms with information, which is
as the
information goes into it. Solve this by bringing
#If
Win16 Then
Declare
Function LockWindowUpdate Lib "User"
(ByVal hWndLock As Integer) As Integer
#Else
Declare
Function LockWindowUpdate Lib _
"user32"
(ByVal hWndLock As Long) As Long
#End
If
The
hWndLock variable refers to the hWnd property of the
hwndLock,
youll free up the screen and all updates will be shown
Dim
lErr as Long
Dim
x as Integer
'No
list box flicker, it will appear blank for 'just a moment
Screen.MousePointer
= vbHourglass
lErr
= LockWindowUpdate(Me.hWnd)
For
x = 1 to 5000
lstMyListbox.AddItem
CStr(x)
Next
Now
all the information is there:
lErr
= LockWindowUpdate(0)
Screen.MousePointer
= vbDefault
|
|
|
51 |
WATCH HOW YOU USE YOUR BOOLEANS
With
the introduction of the Boolean data type in VB4, you might
a Boolean
into -1 (or 1) as you might expect. Use the Abs or
Val(True)
gives 0
CInt(True)
gives -1
Abs(True)
gives 1
|
|
|
52 |
NUMERIC CONVERSION
OF STRINGS
When
dealing with numerics and strings, be advised of a couple
blindly
switch to CLng, CInt, and so on, which are internationally
will
raise an error if used as such. Consider wrapping your own
Public
Function CInt(IntValue as Variant) as Integer
If IsNumeric(IntValue)
Then
MyCInt
= CInt(IntValue)
Else
MyCInt
= 0
End
If
|
|
|
53 |
PREVENT CHECKBOX CHANGES
Youll
often want to display a checkbox-style listbox to show
that
is, to change which boxes they checked. You cant
the
listbox doesnt have a Locked property.
Option
Explicit
Dim
mbDisabled As Boolean
Private
Sub Command1_Click()
mbDisabled
= Not mbDisabled
End
Sub
Private
Sub List1_ItemCheck(Item As Integer)
If
mbDisabled Then
List1.Selected(Item)
= Not List1.Selected(Item)
End
If
End
Sub
When
mbDisabled is set to True, the changes made by the
still
scrollable.
|
|
|
54 |
AVOID IIF INEFFICIENCIES
The
IIf functionwhich returns one of two values determined
IfElse...End
If block. However, IIf is designed to execute both
? IIf(True,
MsgBox("True Part"), MsgBox("False Part"))
Obviously,
its extremely inefficientand possibly errorinducing
|
|
|
55 |
Split
Strings Cleanly
The
Split function is great for parsing strings, but what happens when a string has more
than one consecutive delimiter? It might seem odd that Split() returns empty substrings
as placeholders for the data missing between delimiters, but thats exactly what needs
to happen so these data positions arent lost.
Unfortunately,
Split() does not have an option to ignore multiple delimiters. CleanSplit() uses the
same arguments as Split() and efficiently discards empty substrings caused by more
than one delimiter in a row:
Public
Function CleanSplit(ByVal Expression As String, Optional By Val Delimiter As String
= " ", Optional ByVal Limit As Long = -1, Optional Compare As VbCompareMethod = vbBinaryCompare)
As Variant
Dim
varSubstrings As Variant, i As Long
varSubstrings
= Split(Expression, Delimiter, Limit, Compare)
'mark
empty substrings with delimiter because
'the
delimiter won't be around after Split()
For
i = LBound(varSubstrings) To UBound(varSubstrings)
If
Len(varSubstrings(i)) = 0 Then
varSubstrings(i) =
Delimiter
End
If
Next
i
CleanSplit
= Filter(varSubstrings, Delimiter, False)
End
Function
|
|
|
56 |
CONVERT
A DECIMAL NUMBER TO BASE N
Heres
a function that converts a decimal number (base 10) to another base number system.
Each digit position corresponds to a power of N, where N is a number between 2 and
36. In other words, if a number systems base is N, then N digits are used to write
numbers in that system. For example, the base 2 number system (binary) uses the digits
0 and 1, while the base 20 system uses digits 0 through K.
The
ConvertDecToBaseN function accepts a double-value decimal number and a byte-value
representing the base number between 2 and 36. By default, the base value used is
16 (hexadecimal). The decimal number is converted to a positive number if its negative.
This function is useful for representing large numbers as strings, using fewer digit
positions. I developed it to help reduce the footprint of several large numbers used
in constructing a 16-character unique string ID. (Creating a complementary function
to convert a base N number back into a decimal would be a great exercise.)
Public
Function ConvertDecToBaseN(ByVal dValue As Double, Optional
ByVal byBase As Byte = 16) As String
Const BASENUMBERS As String =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim
sResult As String
Dim
dRemainder As Double
On Error
GoTo ErrorHandler
sResult
= ""
If (byBase
< 2) Or (byBase > 36) Then GoTo Done
dValue
= Abs(dValue)
Do
dRemainder
= dValue - (byBase * Int((dValue / byBase)))
sResult
= Mid$(BASENUMBERS, dRemainder + 1, 1) & sResult
dValue
= Int(dValue / byBase)
Loop
While (dValue > 0)
Done:
ConvertDecToBaseN
= sResult
Exit
Function
ErrorHandler:
Err.Raise
Err.Number, "ConvertDecToBaseN", _
Err.Description
End
Function
Sample
usage:
ConvertDecToBaseN(999999999999#,
36)
'Returns
'CRE66I9R
|
|
|
57 |
COUNT
THE OCCURRENCES OF A CHARACTER OR SUBSTRING
VB6
introduced the Split function, which returns a zero-based, one-dimensional array containing
a specified number of substrings. Although this function is useful in itself, you
can also use it in other useful ways. For example, by combining the UBound and Split
functions, you can count how many times a substringor single characterappears inside
another string:
Function
InstrCount(Source As String, SearchString As String) As Long
If Len(Source)
Then
InstrCount
= UBound(Split(Source, SearchString))
End
If
End
Function
|
|
|
|
|
|