Create folder from within Access VB

M

Michael T

Please will you advise on the following:

I am trying to create a folder on the C drive of my PC from within Access
VB. Hunting through the Access VB help files has got me the following but
there are no examples and I get a message that "ActiveX component can't
create object" in line 4 below (Set fs..) when I execute it.

Public Function CreateFolder(strFolderName As String)

Dim objTemp As Object
Dim fs As Object

Set fs = CreateObject("Scripting.FilesSystemObject")
Set objTemp = fs.CreateFolder(strFolderName)
Set objTemp = Null
Set fs = Null

/* Ignore any errors and just send back OK
CreateFolder = "OK"

End Function

I just call this with the folder name:
strResult = CreateFolder("C:\foldertest") with and without trailing \ gives
the same error.

I guess I don't understand how/why I need to create a system object - the
syntax for the actual create seems to be simple.

Help appreciated.

Michael.
 
R

RDub

Why in holly Hanna are you using FSO when you can do it by using a simple
VBA statement? Have a look at the MkDir statement in Access help.

Ron W
 
M

Michael T

Hello Ron,

Thank you for your advice. The help index is not exactly helpful - it's one
of those things where you have to know the nswer before you can ask the
question - I was innocently looking up 'folder' and how to create them.

I'm sorry but I don't understand your reference to 'holly Hanna' and 'FSO'
whoever or whatever she/it/they is/are. Is it some form of joke? Never mind
:)

Anyway...

Thanks again.

M.
 
R

Ron Weiner

Holly Hanna is a mild expletive (kinda' like "Great Caesar's Ghost" Ala
Perry White editoe of the Daily Planet) I use every time I see someone using
the File Scripting Object (FSO) in an Access app. I can not think of a
single reason to use that abomination (FSO) in an Access application.
Everything that can be done using FSO can be done easier, faster, better
using native Access commands, or when there is a need for speed the Windows
API. In fact there are loads of computers out there (especially in
corporate computing environments) that will puke should you try to use it.

Ron W
 
M

MikeB

Ron Weiner said:
Holly Hanna is a mild expletive (kinda' like "Great Caesar's Ghost" Ala Perry
White editoe of the Daily Planet) I use every time I see someone using the
File Scripting Object (FSO) in an Access app. I can not think of a single
reason to use that abomination (FSO) in an Access application. Everything
that can be done using FSO can be done easier, faster, better using native
Access commands, or when there is a need for speed the Windows API. In fact
there are loads of computers out there (especially in corporate computing
environments) that will puke should you try to use it.

I agree that the available file handling within Access should be used, but
the blanket, wholesale dismissal of the FSO likely would be contested by
(insert whatever your suspension of disbelief will allow for a number) of sys
Admins that use it everyday in the Scripting Environment for which it was
specifically created.

I personally have never experienced a conflict or failure of the FSO (have used
it since NT4, so a while), but I have also never heard anyone sing a praise of
it.. curious not?
 
D

Dirk Goldgar

Michael T said:
Please will you advise on the following:

I am trying to create a folder on the C drive of my PC from within Access
VB. Hunting through the Access VB help files has got me the following but
there are no examples and I get a message that "ActiveX component can't
create object" in line 4 below (Set fs..) when I execute it.

Public Function CreateFolder(strFolderName As String)

Dim objTemp As Object
Dim fs As Object

Set fs = CreateObject("Scripting.FilesSystemObject")
Set objTemp = fs.CreateFolder(strFolderName)
Set objTemp = Null
Set fs = Null

/* Ignore any errors and just send back OK
CreateFolder = "OK"

End Function

I just call this with the folder name:
strResult = CreateFolder("C:\foldertest") with and without trailing \
gives the same error.

I guess I don't understand how/why I need to create a system object - the
syntax for the actual create seems to be simple.


I agree with others who have posted suggesting that you use the MkDir
statement instead of incurring the overhead of the File System Object.
However, it may be worth pointing out that the reason for the error your
code is raising is probably that you've misspelled the name of the object:
Set fs = CreateObject("Scripting.FilesSystemObject")

should be:

Set fs = CreateObject("Scripting.FileSystemObject")
 
R

Ron Weiner

The use of FSO in a scripting environment is fine as this is what it was
designed for and where it belongs. As a former IT admin in a mid size
company with several dozen branch offices, I couldn't have gotten my job
done with out it. I am just saying it should NEVER be used in an Access
Application, and should be considered an EVIL there. As an aside, we had
all scripting turned off in almost all employee workstations as another
level of protection against malware. Your Access application would have
failed there. I suspect we were/are not the only company who had a policy
about allowing employees to run scripts on their workstations.

Ron W
 
M

MikeB

Ron Weiner said:
The use of FSO in a scripting environment is fine as this is what it was
designed for and where it belongs. As a former IT admin in a mid size
company with several dozen branch offices, I couldn't have gotten my job done
with out it. I am just saying it should NEVER be used in an Access
Application, and should be considered an EVIL there. As an aside, we had all
scripting turned off in almost all employee workstations as another level of
protection against malware. Your Access application would have failed there.
I suspect we were/are not the only company who had a policy about allowing
employees to run scripts on their workstations.

Ron W

Did you also have DOS Batch files turned off?
Not to continue this line, but I seem to remember that an executable can be
created in / with a batch file, no?
I think that is the source of original virii, IIR... course the first virii of
the 80's usually infected / affected only one box.
 
M

MikeB

MikeB said:
Did you also have DOS Batch files turned off?
Not to continue this line, but I seem to remember that an executable can be
created in / with a batch file, no?
I think that is the source of original virii, IIR... course the first virii
of the 80's usually infected / affected only one box.

A quick google showed a link of how to create an executable with a batch
file. I am not an assembler programmer (required to do this), but even though
the example is of a mundane process, I don't think it is going to take much of
a program to wreak havoc from a batch file.

http://www.palmtoppaper.com/ptphtml/25/pt250058.htm
 
R

RDub

The idea was to reduce the attack surface area, not to bring the company to
a grinding halt. All I am/was saying is that you use the Scripting Objects
at your own peril, as they MAY not be available everywhere. Far better to
use the native commands in Access or the Win API which by the nature of an
Access application HAVE to be there. Won't even get into performance where
everything out performs the Scripting Objects.
 
M

MikeB

RDub said:
The idea was to reduce the attack surface area, not to bring the company to a
grinding halt. All I am/was saying is that you use the Scripting Objects at
your own peril, as they MAY not be available everywhere. Far better to use
the native commands in Access or the Win API which by the nature of an Access
application HAVE to be there. Won't even get into performance where
everything out performs the Scripting Objects.

I know.. You do understand I am in lock step with you, I just was pointing
out another vulnerability that is likely overlooked (and drifting way OT to
boot, so sorry there).
 
K

Klatuu

Use the MkDir statement. It is much easier to use an doesn't require
instanciating an object.
 
D

Daniel Pineault

You don't need such as function, simply use the MKDIR statement

ie:
mkdir("C:\testfolder")
 
T

Trever B

Hi Michael,

When creating a folder I tend to do it this way

Public Function CreateFolder(strFolderName As String)

On Error Resume Next ' skip if folder already exists

'strFolderName must include the
full path

MkDir strFolderName

End Function

Hope this works

Trev
 
D

Dale Fye

Michael,

I think you are going about it the hard way.

Check out the BrowseFolderAPI (see link below). This is the standard
windows folder browsing dialog box, which provides the user the ability to
create a folder at a specified location.

http://www.mvps.org/access/api/api0002.htm

Also, check out the help on the mkDir( ) statement.

HTH
Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top