Results 1 to 11 of 11

Thread: InStr and Mid crap [resolved]

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    InStr and Mid crap [resolved]

    ok i was told how to get just part of a line from a text file using InStr and Mid, and ive looked all over for how to do that, because i have no idea how to use InStr and Mid at all. i see things like
    Code:
    InStr(strCheck, Mid(sInvalidChars, i, 1)
    but i have no idea why its setup like that, or what it means (ill post the entire script of the program dealy that had it inside). thats the closest thing to instructions on what InStr and Mid are that ive seen


    Code:
    Dim bCK As Boolean
    Dim strDomainType As String
    Dim strDomainName As String
    Const sInvalidChars As String = "!#$%^&*()=+{}[]|\;:'/?>,< "
    Dim i As Integer
    
    bCK = Not InStr(1, strCheck, Chr(34)) > 0 'Check to see if there is a double quote
    If Not bCK Then GoTo ExitFunction
    
    bCK = Not InStr(1, strCheck, "..") > 0 'Check to see if there are consecutive dots
    If Not bCK Then GoTo ExitFunction
    
    ' Check for invalid characters.
    If Len(strCheck) > Len(sInvalidChars) Then
        For i = 1 To Len(sInvalidChars)
            If InStr(strCheck, Mid(sInvalidChars, i, 1)) > 0 Then
                bCK = False
                GoTo ExitFunction
            End If
        Next
    Else
        For i = 1 To Len(strCheck)
            If InStr(sInvalidChars, Mid(strCheck, i, 1)) > 0 Then
                bCK = False
                GoTo ExitFunction
            End If
        Next
    End If
    
    If InStr(1, strCheck, "@") > 1 Then 'Check for an @ symbol
        bCK = Len(Left(strCheck, InStr(1, strCheck, "@") - 1)) > 0
    Else
        bCK = False
    End If
    If Not bCK Then GoTo ExitFunction
    
    strCheck = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "@"))
    bCK = Not InStr(1, strCheck, "@") > 0 'Check to see if there are too many @'s
    If Not bCK Then GoTo ExitFunction
    
    strDomainType = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "."))
    bCK = Len(strDomainType) > 0 And InStr(1, strCheck, ".") < Len(strCheck)
    If Not bCK Then GoTo ExitFunction
    
    strCheck = Left(strCheck, Len(strCheck) - Len(strDomainType) - 1)
    Do Until InStr(1, strCheck, ".") <= 1
        If Len(strCheck) >= InStr(1, strCheck, ".") Then
            strCheck = Left(strCheck, Len(strCheck) - (InStr(1, strCheck, ".") - 1))
        Else
            bCK = False
            GoTo ExitFunction
        End If
    Loop
    If strCheck = "." Or Len(strCheck) = 0 Then bCK = False
    
    ExitFunction:
    ValidEmail = bCK
    End Function
    PS: i dont have any help files oO
    go figure...

    PS: thats not my code...
    Last edited by R.a.B.B.i.T; Jul 15th, 2003 at 09:33 PM.

  2. #2

  3. #3
    Hyperactive Member Stiletto's Avatar
    Join Date
    Aug 2002
    Location
    Jerusalem, Israel
    Posts
    287
    Syntex of InStr is
    (Start, Original String, String to be found)
    This function will tell you where the String to be found is located in the Original String.
    For example:
    VB Code:
    1. Msgbox InStr(1,"Hey You","You")
    This code will show you 5, cuz the string "You" is starting in the 5th place of the original string.

  4. #4
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    This appears to be your problem:

    VB Code:
    1. Const sInvalidChars As String = "!#$%^&*()=+{}[]|\;:'/?>,< "

    If you define your constant like this, the program will only do what you want when it encounters all those characters in that specific sequence. I'm not sure how to declare a multiple-setting constant, but I do know that a delimiter is used.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255
    PS: i dont have any help files oO
    go figure...
    oooooo ok so thats how it works.
    and now 1 more question:
    how would i save "Hey" to a variable?

  6. #6
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    You could do something like this:

    VB Code:
    1. VariableName = Left$("Hey You", 3)

    That will return the first three characters from the left end of the string. In this case, it would return "Hey."

    Now if I did something like this:

    VB Code:
    1. VariableName = Right$("Hey You", 5)

    That would return the first 5 characters from the right end of the string. This would return "y You." Mid$ has nothing to do with what you're trying to accomplish, although this would achieve the same thing as my Right$ example:

    VB Code:
    1. VariableName = Mid$("Hey You", 3)

    It would skip to the 3rd position in the string, and return everything from that point. You would need to do a reverse Mid$ to use that function in this case, and I don't think there is a reverse Mid$.

    I see you pretty much have InStr down pat though.

    Get it now?
    Last edited by hothead; Jul 14th, 2003 at 02:27 AM.

  7. #7
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    BTW Stiletto, you can also use InStr to check for specific characters. I do it all the time. Something like this would check for ('s and return a message box if one was found:

    VB Code:
    1. If InStr(1, strCheck, "(" Then
    2.     MsgBox "There is a ( in the string."
    3. End If

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255
    nice....thx 4 the help, i finally understand WOOTTTTTTTT

  9. #9
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    It's not a problem. These functions are quite intimidating at first, but once you get the hang of them, you'll find they're actually quite easy.

  10. #10
    Lively Member
    Join Date
    Apr 2003
    Location
    Georgetown, Texas
    Posts
    114
    Hothead, and all of you out there who think something "is easy". After all these year, I've finally come up with "THE ABSOLUTE TRUTH". It is:

    BEFORE YOU KNOW HOW TO DO SOMETHING, "IT'S IMPOSSIBLE"; ONCE YOU LEARN HOW TO DO IT, "IT'S SO EASY"

  11. #11
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    That's my point. Re-read my post.

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