none
Is there a way to allow a user to create a new file using visual basic ? RRS feed

  • Question

  • My program contains a database in MS Access. One of the tables contains a list of file directories related to a product number. This allows for each product to have multiple files associated with it. 

    When the user is in add mode
    I was wondering if it's possible to create a new file in the product folder with the name PROD_n (n depending on the ID) 

    To explain this better

    1) Here's a pre-existing  folder that contains all files related to the database:

    

    2) When user clicks button add new in VB a new folder is automatically created inside Prods with the name (assume) PROD_1

    Then if the user keeps on adding new, new folders (Prod_2, Prod_3) will continuously be created in the PROD folder. Inside these folders are all files related to this product. 

    Hope that makes sense :S?

    Feel free to ask for more information...

    Monday, July 17, 2017 3:33 PM

Answers

  • I want to create that directory myself

    I should have continued:

    There's a Directory class (shared methods) and a DirectoryInfo class also.

    As an example:

    Dim di As New System.IO.DirectoryInfo("K:\Example Directory")
    
    If Not di.Exists Then
        di.Create()
    End If
    
    Stop


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Monday, July 17, 2017 4:15 PM ...added example code
    • Marked as answer by creepyspirals Monday, July 17, 2017 5:45 PM
    Monday, July 17, 2017 4:10 PM

All replies

  • My program contains a database in MS Access. One of the tables contains a list of file directories related to a product number. This allows for each product to have multiple files associated with it. 

    When the user is in add mode
    I was wondering if it's possible to create a new file in the product folder with the name PROD_n (n depending on the ID) 

    To explain this better

    1) Here's a pre-existing  folder that contains all files related to the database:

    

    2) When user clicks button add new in VB a new folder is automatically created inside Prods with the name (assume) PROD_1

    Then if the user keeps on adding new, new folders (Prod_2, Prod_3) will continuously be created in the PROD folder. Inside these folders are all files related to this product. 

    Hope that makes sense :S?

    Feel free to ask for more information...


    Unless it's a protected directory then you can add/remove/edit all you want.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, July 17, 2017 3:38 PM
  • But How?

    All I can find is how to write things into a directory...
    I want to create that directory myself
    Monday, July 17, 2017 4:01 PM
  • But How?

    All I can find is how to write things into a directory...
    I want to create that directory myself

    The System.IO namespace is chock full of classes to do all sorts of things like this.

    For example the File class has shared methods.

    If you find yourself "repeating yourself" about the path, for example then consider using a FileInfo object. Once you declare an instance, it's got lots of great properties and methods and you won't need to give it the path again (which is why they're instance methods).


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Monday, July 17, 2017 4:06 PM
  • I want to create that directory myself

    I should have continued:

    There's a Directory class (shared methods) and a DirectoryInfo class also.

    As an example:

    Dim di As New System.IO.DirectoryInfo("K:\Example Directory")
    
    If Not di.Exists Then
        di.Create()
    End If
    
    Stop


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Monday, July 17, 2017 4:15 PM ...added example code
    • Marked as answer by creepyspirals Monday, July 17, 2017 5:45 PM
    Monday, July 17, 2017 4:10 PM
  • If you mean to add a file as link in Office Access (it is real a feature of MS Office and not of Access) then the answer is AFAIK standard "No".

    Newer versions of SQL server have a possibility in the case of an Image

    But probably Paul Clement knows a trick to do what you ask so wait until he replies. 


    Success
    Cor


    Monday, July 17, 2017 4:17 PM
  • Here is a basic example and note what I did with the file name can be done with the folder.

    Dim mockedIdentifier As Integer = 100
    Dim appFolder As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Production")
    Dim fileName As String = IO.Path.Combine(appFolder, $"SomeFile{mockedIdentifier}.txt")
    If Not IO.Directory.Exists(appFolder) Then
        IO.Directory.CreateDirectory(appFolder)
    End If
    
    If Not IO.File.Exists(fileName) Then
        IO.File.Create(fileName)
    End If
    But rather than simply using code such as above you should go through the documents that Frank provided.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, July 17, 2017 4:31 PM
    Moderator
  • Thank you!
    Monday, July 17, 2017 6:08 PM