| This is a page for little code snippets
and useful bits of information. Accessing Environment Variables
A little out of date
but still useful stuff - particularly under NT and Windows 2000 where these can be set by
user rather than just for the machine. Do remember that environment variables are case
sensitive. This code sample assumes that the environment variable set is MYENV and that it
is a path to an application data set.
Dim EnvValue as String
EnvValue
= Environ$("MYENV")
If EnvValue = "" Then
EnvValue = "C:\DEFAULTFOLDER\"
End If
If Right$(EnvValue, 1) <> "\" Then
EnvValue = EnvValue + "\"
End If
Numeric Entry to a
Text Box
You can call this
routine from the KeyPress event of any text box by placing a line in the event code that
looks like:
Private Sub
Text1_KeyPress(KeyAscii As Integer)
KeyAscii = NumericOnly(KeyAscii)
End Sub
and the routine looks
like this:
Public Function
NumericOnly(KeyAscii As Integer) As Integer
If Not IsNumeric(Chr$(KeyAscii)) And Not KeyAscii = vbKeyBack Then
NumericOnly = 0
Else
NumericOnly = KeyAscii
End If
End Function
Of course sometimes
you want the user to be able to enter decimal points, minus signs or even currency symbols
so how about:
Public Function
NumericOnly(KeyAscii As Integer, Optional extrachar As Variant) As Integer
Select Case Chr$(KeyAscii)
Case "0" To "9", Chr$(vbKeyBack)
NumericOnly = KeyAscii
Case Else
NumericOnly = 0
If Not IsMissing(extrachar) Then
If InStr(extrachar, Chr$(KeyAscii))
Then
NumericOnly =
KeyAscii
End If
End If
End Select
End Function
You just pass any
optional characters in the extrachar variable. You can omit the variable if none are
required in a given instance.
Adding Lines
to a MsgBox message
If you need to build up a message string for a message box
- perhaps while doing some validation you might want to separate new additions onto a new
line within the message box. You could keep testing the string to see if it has any
existing content and then add a linefeed character if it has.
Your code might look like this in a number of places in
the validation routine:
If Msg > "" then
Msg = Msg & vbLF & "Found yet
another fault"
Else
Msg = "Found yet another fault"
End If
The alternative is to append the function below and then
use something like:
Msg = AddLines(Msg, "Found a
Fault")
Private Function AddLines(ByVal TargetString As String,
ByVal NewLineString As_ String) As String
If TargetString > "" Then
TargetString = TargetString & vbLf
End If
TargetString = TargetString & NewLineString
AddLines = TargetString
End Function
Ascii Codes
For a full list of the
default ascii codes click here. The quick reference below
will help in most instances. Select, cut and paste it somewhere handy.
| a-z |
97-122 |
| A-Z |
65-90 |
| 0-9 |
48-57 |
| . |
46 |
| - |
45 |
| Space |
32 |
| BackSpace |
8 |
| Tab |
9 |
| LineFeed |
10 |
| Return/Enter |
13 |
|
|