Can MkDir be used to create a folder with a unique name? (1 Viewer)

gojets1721

Registered User.
Local time
Today, 04:50
Joined
Jun 11, 2019
Messages
429
So I've got a DB of employees and a query that filters down to one random employee whenever opened.

I am trying to code a function using MkDir that, when executed, it creates a new windows folder in a designated location and names the folder using the employee found in the random query (i.e. "Smith, John").

I've found a guide on how to use MkDir but I can't find anything about designating a unique name for the folder when the VBA is run.

Any thoughts?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 04:50
Joined
Oct 29, 2018
Messages
21,471
MkDir can make a folder if you give it the name of the folder. So, you need a code to generate that name. If you give us the logic for generating the random name, we can try to translate it into vba.
 

jdraw

Super Moderator
Staff member
Local time
Today, 07:50
Joined
Jan 23, 2006
Messages
15,379
Suppose you weren't creating a directory/folder, how would you display the unique name?
 

Isaac

Lifelong Learner
Local time
Today, 04:50
Joined
Mar 14, 2017
Messages
8,777
I've found a guide on how to use MkDir but I can't find anything about designating a unique name for the folder when the VBA is run.
how is that possible? Any "guide on how to use MkDir" will include pretty much the one and only thing you need to know:

MkDir SomeString

What are you having trouble with? What's the problem specifically when you try to use it?

Declare a string variable, assign it a value, then use MkDir StringVariableName
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:50
Joined
Feb 28, 2001
Messages
27,179
The only real problem you face is that there are some characters that won't work in a folder name.


If you have a guy named Mike Smith and you want to make a folder for him, there is nothing wrong with the name Mike_Smith - except if you have two different Mike Smith employees. Can't make a new folder if another one exists with the same name. How you want to approach THAT problem is strictly up to you.
 

gojets1721

Registered User.
Local time
Today, 04:50
Joined
Jun 11, 2019
Messages
429
Hi all,

I apologize; I forgot to link the guide I used. See here. You'll notice that it provides the ability to create a custom folder name, but I'm not show how to code it so that it pulls from the query.

I'm generating the first and last name by just using a 'select top 1' query. So the query is pulling 1 random staff member each time.

I'm doing this because I have to audit 1 employee each month. I store the audit documents in a folder with their name. So if I have a code that generates a folder with their name, I basically kill two birds with one stone. I identify the employee who needs to be audited and I have a folder with their name already created. The folder is also created in a temp spot. I'd move it somewhere else for long term storage when completed.
 

moke123

AWF VIP
Local time
Today, 07:50
Joined
Jan 11, 2013
Messages
3,920
One simple way of creating a unique name is to append cdbl(date) or cdbl(Now) to the end of the staff name.
The folder name would look like John_Smith_44733 or John_Smith_44733.3882638889
 

CJ_London

Super Moderator
Staff member
Local time
Today, 12:50
Joined
Feb 19, 2013
Messages
16,610
In your query call your function and pass the name as a parameter to the function

select myfunction([namefield])
From mytable
Where pk=1
 

Isaac

Lifelong Learner
Local time
Today, 04:50
Joined
Mar 14, 2017
Messages
8,777
One simple way of creating a unique name is to append cdbl(date) or cdbl(Now) to the end of the staff name.
The folder name would look like John_Smith_44733 or John_Smith_44733.3882638889
I also make a habit out of appending dates to filenames. The simplicity - and foolproof nature of it - is unbeatable.
Whether you use a YYYYMMDDHHMMSS format or a number, etc.
 

Users who are viewing this thread

Top Bottom