Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check to see if network path exists 1

Status
Not open for further replies.

ScorpioX

MIS
Apr 30, 2003
63
US
Hello,
Sorry I have been searching and can not seem to find what it is I need. I think I have come close but seem to get errors everytime. I am looking to check and see if a network path exists. If it does I want to populate a box. If not, show a message and end the sub. Below is what I have now, but keep getting a bad file name or number error. Any help is greatly appreciated.

Private Sub Form_Load()
Label2.Visible = False
PopulateListControlFromFile List3, "C:\Windows\Path.txt" 'populates list box 3 with the network path to search for IE. \\Cluster1
PopulateListControlFromFile List2, "C:\Appendum.txt" ' populates list box 2 with the information from C:\Appendum.txt
If Dir(List3, vbDirectory) <> "" Then
PopulateListControlFromFile List1, List2 ' populates the list box 1 with the text from List box 2
Else
Label2.Visible = True ' Shows the no connection mecssage
End If
End Sub

Thanks,
Scorpiox
 
Where does your code error?

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
sorry that would probably help. It errors at the If statement on List3. Keeps coming back as bad file name or number. I can get it to work if I put a path in there but I need to use a variable for it and I can not seem to get it right.

Thanks,
Scorpiox
 
What's the value of List3 when it errors passing it to the Dir function (in my experience it's quite hard to get the Dir() function to error out though)?

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Sorry, ignore that. I missed that you were working with the server root [banghead]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
That was what I thought also. Never tried it using a network path though. The value is "\\Cluster1\" no quotes though.

Scorpiox
 
I'd think it's to do with shared folders, could you not specify a folder on the server to check?

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I tried that as well. I used "\\Cluster1\ComputerImages\" but it keeps erroring out with bad file name or number.

ScorpioX
 
The error only happens when the path is not valid. Do I need some kind of boolean or false statement other then Else maybe?

Scorpiox
 
HarleyQuinn,
Thank you for your time and your posts. I did get it to work though. I added an On error statement and now it works like a charm. Not sure if it is the right way but it works. Below is the changes incase your interested.

Private Sub Form_Load()
On Error GoTo NoPath_Error
Label2.Visible = False
PopulateListControlFromFile List3, "C:\Windows\Path.txt" 'populates list box 3 with the network path to search for IE. \\Cluster1
PopulateListControlFromFile List2, "C:\Appendum.txt" ' populates list box 2 with the information from C:\Appendum.txt
If Dir$(List3, vbDirectory) <> vbNullString Then
PopulateListControlFromFile List1, List2 ' populates the list box 1 with the text from List box 2
Exit Sub
NoPath_Error:
PopulateListControlFromFile List2, "C:\Windows\NC.txt"
PopulateListControlFromFile List1, List2
Label2.Visible = True ' Shows the no connection mecssage
Else
PopulateListControlFromFile List2, "C:\Windows\NC.txt"
PopulateListControlFromFile List1, List2
Label2.Visible = True ' Shows the no connection mecssage
End If
End Sub


Thanks again,
Scorpiox
 
If you're going to use the error handling route I'd make it a bit more robust (and readable, it's generally to have the error handling routine seperate from the general code i.e outside oof your IF statement), using something like:
Code:
Private Sub Form_Load()
On Error GoTo NoPath_Error
Label2.Visible = False
    PopulateListControlFromFile List3, "C:\Windows\Path.txt" 'populates list box 3 with the network path to search for IE. \\Cluster1
    PopulateListControlFromFile List2, "C:\Appendum.txt" ' populates list box 2 with the information from C:\Appendum.txt
If Dir$(List3, vbDirectory) <> vbNullString Then
   PopulateListControlFromFile List1, List2 ' populates the list box 1 with the text from List box 2
Else
    PopulateListControlFromFile List2, "C:\Windows\NC.txt"
    PopulateListControlFromFile List1, List2
    Label2.Visible = True ' Shows the no connection mecssage
End If

Exit Sub
NoPath_Error:
If Err.Number = 52 Then
    PopulateListControlFromFile List2, "C:\Windows\NC.txt"
    PopulateListControlFromFile List1, List2
    Label2.Visible = True ' Shows the no connection mecssage
Else ' some other error
    'normal error handling e.g.
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of Form Form1", vbCritical
End If

End Sub
Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Thank you that worked like a charm as well. I will use that instead as it is written better.

Thanks, again,
Scorpiox
 
I'm reading this thread with interest but what is a PopulateListControlFromFile ?
Is there some other code you dont show?
 
PopulateListControlFromFile is a function used to populate a List box or Check box on your form. With the data from a text file.
1)Create a form.
2)Create 3 list boxes on the form named List1 List2, List3.
3)Create a TxtArray on the form, named TxtArray.
4)Create 2 command buttons, one named Remove, the other named Close.
5)Create the files shown in the code and put them where they point to.
5a)Remove2.bat is just a batch file that tells it to delete the link from the startup folder in All Users.
5b)Path.txt contains the path to check and see if the network exists.
5c)NC.txt contains the message to populate the Array with if there is no connection.
5c)Addendum.txt contains the path and file to get the information for the Array if there is a network connection.
5d) The file within the addendum.txt is stored somewhere on the network.
6) Create a Label on the form, name it Label2.
7) Do everything above and then paste the entire code below in to the form and you can test it for yourself.

It is pretty cool if you ask me. Probably better or neater ways out there but this works for what I need.

The idea is that this form is stored in the image. Then when it is sent to a machine and the machine boots up. It will popup and display any changes that need to be done to the PC. the idea of storing the one file on the network. Is it can be changed at anytime and the image does not actually have to be changed.
Code:
Public Function PopulateListControlFromFile( _
  ListControl As Object, FullPath As String, _
  Optional Delimiter As String = vbCrLf) _
  As Boolean

'*******************************************
'Purpose: Populate a list control (e.g.,
'list box, combo box) with the contents of a text file

'Parameters:
'           ListControl:    The control to populate
'           FullPath:       The FullPath of the file to
'                           read from
'           Delmiter:       (Optional) Delimiter used to
'                           split file contents into items;
'                           defaults to vbCrlf (i.e., each
'                           line in file is an item)
'
'Returns:   True if sucessfule, false otherwise
'Requires:  Reference to Microsoft Scripting Runtime
'Example:   PopulateListControlFromFile Combo1, _
'               "C:\MyTextFile.txt"
'***************************************************
Dim objFso As New Scripting.FileSystemObject

Dim objTextStream As Scripting.TextStream
Dim strContents As String
Dim arrContents() As String
Dim lCtr As Long, lCount As Long

On Error GoTo ErrHandler:

'Ensure file exists
If Dir(FullPath) = "" Then Exit Function

'Clear List Control.
ListControl.Clear

'Get file contents
Set objTextStream = objFso.OpenTextFile(FullPath, ForReading)
strContents = objTextStream.ReadAll
objTextStream.Close

'Split file contents based on delimiter
arrContents = Split(strContents, Delimiter)
lCount = UBound(arrContents)

'Populate list control
For lCtr = 0 To lCount
  ListControl.AddItem arrContents(lCtr)
Next
Dim i As Integer
TxtArray(0).Text = arrContents(0)
For i = 1 To lCount
    Load TxtArray(i)
    With TxtArray(i)
      .Text = arrContents(i)
      .Visible = True
      .Top = TxtArray(i - 1).Top + 550
    End With
Next
'Select first item (Optional, comment out
'if not desired
If ListControl.ListCount > 0 Then
    ListControl.ListIndex = 0
End If
PopulateListControlFromFile = True

ErrHandler:
Set objTextStream = Nothing
Set objFso = Nothing
End Function

Private Sub Close_Click()
End
End Sub

Private Sub Form_Load()
On Error GoTo NoPath_Error
Label2.Visible = False 'basic label containing a message that says there is no connection to the network
    PopulateListControlFromFile List3, "C:\Windows\Path.txt" 'populates list box 3 with the network path to search for IE. \\Cluster1
If Dir$(List3, vbDirectory) <> vbNullString And Dir$("C:\Windows\Addendum.txt", vbDirectory) <> vbNullString Then
    PopulateListControlFromFile List2, "C:\Windows\Addendum.txt" ' populates list box 2 with the information from C:\Appendum.txt
    PopulateListControlFromFile List1, List2 ' populates the list box 1 with the text from List box 2
Else
    PopulateListControlFromFile List2, "C:\Windows\NC.txt"
    PopulateListControlFromFile List1, List2
    Label2.Visible = True ' Shows the no connection mecssage
End If

Exit Sub
NoPath_Error:
If Err.Number = 52 Then
    PopulateListControlFromFile List2, "C:\Windows\NC.txt"
    PopulateListControlFromFile List1, List2
    Label2.Visible = True ' Shows the no connection mecssage
Else ' some other error
    'normal error handling e.g.
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of Form Form1", vbCritical
End If

End Sub
Private Sub Remove_Click()
On Error GoTo ErrHndlr
Shell ("C:\Windows\System32\Remove2.bat")
MsgBox " File has been removed. This form will no longer Autostart!"
Exit Sub
ErrHndlr:
    MsgBox "File is not in the Startup folder. So I can not remove it!"
    Err.Clear
End Sub

Hope this help explain it!
Scorpiox
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top