back up codes

always have a backup of your code

space & strdup function

The Space function returns a string consisting of the specified number of spaces.


Dim myString1, myString2 As String
myString1 = Space(10)
myString2 = "Hello" & Space(10) & "World"

myString1 returns a string with 10 spaces.
myString2 will insert 10 spaces between two strings.

The StrDup function returns a string or object consisting of the specified character repeated the specified number of times.


Dim myString As String = "Wow! What a string!"
Dim myObject As New Object
Dim myString1, myString2, myString3 As String
myObject = "This is a String contained within an Object"
myString1 = StrDup(5, "P")
myString2 = StrDup(10, myString)
myString3 = CStr(StrDup(6, myObject))

myString1 returns PPPPP.
myString2 returns WWWWWWWWWW.
myString3 returns TTTTTT.

chr & chrw functions

Chr and ChrW functions Returns the character associated with the specified character code.

Chr uses the Encoding class in the System.Text namespace to determine if the current thread is using a single-byte character set (SBCS) or a double-byte character set (DBCS). It then takes CharCode as a code point in the appropriate set. The range can be 0 through 255 for SBCS characters and -32768 through 65535 for DBCS characters. For charts of the single-byte ASCII characters, see ASCII Character Codes.The returned value depends on the code page for the current thread, which is contained in the ANSICodePage property of the TextInfo class in the System.Globalization namespace. You can obtain ANSICodePage by specifying System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage.

ChrW takes CharCode as a Unicode code point. The range is independent of the culture and code page settings for the current thread. Values from -32768 through -1 are treated the same as values in the range +32768 through +65535.

Numbers from 0 through 31 are the same as standard nonprintable ASCII codes. For
example, Chr(10) returns a line feed character.

The following example uses the function Chr.


Dim myChar1, myChar2, myChar3, myChar4 As Char
myChar1 = Chr(65)
myChar2 = Chr(97)
myChar3 = Chr(62)
myChar4 = Chr(37)

myChar1 will return A.
myChar2 will return a.
myChar3 will return >.
myChar4 will return %.

rtrim, ltrim function

LTrim function returns a string containing a copy of a specified string with no leading spaces while RTrim function returns a string containing a copy of a specified string with no trailing spaces.

The following example uses the LTrim function to strip leading spaces and the RTrim function to strip trailing spaces from a string variable.


Dim myTestString As String = " Trim "
Dim myTrimString1, myTrimString2, myTrimString3 As String
myTrimString1 = LTrim(TestString)
myTrimString2 = RTrim(TestString)
myTrimString3 = LTrim(RTrim(TestString))

myTrimString1 returns “Trim “.
myTrimString2 returns ” Trim”.
myTrimString3 returns “Trim”.

len function

The Len Function returns an integer containing either the number of characters in a string or the nominal number of bytes required to store a variable.


Dim myTestString As String = "Hello World"
Dim myTestLen As Integer = Len(myTestString)

The code would return 11.

str function

The Str function returns a string representation of a number.If a positive number is converted to a string, a leading space is always reserved for its sign.


Dim myTestString1, myTestString2, myTestString3 As String
myTestString1 = Str(459)
myTestString2 = Str(-459.65)
myTestString3 = Str(459.001)

The first line of code will return a ” 459″.
The second line will return a “-459.65″.
The last line will return a ” 459.001″.

enable/disable dropdownlist

The following code will disable the control:


Public Sub disabledddl(ByVal cs As ControlCollection)
Dim d As Control
For Each d In cs
If TypeOf d Is DropDownList Then
CType(d, DropDownList).Enabled = False
End If
disabledddl(d.Controls)
Next
End Sub

The following code will enable the control


Public Sub enabledddl(ByVal cs As ControlCollection)
Dim d As Control
For Each d In cs
If TypeOf d Is DropDownList Then
CType(d, DropDownList).Enabled = True
End If
disabledddl(d.Controls)
Next
End Sub

Then call the sub routine as:


enabledddl(Me.Controls)
disabledddl(Me.Controls)

apostrophe & quotation marks in sql

There are problems that arises whenever a string containing apostrophe and/or quotation mark are searched.

Assuming you have a SQL string as below:


mySQLString = "SELECT * FROM myTable WHERE theLastName='" & LastName & "'"

Whenever variable LastName contains a name like O’Kien, the SQL search string becomes


SELECT * FROM myTable WHERE theLastName='O'Kien'

The second ‘ ends the SQL statement, leaving the rest of the string dangling.

The solution is to replace each ‘ in O’Kien with two ‘s, something like O”Kien, so the final SQL select statement looks like:


SELECT * FROM myTable WHERE theLastName='O''Kien'

To use the Replace() function, use the following code or this.


LastName = Replace(LastName, "'", "''")

filter function

The Filter function returns a zero-based array containing a subset of a string array based on specified filter criteria.


Dim myString() As String
Dim theString(2) As String
theString(0) = "This"
theString(1) = "Is"
theString(2) = "It"
myString = Filter(theString, "is", True, CompareMethod.Text)
myString = Filter(theString, "is", True, CompareMethod.Binary)
myString = Filter(theString, "is", False, CompareMethod.Binary)

First filter will result “This”, “Is”.
Second filter will return “This”.
Third filter will return “Is”, “It”.

Oct function

The Oct function will returns a string representing the octal value of a number.


Dim MyOctal1, MyOctal2, MyOctal3 As String
MyOctal1 = Oct(4)
MyOctal2 = Oct(8)
MyOctal3 = Oct(459)

MyOctal1 will return 4.
MyOctal2 will return 10.
MyOctal3 will return 713.

Hex function

The Hex function returns a string representing hexadecimal value of a number.


Dim MyHex1, MyHex2, MyHex3 As String
MyHex1 = Hex(5)
MyHex2 = Hex(10)
MyHex3 = Hex(459)

MyHex1 will return 5.
MyHex2 will return A.
MyHex3 will return 1CB.

Post Navigation

Follow

Get every new post delivered to your Inbox.