Select file

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Select file

Post by D Willett »

The following code from my VB6 project locates pdf files with the word "Meth". I also want it to locate pdf with the word "JC".
I'm not sure if I should use "and" - "or" in the line, which is the best way?

strFilePDF = Dir(strFolderPDF & "\" & strPDF5 & "*Meth*.pdf")

------------------------------------------------------------------------------------------------------------------------------------------------------

' The full procedure is below:

Private Function FetchPDF()
Dim i As Integer
Dim strFilePDF As String
Dim strErrorPDF As String

PDF1.Visible = True
Dim strPDF5 As String
On Error GoTo ErrHandler

' Get Left5
strPDF5 = Left(Me.txtEst, 8)
strFolderPDF = "L:\MMPDF\ConsoleFiles\" & Me.txtEst & "\"
Erase arrFileNamesPDF
i = 0
' Get PDF files in strFolder whose names begin with strPDF5
strFilePDF = Dir(strFolderPDF & "\" & strPDF5 & "*Meth*.pdf")
strErrorPDF = "L:\MMPDF\Utilities\PDFViewer-WS.pdf"
' Loop through files
Do While strFilePDF <> "" And i < 127
Me.lstArch.AddItem strFilePDF
i = i + 1
arrFileNamesPDF(i) = strFilePDF
strFilePDF = Dir

Loop
intFileCountPDF = i
If intFileCountPDF < 1 Then
intNumberOfPDF = 0
Me.AcrobatPath = strErrorPDF
Me.PDF1.LoadFile Me.AcrobatPath
Exit Function
Else
intNumberOfPDF = 1
End If
FillPDFS 1
Exit Function

ErrHandler:
MsgBox Err.Description, vbCritical, "Error"
End Function
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78478
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Select file

Post by HansV »

You can specify only one file pattern at a time to the Dir function, you can't let it search for "Meth or JC" or something like that.

You could add an argument to the function to specify the text you're looking for:

Code: Select all

Private Function FetchPDF(strText As String)
    ...
    ' Get PDF files in strFolder whose names begin with strPDF5
    strFilePDF = Dir(strFolderPDF & "\" & strPDF5 & "*" & strText & "*.pdf")
    ...
End Function
You can then call the function once with each search string:

Code: Select all

    Call FetchPDF("Meth")
    Call FetchPDF("JC")
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Select file

Post by D Willett »

Cheers Hans

Does strText need a dim reference and where do I put the call statements ?
( sorry if I'm being a bit thick lol )
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78478
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Select file

Post by HansV »

strText is declared as a string in the definition of FetchPDF, there's no need to declare it elsewhere.

Where and how do you use the original version of the FetchPDF function?
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Select file

Post by D Willett »

Sorry Hans I never saw the definition.....

FetchPDF is used in the following procedure. I just need to know where to put the Call procedures.


Public Sub cmdList_Click()
WriteLog GetComputerName & " " & GetNetUser & _
" Estimate No " & Me.txtEst.Text & " Was Viewed"
Dim z As Integer
Dim fso As Object

Set fso = CreateObject("scripting.FileSystemObject")
Me.lstArch.Clear
Me.lstFTP.Clear
imgDel
Me.txtInfo.Text = 0
Me.txtImgCount.Text = 0
Me.txtInfoPDF.Text = 0
Me.txtTrans.Text = 0

For z = 1 To intNumberOfImages
Me.Controls("bor" & z).Visible = False
Next

FolderExists
TransferExists

If Me.txtEst = "" Or Me.txtEst = "*" Then
MsgBox "You Must Enter A Reference No.", vbInformation, "Information"
Me.AcrobatPath = "L:\MMPDF\Utilities\PDFViewer-WS.pdf"
Me.PDF1.LoadFile Me.AcrobatPath
imgDel
'Me.lstImages.Clear
Exit Sub
End If

Dim i As Integer
Dim strFile As String
' strLeft5 to find left most 5 characters
Dim strLeft5 As String
' Fixed folder
strFolder = "L:\MMPDF\ConsoleFiles\" & Me.txtEst & "\"

FetchPDF
' Get Left5
strLeft5 = Left(Me.txtEst, 8)
'strLeft5 = Me.txtEst

i = 0
' Get jpg files in strFolder whose names begin with strLeft5
strFile = Dir(strFolder & strLeft5 & "*.jpg")
'Me.lstArch.Clear

' Loop through files
Do While strFile <> "" And i < 127

'Fill lstArch with all files beginning with estimate number
Me.lstArch.AddItem strFile
i = i + 1
arrFileNames(i, 1) = strFile
arrFileNames(i, 2) = Format(fso.GetFile(strFolder & "\" & strFile).DateLastModified, "dd/mm/yy") & _
" - " & vbCrLf & "Added To Console On : " & Format(fso.GetFile(strFolder & "\" & strFile).DateLastModified, "dd/mm/yy")
strFile = Dir
Loop
intFileCount = i
If Me.txtTrans > 0 Then MsgBox "There are " & Me.txtTrans.Text & " files to be transferred", vbInformation, "Management Information Centre"
If intFileCount = 0 Then
Exit Sub
End If

If intFileCount < MaxNumberOfImages Then
intNumberOfImages = intFileCount
Else
intNumberOfImages = MaxNumberOfImages
End If

If intNumberOfImages > 0 Then
FillImages 1
End If
Set fso = Nothing

End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78478
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Select file

Post by HansV »

You call the FetchPDF function about halfway through the cmdList_Click procedure:

...
strFolder = "L:\MMPDF\ConsoleFiles\" & Me.txtEst & "\"

FetchPDF
' Get Left5
...

Replace that line with the two lines that I posted, so that it looks like this:

...strFolder = "L:\MMPDF\ConsoleFiles\" & Me.txtEst & "\"

Call FetchPDF("Meth")
Call FetchPDF("JC")

' Get Left5
...
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Select file

Post by D Willett »

Cheers Hans, I'll have a look shortly.
Have a great weekend.
Cheers ...

Dave.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Select file

Post by D Willett »

Hi Hans

The code works good but picking out files twice, something perhaps in the loop process.
Any idea's ?
You do not have the required permissions to view the files attached to this post.
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78478
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Select file

Post by HansV »

You have files whose name contains both "METH" and "JC", so they are selected twice - first by FetchPDF("METH"), then by FetchPDF("JC").
To change this would require substantial rewriting of the code.

By the way, you fill Me.lstArch with .pdf files in FetchPDF, and with .jpg files in cmdList_Click. Is that correct?
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Select file

Post by D Willett »

I just nipped out for a ciggie and realised the naming of the files caused the duplication.. you beat me to it.
The files with METHJC or JCMETH were a quick fix over the last couple of days.

Yes lstArch shows the jpg's too because the list has another function which needs them. They don't interfere with the usability so I prefer to leave them in.

Many regards once again Hans
Cheers ...

Dave.