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 :\
Printable View
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 :\
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
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
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
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 validQuote:
Originally Posted by Hack
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.
When I used your code It typeQuote:
Originally Posted by Chris001
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
Did you try one of RhinoBull's suggestions?
Quote:
Originally Posted by matrik02
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'.
Same it said C: <---is valid is should be C:\ or any letter with :\ should be validQuote:
Originally Posted by Hack
C:\Database <--valid (correct)
C:\ <---Valid (correct)
C: valid .This type should pop that C: is not valid because doen't hve :\
You right !:DQuote:
Originally Posted by Chris001
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).
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.Quote:
Originally Posted by matrik02
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