# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  VB 6 - Useful Vb Functions

## Pino

*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  :Smilie:  Thanks. 

  Click a link  to go directly to that function Post

*String Functions*


InStrLeft and RightMid  Split InStrRev Ucase And LCaseJoin LTrim and RTrimReplace *
    Conversion Functions*

CIntCLngCDateCStrCBoolCDbl*Dates And Time* 

DateTimeNow
*    Maths Functions* 
IsNumericVal *Other Functions
*  
TypeName
Click a link above to go directly to that function Post

----------


## Pino

*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]_

----------


## Pino

*Left And Right Functions*


*Left*


*Use *



```
[String2] = Left([String1],[Length])
```

 
  This will put the the number of characters specified from _[String1]_ (starting from the left) into _[String2]_ 


*Example*



```
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*



```
mystring = "abcdefghijk"
    newstring = Right(mystring, 4)
```

 _        newstring_ would now be hijk

----------


## Pino

*Mid* 


    The mid function is slightly different

* Use * 




```
 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*



```
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_

----------


## Pino

*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 -*



```
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* 



```
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 



```
newstring(0) = ab
     
        newstring(1) = cd
     
        newstring(2) = ef
     
        newstring(3) = gh
     
        newstring(4) = ijk
```

----------


## Pino

*InStrRev

 Use*



```
 result = InStrRev([StringCheck],[StringMatch])
```

 This function returns 1 if _[StringMatch]_ is contained in _[StringCheck]_
*
 Example*



```
mystring = "abcdefghijk"
 otherstring = "p"
 newstring = InStrRev(mystring, otherstring)
```

 newString here would return 0 because _[otherString]_ is not in _[Mystring]
_
 where as ...



```
mystring = "abcdefghijk"
  otherstring = "abc"
  newstring = InStrRev(mystring, otherstring)
```

 newString here would return 1 because _[otherString]_ is not in _[Mystring]
_

----------


## Pino

*UCase and LCase

*These functions make a chosen string UpperCase (Capital letters) or LowerCase (Small Letters)*

 UCase*

* Use -

* 

```
[NewString] = Ucase([String])
```

 *
  Example - 

* 

```
mystring = "abcdefghijk"
  otherstring = UCase(mystring)
```

 *[Otherstring]* would now be "ABCDEFGHIJK"

*LCase*

* Use -

* 

```
[NewString] = Lcase([String])
```

 * Example -* 

 

```
mystring = "ABCDEFGHIJK"
  otherstring = LCase(mystring)
```

 _[OtherString]_ would now be "abcdefghijk"

----------


## Pino

*Join*

Used to join a number of strings from an array seperated by a character (Dilemeter)

*Use -* 



```
Join([Array Souce],[Dilemeter]) as string
```

 *Example*



```
 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"

----------


## Pino

*Len*

*Use* -


VB Code:
Len([Expression])

*[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

----------


## Pino

*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

----------


## Pino

*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  :Smilie: 

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

----------


## Pino

*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, ".", "/")

----------


## Pino

*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  :Wink: 

*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

----------


## Pino

*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  :Smilie:

----------


## Pino

*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")

----------


## Pino

*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

----------

