|
-
Jun 17th, 2008, 01:08 PM
#1
Thread Starter
Frenzied Member
[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 :\
-
Jun 17th, 2008, 01:17 PM
#2
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
-
Jun 17th, 2008, 01:19 PM
#3
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
-
Jun 17th, 2008, 01:22 PM
#4
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
-
Jun 17th, 2008, 01:30 PM
#5
Thread Starter
Frenzied Member
Re: Check Textbox if directory type or not
 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
-
Jun 17th, 2008, 01:34 PM
#6
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.
-
Jun 17th, 2008, 01:35 PM
#7
Thread Starter
Frenzied Member
Re: Check Textbox if directory type or not
 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
-
Jun 17th, 2008, 01:38 PM
#8
Re: Check Textbox if directory type or not
Did you try one of RhinoBull's suggestions?
-
Jun 17th, 2008, 01:39 PM
#9
Re: Check Textbox if directory type or not
 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'.
-
Jun 17th, 2008, 01:43 PM
#10
Thread Starter
Frenzied Member
Re: Check Textbox if directory type or not
 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 :\
-
Jun 17th, 2008, 01:50 PM
#11
Thread Starter
Frenzied Member
Re: Check Textbox if directory type or not
 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 !
-
Jun 17th, 2008, 01:51 PM
#12
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).
-
Jun 17th, 2008, 03:05 PM
#13
Re: [RESOLVED] Check Textbox if directory type or not
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|