Results 1 to 16 of 16

Thread: VB 6 - Useful Vb Functions

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: VB 6 - Useful Vb Functions

    InStr


    Use -

    VB Code:
    1. 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:
    1. 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.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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”

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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”

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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”

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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]

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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"

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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"

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Question Len

    Len

    Use -


    VB Code:
    1. Len([Expression])

    [Expression] - Could be a varible, a textbox, "String of text", anything really

    Example

    VB Code:
    1. Len(Text1.Text) 'Would return the length of text1.text
    2.  
    3. Len(myVarible) 'Would return the length of myVarible
    4.  
    5. Len("Hello") 'Would return the length of that string which is 5
    Last edited by Pino; Jan 19th, 2005 at 11:10 AM.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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:
    1. LTrim([String])
    2. RTrim([String])

    Example -

    VB Code:
    1. Msgbox LTrim("       Hello) 'Would remove the spaces at the start and display "Hello"
    2. Msgbox RTrim("Hello         ") 'Would do the same but remove the sapces from the end

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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:
    1. Msgbox Date ' Would show current date according to system clock :)
    2. Msgbox Time ' Would show current time according to system clock (Note it will be in the same format as the system clock)
    3. Msgbox Now ' Would show the Date & time together

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: VB 6 - Useful Vb Functions

    Replace

    Use -

    VB Code:
    1. [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:
    1. myString = Replace(Text1.Text, ".", "/")
    2.  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:
    1. text1.text = Replace(Text1.Text, ".", "/")

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Question 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:
    1. [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:
    1. Dim V1 As String
    2. Dim V2 As Long
    3. Dim V3 As Boolean
    4. 'etc
    5.  
    6. MsgBox TypeName(V1) ' Would Return string
    7. MsgBox TypeName(V2) ' Would Return Long
    8. MsgBox TypeName(V3) ' Would Return Boolean

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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:
    1. IsNumeric([Expression]) as boolean

    [Expression] Would be the item you were looking to see if it was numeric.

    Example -

    VB Code:
    1. If IsNumeric("78906") = True Then
    2.     MsgBox ("True")
    3. End If
    4.  
    5. 'The above statement would return the true msgbox
    6.  
    7. If IsNumeric("5566pp578") = True Then
    8.       MsgBox ("true")
    9. End If
    10.  
    11. ' 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.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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:
    1. Clng([Expression]) as long
    2.  CDbl([Expression]) as Double
    3.  CInt([Expression]) as Integer
    4.  CBool([Expression]) as Boolean
    5.  CDate([Expression]) as Date
    6.  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:
    1. myLong = CLng(Text1.text)
    2.  'Text1 must contain digits
    3.  myDouble = CDbl(Text1.text)
    4.  'Text1 must contain digits
    5.  myInteger = CInt(Text1.text)
    6.  'Text1 must contain digits (Whole numbers no decimals)
    7.  myBoolean = CBool(Text1.text)
    8.  'Text1.text must contain True Or False or 0 or 1
    9.  myDate = CDate(text1.text)
    10.  'Text1 must contain a valid date ie (26/10/1926)
    11.  myString = CStr(Text1.text)
    12.  '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:
    1. myBoolean = CBool("Ya Its Not Gonna Work")

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: VB 6 - Useful Vb Functions

    Val

    The val function take the Value of a given epression.

    Use


    VB Code:
    1. Val([Expression]) as Double

    Expression is the thing we wish to take the value of

    Example


    VB Code:
    1. 'This would return the value of text1.text
    2.  
    3. 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
  •  



Click Here to Expand Forum to Full Width