Delete Files Based On a Text Field

Learn2010

Registered User.
Local time
Today, 06:06
Joined
Sep 15, 2010
Messages
415
I have a database and use part of it for billing. I create a file and upload it to a site. The structure of the file is M123456.1. The next file generated would be M123456.2, and so on. In order to keep the folder cleaned out and make it easy to identify the file to upload, I get rid of similar files in the folder with the following line of code:
If Dir("C:\Folder1\Folder2\M*.*") <> "" Then Kill "C:\Folder1\Folder2\M*.*"
I would like to use this for something similar. Instead of using the M123456 as the file name to delete, I would like to use a field name on a subform from where this code is activated. This field is used to identify customers. For example, if the Customer ID is 123456, I would like to delete all files that start with that number. I can’t get it to work.
If Dir("C:\Folder1\Folder2\CustomerID") <> "" Then Kill "C:\Folder1\Folder2\CustomerID"
Any ideas?
Thank you.
 
Try something like:

Dim YourStringVar As String

YourStringVar = C:\Folder1\Folder2" & CustomerID

If Dir( YourStringVar ) <> "" Then Kill YourStringVar

Warning: Untested
 
Create a string variable called strCustID
It doesn't matter if your customer id field is a number as its going to be used as art of a string.

When the button is clicked add code similar to this

Code:
strCustID = Me.CustomerID

Then replace your Kill statement with

Code:
 Kill "C:\Folder1\Folder2\" & '" & strCustID & "'"

EDIT RuralGuy got there first. Hi Allan
 
Tried several variations and could not get it to work.
 
Dim MyVar As String
MyVar = "C:\Folder1\Folder2" & CustomerID
If Dir(MyVar) <> "" Then Kill MyVar
 
You didn't say what happened but its easy to fix.
Add a backslash after Folder2
 
Nothing happened. But, the Customer ID will be a number with six digits.
 
I ttried editing my post right away and it would not stick. change this line:
MyVar = "C:\Folder1\Folder2" & CustomerID
...to...
MyVar = "C:\Folder1\Folder2" & CustomerID
 
Last edited:
Try my similar code instead
If it still doesn't work, add a Debug.Print line to view the output in the immediate window
 
The site will not let me post that trailing "".
???? I reported it to the Mods.
 
C:\Folder1\Folder2\

"C:\Folder1\Folder2"

Doesn't appear to work when you use quotes and outside of a code window?
 
So adding the code tags allowed the trailing ""?
Code:
"\"
 

Users who are viewing this thread

Back
Top Bottom