Strange limit in explorer search window or somewhere else ... ??

I'm trying to fetch items from the Wondows explorer search window and use it in a Access form.

In the first try I fetched the items in a collection and put this collection into at listbox in the form - it kind of worked but some test I did showed tha a listbox has an upper limit around 350-360 items and then the code ran loose like a goose and Access crached - it worked nicely below that limit !

I did a thread on this problem: https://answers.microsoft.com/en-us/msoffice/forum/all/getting-path-filenames-from-explorer-window-to-a/bfa3e196-45aa-4235-9704-2b63b335854f

I now scrapped the collection and the listbox and puts each item from the Windows explorer search window into a table and then shows the records from this table on a form ... still there is a strange limit where Access chrashes ... but below the limit it's working nicely !

Here is my code:

Sub GetSelectedFilesInWinExplorers(sSearchTitle As String, tbl As String) '  As Collection
    Dim ExpWin          As SHDocVw.ShellWindows    'Object
    Dim CurrWin         As SHDocVw.InternetExplorer
    Dim ExplorerHwnd    As Long
    Dim i               As Long
    Set ExpWin = New SHDocVw.ShellWindows
    x = 0   ' tæller de fundne emner
    For Each CurrWin In ExpWin
        If Not CurrWin.Document Is Nothing Then
            ' I think it's wrong to use ...Title = sSearchTitle to choose the right window because
            ' when searching for frases the user have to fence the frase ... yeah, with " double qoutationmarks
            ' but it works for now ...
            If CurrWin.Document.folder.Title = sSearchTitle Then
                ExplorerHwnd = CurrWin.hwnd
                Call ShowWindow(ExplorerHwnd, SW_HIDE)    'Hide the windows since shell isn't hiding it
                '-------------------------------------------------------------------------------------------
                For i = 0 To CurrWin.Document.folder.Items.Count - 1
                    If i Mod 3 = 0 Then DoEvents
                    x = x + 1
                    strSQL = "INSERT INTO [" & tbl & "] (Nr, KortFilePath) SELECT '" & x & "', '" & _
                                       Mid(CurrWin.Document.folder.Items.Item(CLng(i)).Path, Len(Environ("SlægtHovedmappe")) + 1) & "';"
                    CurrentDb.Execute strSQL
                Next
                '--------------------------------------------------------------------------------------------
                Exit For
            End If
        End If
    Next CurrWin
    If Not CurrWin Is Nothing Then CurrWin.Quit
    Set ExpWin = Nothing
End Sub

The For-Next loop between the ------- is the problem !

I cant find the "limit" exactly but when the Windows explorer window holds 180 found files, it's

running nicely ... but 4358 found items the code crashes ... so the limit is somewhere in between.

Why is it a problem at all and how can I work around it ?

* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

The listbox is limited to about 4000 chars if you using "value" list (as you are).

You can use an array, and directly feed the combo/listbox from that, but then again, your idea of using a table is not all that bad.

I don't see why with a table that some "limit" would exist.

You could try code say like this: (use the built in file dialog), display the file explore, and then get the selected items.

eg this:

Sub GetMyFiles(tbl As String)


   Dim rst     As dao.Recordset

   

   Dim fDig    As FileDialog

   Set fDig = Application.FileDialog(msoFileDialogFilePicker)

   

   fDig.AllowMultiSelect = True

   fDig.Title = "Please select files"

   fDig.InitialFileName = "c:\test2"   ' optional start folder

   fDig.Show

   

   ' if user selected files, they are in SelectedItems.

   Dim strFile    As Variant     ' string - but variant due to "for/each"

   Dim x          As Integer

   

   If fDig.SelectedItems.Count > 0 Then

      Set rst = CurrentDb.OpenRecordset(tbl)

      x = 0

      For Each strFile In fDig.SelectedItems

         x = x + 1

         rst.AddNew

         rst!nr = x

         rst!KortFilePath = strFile

         rst.Update

      Next strFile

      rst.Close

   End If            

   

End Sub

So, above would eliminate the need for API code. 

But, to answer your question:

combo box/list box. If you filling it directly with a "value list", then yes, it is limited to about 4000 characters total.

You can get around this limitation in two ways:

Use a temp table (as you are doing).

Use a custom function to feed the contents of the combo box/list box. This approach is "rare" used in the Access community, and VERY few are aware of this 2nd choice even exists! It can be tricky, since I recall that the code runs EVEN before the forms on-open event.  However, it is a VERY cool option since you can map an array directly to the combo/listbox. (and thus eliminate the 4000 char limit). It also runs WAY faster, since you not behind the scenes loading up a huge 4000 char long string. (we all know that as stings get that long - they start to run very slow!!!).

How to do this it outline here:

http://access.mvps.org/access/forms/frm0049.htm

It is as noted that sample uses a call back - and it can be tricky to setup the first time. However this access feature quite much lets you map a array (even a multi-column one) directly to the listbox/combo, and all this extra work would thus eliminate your temp table. Hard call if this approach is worth  the extra effort, but this little cool and little known option is still a viable choice. But, it is VERY FAST and MUCH faster then those long value list approaches, and it also eliminates the 4000 char limit.

Also in above:

Using the filedialog requires a VBA reference to office to:

Microsoft Office 14.0 Object Library     (replace 14 - 2010 with your version)

And if you don't like nor want to add this additional reference in VBA? You can late bind the file dialog with this:

   Dim f    As Object 

   Set f = Application.FileDialog(3) 

   f.AllowMultiSelect = True 

   f.Show 

So, not clear if your still experiencing a limit - with your table approach you should be ok. the above use of the built in office/Access "file dialog" object is a better choice since you avoid API code and thus no changes required if you ever adopt access x64 bit version. (only a FYI to avoid windows API when possible).

So, you can dump the temp table and say feed the combo box with an array (or even custom collection), and not have the 4000 chars limit with the above combo/listbox interface that Acces has for listbox/combo.  The approach works well once the code is written, but since it is a call back approach (the code will and can be called over and over - even just passing the mouse over the listbox) can often case the code to run - even before any other form event on load.

Regards,

Albert D. Kallal (Access MVP 2003-2017)

Edmonton, Alberta Canada

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Thx for the quick and wide answer, but I think we talk a little beside each other ...

I have dropped the solution with the listbox and the collection and want to "pick" the found items/files from the Windows explorer window directly and put them into a table.

The Windows explorer window work well and CAN hold - let say 4.745 found files and they look to me allright ... it's very much posible that they includes the frase I'm looking for as content in the file - notice my searchtext is content IN the files NOT only in the filenames ... and it's working nicely in the Windows explorer search window.

The problem comes up when I - with the code I showed - have found the right window and want to fetch each of the found file names ... here something is causing Access to crash and I beleave it's in this part of the code somewhere:

Mid(CurrWin.Document.folder.Items.Item(CLng(i)).Path, Len(Environ("SlægtHovedmappe")) + 1)

speially this: Mid(CurrWin.Document.folder.Items.Item(CLng(i)).Path,

because this: Len(Environ("SlægtHovedmappe")) is just a length of the name of af main folder of mine.

EDIT:

Sometimes I wonder why a particular file is found - a picture = a JPG-file I scanned - my theory is that my name (Keld) is part of the path to that file because my computer is named KELDSHP - it's NOT very clever to tke such a file I think and that why the count of found files sometimes are in the 3-5000's !

How can I make a criteria to get rid of such findings ?

Best regards
KeldSor
Aarhus V, Denmark

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Ok, if the sending of the  files to a combo/list box is being dumped?

Well, then we are:

Stuck with working with that windows API,

or

Adopting the built in file dialog.


I would have to take a good guess that adopting the built in dialog going to work a lot better.

As to why working and using the windows API is causing some "pain"?

Well, not too much of a surprise, since working with windows API code is a challenge in the best of times, and when something fails or goes wrong? Well, now it going to be hard to track down that issue. So, memory leaks, memory management - perhaps even just the thumb-nail preview is causing havoc here (I don't know why the code is chocking here.).

Those are large file selects - might be some internal value that Access is not doing well with but I can't say.

I would consider trying  the filedialog. If that don't work, then your running out of options, and may well have to send the files to some temp table for the selecting process. 

All in all? My above advice is not all that great - but trying some alternative pathways to the solution is about the best card from the deck I can deal here.

Regards,

Albert D. Kallal (Access MVP 2003-2017)

Edmonton, Alberta Canada

 

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Maybe I don't understand buikding that "file dialog" - how can I set up criteria for THE CONTENT of the files I wont to show in that dialog.

I have tried doing some loops through my files ... but oh. it's much, MUCH too slow - I have 10.000's of files (mostly Word documents) in 100's of folders I have to search - Windows explorer seach window can do it very, very fast - that why !

In the first place people in here didn't reallize that it was THE CONTENT and some even did say that I could NOT search the centent of files.

I can't see how I can set up the criterias for the CONTENT of fiels in a file dialog.

Best regards
KeldSor
Aarhus V, Denmark

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi Keldsor, just on another part of your project.... do you know if it's possible to add the search criteria and run the search to an already existing instance of explorer i.e. without using the search criteria within shell("explorer.exe"....)?
If you feel a reply works for you, please kindly vote and mark it answered as it will be beneficial to other community members reading this thread

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

> S. A. Marshall

??? ha, ha, I'm sure you know more about this than I do ... I struggle Big Time just to get this to work ;-))

Now I've tried to use a table instead of the combination of a collection and a listbox - and I still have the problem crashing Access.

Sorry, I can't contribute there ;-((

Best regards
KeldSor
Aarhus V, Denmark

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I'm stumped. I was looking at your code.... how you select the correct window etc.

I've worked out how to open a new instance of explorer with separate PID, set focus, kill it etc. but I just can't work out how to combine the search string with the switch.

vPID = Shell("""" & strProgramName & """ """ & strArgument & """")

StrProgramName ="explorer.exe"

strArgument = "/separate"

the search string I'm using is "search-ms:query=*.pdf&crumb=location:c:\" (which I think is correct)

but I can't concatenate it all together.

should be something like:

vPID = Shell("""" & strProgramName & """ """ & strArgument & """" & "search-ms:query=*.pdf&crumb=location:c:\")

but that isn't correct... neither is any of the other combinations I've tried.

If you feel a reply works for you, please kindly vote and mark it answered as it will be beneficial to other community members reading this thread

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I'm stumped. I was looking at your code.... how you select the correct window etc.

I've worked out how to open a new instance of explorer with separate PID, set focus, kill it etc. but I just can't work out how to combine the search string with the switch.

vPID = Shell("""" & strProgramName & """ """ & strArgument & """")

StrProgramName ="explorer.exe"

strArgument = "/separate"

the search string I'm using is "search-ms:query=*.pdf&crumb=location:c:\" (which I think is correct)

but I can't concatenate it all together.

should be something like:

vPID = Shell("""" & strProgramName & """ """ & strArgument & """" & "search-ms:query=*.pdf&crumb=location:c:\")

but that isn't correct... neither is any of the other combinations I've tried.

It's exactly the problems I have when trying to get rid of all Path's in the found items ... I can't figure out the right combination of double qoutation marks.
When searching for phrases it gets even worse because you have to USE the quotation marks to fence the phrase you search for inside the files.

Arghhh, ... Gabriel, blow our horn !

Best regards
KeldSor
Aarhus V, Denmark

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

hopefully I'll work it out somehow. Would be a better way to do it I think
If you feel a reply works for you, please kindly vote and mark it answered as it will be beneficial to other community members reading this thread

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Just review the sample i had supplied you previously, it created the search string and was fully functional.
--
Daniel Pineault
Microsoft MVP 2010-2021

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

 
 

Question Info


Last updated October 5, 2021 Views 308 Applies to: