GetOpenFilename takes to long to list directory contents


Posted by DavidH on February 11, 2002 3:27 PM

What is the method for speeding up the process of showing the user a directory listing of files on a network drive that has thousands of files in a directory?

I have tried to filter the files to only files that end with a *.nst and it still takes too long. What I want to try is to ask the user for the first six digits and then filter the listing on the first six digits but cannot figure out the syntax. Any help or other ideas will be appreciated.

Here is what I have tried...

Let UserFileName = Inputbox("Enter the first six digits of the filename")

I want to take the users filename and insert it into the following VBA as a filter for any files that start with whatever the user has entered. I am not sure if this will help my situation or not but I did not have any other ideas.

fileToOpen = Application _
.GetOpenFilename("Nest Files (*.nst), *.nst")
If fileToOpen <> False Then
MsgBox "Open " & fileToOpen
End If


Any help or point in the right direction will be appreciated.

Thanks,

David

Posted by Ivan F Moala on February 12, 2002 1:17 AM

David
The getopenfilename dosn't give you this option.
What you are going to have to do is revert to
the old cmdlg which can give the filer options
eg. AAA*.xls etc.
Try this to see if it works on your OS
For more info Repost

Option Explicit
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Sub GetFileNameFilterd_UsingOldCmdlg()
Dim FileToOpen As OPENFILENAME
Dim UserFileName As String

UserFileName = InputBox("Enter the first six digits of the filename")

FileToOpen.lStructSize = Len(FileToOpen)
'Select a filter
FileToOpen.lpstrFilter = "Xls Files (*.xls)" & Chr$(0) & UserFileName & "*.xls" & Chr$(0)
'create a buffer for the file
FileToOpen.lpstrFile = Space$(254)
'set the maximum length of a returned file
FileToOpen.nMaxFile = 255
'Create a buffer for the file title
FileToOpen.lpstrFileTitle = Space$(254)
'Set the maximum length of a returned file title
FileToOpen.nMaxFileTitle = 255
'Set your initial directory here
FileToOpen.lpstrInitialDir = "C:\"
'Set the title
FileToOpen.lpstrTitle = "Open File"
'No flags
FileToOpen.Flags = 0

'Show the 'Open File'-dialog
If GetOpenFileName(FileToOpen) Then
MsgBox "File to Open:= " & Trim(FileToOpen.lpstrFile)
Else
MsgBox "No file selected...user cancelled"
End If
End Sub

HTH


Ivan

Posted by DavidH on February 12, 2002 4:06 PM

Re:Thanks Ivan, your code works...I'll try it out on the network to see if this method is quicker (NoMsg)

FileToOpen.lStructSize = Len(FileToOpen) 'Select a filter FileToOpen.lpstrFilter = "Xls Files (*.xls)" & Chr$(0) & UserFileName & "*.xls" & Chr$(0) 'create a buffer for the file FileToOpen.lpstrFile = Space$(254) 'set the maximum length of a returned file FileToOpen.nMaxFile = 255 'Create a buffer for the file title FileToOpen.lpstrFileTitle = Space$(254) 'Set the maximum length of a returned file title FileToOpen.nMaxFileTitle = 255 'Set your initial directory here FileToOpen.lpstrInitialDir = "C:\" 'Set the title FileToOpen.lpstrTitle = "Open File" 'No flags FileToOpen.Flags = 0 If GetOpenFileName(FileToOpen) Then MsgBox "File to Open:= " & Trim(FileToOpen.lpstrFile) Else MsgBox "No file selected...user cancelled" End If



Posted by DavidH on February 13, 2002 4:01 PM

Ivan, code works but did not help speed up the process, Here is what I did to solve problem

FileToOpen.lStructSize = Len(FileToOpen) 'Select a filter FileToOpen.lpstrFilter = "Xls Files (*.xls)" & Chr$(0) & UserFileName & "*.xls" & Chr$(0) 'create a buffer for the file FileToOpen.lpstrFile = Space$(254) 'set the maximum length of a returned file FileToOpen.nMaxFile = 255 'Create a buffer for the file title FileToOpen.lpstrFileTitle = Space$(254) 'Set the maximum length of a returned file title FileToOpen.nMaxFileTitle = 255 'Set your initial directory here FileToOpen.lpstrInitialDir = "C:\" 'Set the title FileToOpen.lpstrTitle = "Open File" 'No flags FileToOpen.Flags = 0 : 'Show the 'Open File'-dialog If GetOpenFileName(FileToOpen) Then MsgBox "File to Open:= " & Trim(FileToOpen.lpstrFile) Else MsgBox "No file selected...user cancelled" End If

Ivan,

The code above allowed me to filter on whatever the user entered but did not improve the speed of listing the user's file. In case there are other users that have a similar problem, here is what I did to solve the problem:

Created a Userform with

1) a listbox to hold the search results
2) a text box so that users can change the path if they like
3) a text box for the user to enter the beginning of a filename to search for
4) a command button to start the search


The user enters the first six digits of the filename and clicks the command button...

The following code gets ran..

'Clear the contents of the search results listbox below:
If ListBox1.ListCount > 0 Then
For I = 1 To ListBox1.ListCount
ListBox1.RemoveItem (ListIndex)
Next I
End If
'Clear the contents of the search results listbox above:

'Get the path from the Path textbox below:
Let MyPath$ = PathBox.Value
'Get the path from the Path textbox above:


'Get the user's first six digits or whatever below:
UserFileName$ = TextBox1.Value
'Get the user's first six digits or whatever above:

'Use Dir command to fill the search listbox
If UserFileName$ <> "" Then
MyFile = Dir(MyPath$ + UserFileName$ + "*.nst")
If MyFile <> "" Then
ListBox1.AddItem (MyFile)
Else
MsgBox "Your part number was not found, please verify your part number"
End If

Do Until MyFile = ""
MyFile = Dir
ListBox1.AddItem (MyFile)
Loop
Else
MsgBox "You must type a part number in order to search for one"
End If

TextBox1.SetFocus

The search results will now be displayed in the listbox and when the user selects one of the files in the search results listbox the following code will run...

Let FileForProcessing$ = ListBox1.Text
MsgBox Dum$
'Do processing with the file here...

Ivan,

Thanks for your time and help. I hope this code may be useful for someone's application.

DavidH.