|
-
Dec 15th, 2004, 12:45 PM
#1
Thread Starter
PowerPoster
VB 6 - Useful Vb Functions
VB Functions
Please do not respond to this post, if you have any comments/thank-yous/corrections etc please contact me Via Pm or use the rating system Thanks.
Click a link to go directly to that function Post
String Functions
Conversion Functions
Dates And Time
Maths Functions
Other Functions
Click a link above to go directly to that function Post
Last edited by Pino; Jun 14th, 2005 at 01:31 PM.
-
Dec 15th, 2004, 12:46 PM
#2
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
InStr
Use -
VB Code:
instr([Start],[String 1],[String 2],[Compare Method])
Its not as bad as it looks firstly to keep this simple we can ignore the [Compare Method] and we will always set the [start] value to 1,
[String 1] = Is the string we will be looking in
[String 2] = Is the string we will be looking for
Instr will return the position of [String 2] in [String 1]
Example
VB Code:
MsgBox InStr(1, "abcdefghi", "d")
Will return 4 because that is the position of [String2] ('d') within [String1]
Last edited by Pino; Dec 22nd, 2004 at 10:09 AM.
-
Dec 15th, 2004, 12:47 PM
#3
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
Left And Right Functions
Left
Use –
Code:
[String2] = Left([String1],[Length])
This will put the the number of characters specified from [String1] (starting from the left) into [String2]
Example
Code:
mystring = "abcdefghijk"
newstring = Left(mystring, 4)
newstring would now be “abcd”
Right
Is exactly the same as above but works from the right hand side
Example
Code:
mystring = "abcdefghijk"
newstring = Right(mystring, 4)
newstring would now be “hijk”
-
Dec 15th, 2004, 12:48 PM
#4
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
Mid
The mid function is slightly different
Use –
Code:
String2 = Mid([String 1],[Start], [Length])
[String 1] is the string we will be looking in
[Start]Is the start position of where we will begin (See example)
[Length] Is the number of characters we will be copying
Example
Code:
mystring = "abcdefghijk"
newstring = Mid(mystring, 5, 3)
So here we are starting from the 5th character along and taking 3 characters from that position so, newString would now be “efg”
-
Dec 15th, 2004, 12:49 PM
#5
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
Split
This is a very useful function, it separates certain parts of a string using a delimiter for this one the results must be stored in an array (Assume SplitString is an array)
Use -
Code:
SplitString = Split([Expression],[Delimiter])
‘ For now this is all we need to worry about
[Expression] is the string we will be splitting up
[Delimiter] is the character that we are using to break up each ‘part’ of the string
Example
Code:
mystring = "ab,cd,ef,gh,ijk"
newstring = Split(mystring, ",")
Now how do we go about getting the values? Well because its an array Vb has indexed each ‘section’ in order so
Code:
newstring(0) = “ab”
newstring(1) = “cd”
newstring(2) = “ef”
newstring(3) = “gh”
newstring(4) = “ijk”
-
Dec 15th, 2004, 01:04 PM
#6
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
InStrRev
Use
Code:
result = InStrRev([StringCheck],[StringMatch])
This function returns 1 if [StringMatch] is contained in [StringCheck]
Example
Code:
mystring = "abcdefghijk"
otherstring = "p"
newstring = InStrRev(mystring, otherstring)
newString here would return 0 because [otherString] is not in [Mystring]
where as ...
Code:
mystring = "abcdefghijk"
otherstring = "abc"
newstring = InStrRev(mystring, otherstring)
newString here would return 1 because [otherString] is not in [Mystring]
-
Dec 15th, 2004, 01:12 PM
#7
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
UCase and LCase
These functions make a chosen string UpperCase (Capital letters) or LowerCase (Small Letters)
UCase
Use -
Code:
[NewString] = Ucase([String])
Example -
Code:
mystring = "abcdefghijk"
otherstring = UCase(mystring)
[Otherstring] would now be "ABCDEFGHIJK"
LCase
Use -
Code:
[NewString] = Lcase([String])
Example -
Code:
mystring = "ABCDEFGHIJK"
otherstring = LCase(mystring)
[OtherString] would now be "abcdefghijk"
-
Dec 21st, 2004, 04:34 PM
#8
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
Join
Used to join a number of strings from an array seperated by a character (Dilemeter)
Use -
Code:
Join([Array Souce],[Dilemeter]) as string
Example
Code:
myarray(0) = "1"
myarray(1) = "2"
myarray(2) = "3"
myarray(3) = "4"
myarray(4) = "5"
newstring = Join(myarray, ",")
MsgBox newstring
newstring would now be "1,2,3,4,5"
-
Jan 19th, 2005, 09:36 AM
#9
Thread Starter
PowerPoster
Len
Len
Use -
[Expression] - Could be a varible, a textbox, "String of text", anything really
Example
VB Code:
Len(Text1.Text) 'Would return the length of text1.text
Len(myVarible) 'Would return the length of myVarible
Len("Hello") 'Would return the length of that string which is 5
Last edited by Pino; Jan 19th, 2005 at 11:10 AM.
-
Jan 19th, 2005, 11:16 AM
#10
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
LTrim\RTrim
LTrim Removes the spaces from the beginning of a string, (Left to right)
RTrim Removes the spaces from the end of a string, (Right to Left)
Use -
VB Code:
LTrim([String])
RTrim([String])
Example -
VB Code:
Msgbox LTrim(" Hello) 'Would remove the spaces at the start and display "Hello"
Msgbox RTrim("Hello ") 'Would do the same but remove the sapces from the end
-
Jan 19th, 2005, 11:23 AM
#11
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
Date And Time (3 Functions)
Date, Now, Time
Date - Returns the current date
Now - Returns the current date and time
Time - Returns the current time
There isnt a specific way to use these functions you can assign them to varibles display them in msgbox whatever 
Examples
VB Code:
Msgbox Date ' Would show current date according to system clock :)
Msgbox Time ' Would show current time according to system clock (Note it will be in the same format as the system clock)
Msgbox Now ' Would show the Date & time together
-
Feb 16th, 2005, 10:27 AM
#12
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
Replace
Use -
VB Code:
[i][b][string][/b][/i] = Replace([b][i][expresion][/i][/b],[b][i][find][/i][/b],[b][i][Replace With][/i][/b])
[string] - This will hold the result of our Replace() function[expresion] - This is the expresion we we be changing (a text box or label or "23.78.09" etc[find] - This is what we will be changing[Replace With] - and this is what we will be chnaging it to
Example -
Lets say we have a textbox (Named text1) and it contains (12.7.90.50) and for whatever reason we need to change the '.' (period) to a '/' (forward slash)
VB Code:
myString = Replace(Text1.Text, ".", "/") msgbox MyString
MyString would be 12/7/90/50,
Please note it would not change the contents of the textbox, to change the contents of the textBox you would need to do this.
VB Code:
text1.text = Replace(Text1.Text, ".", "/")
-
Feb 17th, 2005, 12:27 PM
#13
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
TypeName()
This returns the variable type of any given varible name could be usefull for debugging purpose.
Use -
VB Code:
[color=#00007f]String[/color] = [color=#00007f]TypeName[/color]([Variable Name])
[Variable Name] Would as it suggest be the name of the variable 
Example
So lets say you did,
VB Code:
Dim V1 As String
Dim V2 As Long
Dim V3 As Boolean
'etc
MsgBox TypeName(V1) ' Would Return string
MsgBox TypeName(V2) ' Would Return Long
MsgBox TypeName(V3) ' Would Return Boolean
-
Feb 17th, 2005, 12:36 PM
#14
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
IsNumeric()
This function returns true if the selected item is Numberic (Numbers) and false if it is not.
Use -
VB Code:
IsNumeric([Expression]) as boolean
[Expression] Would be the item you were looking to see if it was numeric.
Example -
VB Code:
If IsNumeric("78906") = True Then
MsgBox ("True")
End If
'The above statement would return the true msgbox
If IsNumeric("5566pp578") = True Then
MsgBox ("true")
End If
' This statement would not because the string we specified contains characters "pp"
So this function returns true if a specified expresion is numerical and flase if it is not
Note - It does also work for decimals so 0.8950 would return true 
Last edited by Pino; Feb 17th, 2005 at 12:39 PM.
-
Feb 18th, 2005, 05:14 AM
#15
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
CLng, CDbl, CInt, CBool, CDate, CStr
All these functions work in a similar way they all convert a specified expression to the respective type.
Use -
VB Code:
Clng([Expression]) as long
CDbl([Expression]) as Double
CInt([Expression]) as Integer
CBool([Expression]) as Boolean
CDate([Expression]) as Date
CStr([Expression]) as String
[Expression] being the thing we wish to convert, the functions explain themselves but i'll just go through what each does,
CLng - Converts a specified expression so it will fit into a long variable type
CDbl - Converts a specified expression so it will fit into a Double variable type
CInt - Converts a specified expression so it will fit into a Integer variable type
CBool - Converts a specified expression so it will fit into a Boolean variable type
CDate - Converts a specified expression so it will fit into a date variable type
CStr - Converts a specified expression so it will fit into a string variable type
Example
To use these functions is very simple, (myLong, myString etc are defined varibles as there name suggest so myString will be a string etc)
VB Code:
myLong = CLng(Text1.text)
'Text1 must contain digits
myDouble = CDbl(Text1.text)
'Text1 must contain digits
myInteger = CInt(Text1.text)
'Text1 must contain digits (Whole numbers no decimals)
myBoolean = CBool(Text1.text)
'Text1.text must contain True Or False or 0 or 1
myDate = CDate(text1.text)
'Text1 must contain a valid date ie (26/10/1926)
myString = CStr(Text1.text)
'You wouldnt need to do this since text1.text is allready a string :)
Note - trying to convert impossible expressions will result in a Type Mismatch Error ie
VB Code:
myBoolean = CBool("Ya Its Not Gonna Work")
-
Feb 18th, 2005, 01:11 PM
#16
Thread Starter
PowerPoster
Re: VB 6 - Useful Vb Functions
Val
The val function take the Value of a given epression.
Use
VB Code:
Val([Expression]) as Double
Expression is the thing we wish to take the value of
Example
VB Code:
'This would return the value of text1.text
Msgbox Val(Text1.Text)
If text1.text contained "456789" then thats what the msgbox would say, So whats the differnace between this and CDbl (Remember the val function is a double)
Well lets say in the textbox you have "99Chickens"
The code used above would now output "99"
But if the textbox contained "Chickens 99"
The code used above would output 0
So it will output all the leading numbers
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|