Results 1 to 13 of 13

Thread: [RESOLVED] Check Textbox if directory type or not

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Resolved [RESOLVED] Check Textbox if directory type or not

    I have textbox..This textbox is use for path location such as "C:\", "D:\" or C:\database .How to pop up the messagebox if the textbox is not like a path name location for example a string such "fdsad". That mean the textbox doen't have :\

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Check Textbox if directory type or not

    You can sue Dir function and LostFocus/Validate event handlers (or some other procedure):
    Code:
    'USE THIS
    Private Sub Text1_LostFocus()
        If Dir(Trim(Text1.Text)) = "" Then
            MsgBox "Invalid path!", vbExclamation, "Invalid Path"
            Text1.SelStart = 0
            Text1.SelLength = Len(Text1.Text)
            Text1.SetFocus
        End If
    End Sub
    
    'OR THIS INSTEAD
    Private Sub Text1_Validate(Cancel As Boolean)
        If Dir(Trim(Text1.Text)) = "" Then
            MsgBox "Invalid path!", vbExclamation, "Invalid Path"
            Cancel = True
            Text1.SelStart = 0
            Text1.SelLength = Len(Text1.Text)
            Text1.SetFocus
        End If
    End Sub

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Check Textbox if directory type or not

    Code:
    Private Sub Command1_Click()
        If Len(Dir(Text1.Text, vbDirectory)) Then
            If (GetAttr(Text1.Text) And vbDirectory) = vbDirectory Then
              MsgBox Text1.Text & " is a FOLDER"
            End If
        Else
              MsgBox Text1.Text & " is not a FOLDER "
        End If
    End Sub

  4. #4
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Check Textbox if directory type or not

    Code:
    Private Sub Command1_Click()
        If IsFolder("C:\my folder") Then
            MsgBox "a valid folder"
        Else
            MsgBox "not a valid folder"
        End If
    End Sub
    
    Public Function IsFolder(PathName As String) As Boolean
        On Error Resume Next
        If Right$(PathName, 1) <> "\" Then PathName = PathName & "\"
        IsFolder = CBool(LenB(Dir$(PathName, 18)))
    End Function

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Check Textbox if directory type or not

    Quote Originally Posted by Hack
    Code:
    Private Sub Command1_Click()
        If Len(Dir(Text1.Text, vbDirectory)) Then
            If (GetAttr(Text1.Text) And vbDirectory) = vbDirectory Then
              MsgBox Text1.Text & " is a FOLDER"
            End If
        Else
              MsgBox Text1.Text & " is not a FOLDER "
        End If
    End Sub
    Hi hack, When I try type C: .. it pop up that C: is a folder... I think is should not a valid folder.. C:\ should be valid

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Check Textbox if directory type or not

    That is because the root of a drive satisfys the attribute conditions of a folder.

    If (GetAttr(Text1.Text) And vbDirectory) = vbDirectory is true if C: or H: or someother single letter followed by a colon is in text1.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Check Textbox if directory type or not

    Quote Originally Posted by Chris001
    Code:
    Private Sub Command1_Click()
        If IsFolder("C:\my folder") Then
            MsgBox "a valid folder"
        Else
            MsgBox "not a valid folder"
        End If
    End Sub
    
    Public Function IsFolder(PathName As String) As Boolean
        On Error Resume Next
        If Right$(PathName, 1) <> "\" Then PathName = PathName & "\"
        IsFolder = CBool(LenB(Dir$(PathName, 18)))
    End Function
    When I used your code It type

    C:
    D:\
    C:\Database

    The resuld it
    C: is valid folder... it should C:\ is valid folder..(Uncorrect)

    D:\ Is valid folder... yes this correct (correct)

    C:\Database yes this valid valid folder (correct)

    For C:\Database <--- this ok

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Check Textbox if directory type or not

    Did you try one of RhinoBull's suggestions?

  9. #9
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Check Textbox if directory type or not

    Quote Originally Posted by matrik02
    When I used your code It type

    C:
    D:\
    C:\Database

    The resuld it
    C: is valid folder... it should C:\ is valid folder..(Uncorrect)

    D:\ Is valid folder... yes this correct (correct)

    C:\Database yes this valid valid folder (correct)

    For C:\Database <--- this ok

    It says that "C:" is a valid folder, because it adds a backslash "\" to the end.

    Add If Len(PathName) <= 2 Then Exit Function after 'On Error Resume Next'.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Check Textbox if directory type or not

    Quote Originally Posted by Hack
    Did you try one of RhinoBull's suggestions?
    Same it said C: <---is valid is should be C:\ or any letter with :\ should be valid

    C:\Database <--valid (correct)
    C:\ <---Valid (correct)
    C: valid .This type should pop that C: is not valid because doen't hve :\

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Check Textbox if directory type or not

    Quote Originally Posted by Chris001
    It says that "C:" is a valid folder, because it adds a backslash "\" to the end.

    Add If Len(PathName) <= 2 Then Exit Function after 'On Error Resume Next'.
    You right !

  12. #12
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Check Textbox if directory type or not

    C: is a valid directory - backslash isn't necessary when you validating directory only.
    After directory was validated (and result is positive) you may check whether or not it ends with the backslash (perhaps using Right function).

  13. #13
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] Check Textbox if directory type or not

    Quote Originally Posted by matrik02
    I have textbox..This textbox is use for path location such as "C:\", "D:\" or C:\database .How to pop up the messagebox if the textbox is not like a path name location for example a string such "fdsad". That mean the textbox doen't have :\
    A literal interpretation of this question would require a solution that validates a string that is "like a path name" even if the named path doesn't exist.

    Since you've already marked this resolved, I'm going to guess that isn't what you meant. (All the solutions provided return False if the text is a valid folder name but the folder doesn't exist.) But just in case, there is an API call to do that:

    PathIsFileSpec: Test to Determine if String Meets Valid File Spec

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