Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access 2002 - Create a new directory

Posted on 2008-06-18
19
Medium Priority
?
309 Views
Last Modified: 2013-11-28
hello!

I want to accomplish two things:
1)  To check and see if a directory exists or not
2)  To create a new directory off any drive letter.  Example:  C:\NewDirectoryt

Is there a procedure in VB that already does this ?

Thank you!
0
Comment
Question by:ellenjbr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 5
19 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 2000 total points
ID: 21819175
Use this snippet:


   
    If Len(Dir("C\SomePath\SomeMorePath", vbDirectory)) = 0 Then
        MkDir "C\SomePath\SomeMorePath"
    End If
   
0
 

Author Closing Comment

by:ellenjbr
ID: 31468662
Great!  Thanks!
0
 
LVL 75
ID: 21819387
You are welcome ...

mx
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21819557
ellenjbr,

I have been using this for a while:

On Error Resume Next
Dim strFolderName As String
Dim varArray As Variant '(Array)
Dim strFinalName As String
Dim intCounter As Integer
strFolderName = InputBox("Enter Folder Name:" _
                & vbCrLf & "(ex: C:\YourNewFolderName)")
varArray = Split(strFolderName, "\")
strFinalName = varArray(0)
    For intCounter = 1 To UBound(varArray)
        strFinalName = strFinalName & "\" & varArray(intCounter)
        MkDir strFinalName
    Next intCounter


Modified from this link:
http://www.tek-tips.com/viewthread.cfm?qid=1161048&page=1

You can obviously convert this to a function, if need be.

JeffCoachman

0
 
LVL 75
ID: 21819565
?
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21819850
??
0
 

Author Comment

by:ellenjbr
ID: 21819953
boag2000 - Hi and thank you for your input.  I only want to create the one folder.   Also, on testing the previous code, it errored after the folder was created... evidently it does not recognize when it checks
I wrote this:

dim valDir as string

valDir = "C:\newfolder"
if Len(dir(valDir)) = 0 then
  MkDir valDir
endif

Is there no CheckDir or IsDir function/procedure in VB?  Thanks!

0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21821874
ellenjbr,

You actually accepted that post shortly before I posted mine.
So I presumed it was working for you.
I don't know of any "CheckDir" command for DOS.

(I have seen "If Exists" used in some instances though.)

I moded my code like so, to alert me (With a popup messagebox) if the folder/Directory already exists:

On Error GoTo Err_MakeDirFolder
Dim strFolderName As String
Dim varArray As Variant '(Array)
Dim strFinalName As String
Dim intCounter As Integer
strFolderName = InputBox("Enter Folder Name:" _
                & vbCrLf & "(ex: C:\YourNewFolderName)")
varArray = Split(strFolderName, "\")
strFinalName = varArray(0)
    For intCounter = 1 To UBound(varArray)
        strFinalName = strFinalName & "\" & varArray(intCounter)
        MkDir strFinalName
    Next intCounter

Exit_MakeDirFolder:
    Exit Sub

Err_MakeDirFolder:
    'If the Folder/Directory already exists
    If Err.Number = 75 Then
        MsgBox "The folder: " & "'" & strFinalName & "'" & " already exists."
        Resume Exit_MakeDirFolder
    Else
        MsgBox "There was an error executing the command." _
        & vbCrLf & "Error " & Err.Number & ": " _
        & vbCrLf & Error, vbExclamation
        Resume Exit_MakeDirFolder
    End If




JeffCoachman
0
 

Author Comment

by:ellenjbr
ID: 21822360
Jeff:

Hello!  I take that the word 'resume' allows one to escape without getting an Access or Windows error message ...I'll have to try the code later ... I think that the first line of the code mentioned earlier Len(Dir(PRIV)) = 0, apparently doesn't work.

Is there a delete dir command as well ... thank you.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21822843
ellenjbr,

The 'resume' part is just part of the standard Access error handling code.
It goes back to a theory that says; a sub or function should have one exit point.

You can use:
RmDir "C:\YourFolderName"
... to delete a folder/directory.

JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21822870
boag2000,

But I am still confused,
...Did MX's stated code work for you?

JeffCoachman
0
 
LVL 75
ID: 21823096
The code I posted is in daily use - with no errors.

mx
0
 
LVL 75
ID: 21824509
I'm confused ... is there an issue?

mx
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21826047
MX,

I'm not sure, see post:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_23497685.html#a21819953
< Also, on testing the previous code, it errored after the folder was created>

Let's wait and see.

Jeff
0
 
LVL 75
ID: 21826105
Yes .... not clear ... your code  or my code.  I pulled that snippet right out of my app launcher.  It checks for the existance of the dir, if not there - creates it ... then copies the front end and library file down to the users C:\SomeDir.  Again, in daily use.

mx
0
 

Author Comment

by:ellenjbr
ID: 21828878
hi ...

  If Len(Dir("C\SomePath\SomeMorePath", vbDirectory)) = 0 Then
        MkDir "C\SomePath\SomeMorePath"
    End If

the above code, in tact, does not work .... if I add a : for c:\ , and other variations, it still does not work.  only what I posted earlier works re:  making the directory.  The first line of the code doesn't do what you are wanting.

Thank you for the rmdir .... I will test out the complete code asap and get back with you on it.... thanks!
0
 
LVL 75
ID: 21828903
I see the problem ... I left off back slashes at the end of the path - because I actually have string variables - not hard coded paths.

    If Len(Dir("C\SomePath\SomeMorePath\", vbDirectory)) = 0 Then
        MkDir "C\SomePath\SomeMorePath\"
    End If

The snipped above is from this (part of a function)

    Dim obj As Object
    Dim mVersion As String
    Dim DbName As String
    Dim DbRefLib1 As String
    ''Dim DbRefLib2 As String
    Dim DbImg1 As String
    Dim DbImg2 As String
    Dim DbSrcFldrPgrm As String
    Dim DbSrcFldrLib As String
    Dim DbSrcFldrImg As String
    Dim DbDestFldr As String
     
   
    DbSrcFldrPgrm = "\\xyz\workgroup\CSBU2\BPO-CSBU\Mass AOR Desk\zzFEMasters\"
    DbSrcFldrImg = "\\xyz\workgroup\CSBU2\BPO-CSBU\Mass AOR Desk\zzFEMastersImg\"
    DbSrcFldrLib = "\\xyz\workgroup\CSBU2\BPO-CSBU\Mass AOR Desk\zzLIBMasters\"
   
    DbName = "MOPPgrm.mdb"
    DbImg1 = "MOPPgrm.bmp"
    DbImg2 = "MOPLogo.bmp"
    DbRefLib1 = "ADIvba09LIB.mda"
    DbDestFldr = "C:\MassDatabases\"
       
    If Len(Dir(DbDestFldr, vbDirectory)) = 0 Then
        MkDir DbDestFldr
    End If
   
    FileCopy DbSrcFldrImg & DbImg1, DbDestFldr & DbImg1
    FileCopy DbSrcFldrImg & DbImg2, DbDestFldr & DbImg2
    FileCopy DbSrcFldrLib & DbRefLib1, DbDestFldr & DbRefLib1
    '''FileCopy DbSrcFldrLib & DbRefLib2, DbDestFldr & DbRefLib2

    FileCopy DbSrcFldrPgrm & DbName, DbDestFldr & DbName

This code gets called  many times a day by many users ....

mx
0
 

Author Comment

by:ellenjbr
ID: 21843784
Your code works fine ... thanksl
0
 
LVL 75
ID: 21843793
Yes ... I'm using it in another app as we 'speak'.

mx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question