Results 1 to 5 of 5

Thread: [RESOLVED] Can VB6 code direct Windows to create a new file?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Resolved [RESOLVED] Can VB6 code direct Windows to create a new file?

    The application requires the existence of a certain text file, say C:\XYZ.txt.

    If that file does not already exist on the machine, is it possible by VB code to have Windows create it, as it would in Windows Explorer for example?

    I guess one approach might be oAPP of Explorer, but maybe there is a neater way purely from within VB. Calling up EXPLORER might run into difficulties between machines running different versions of EXPLORER under different OS.

    camoore

  2. #2
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    Re: Can VB6 code direct Windows to create a new file?

    You can create it by writing an empty file, kinda like this:
    Code:
    Dim ff as Integer
    ff = FreeFile
    Open "C:\XYZ.txt" For Output As #ff: Close #ff
    If a thread is solved, please click on Thread Tools / Mark Thread Resolved .
    If someone helped you very good, consider rating his post by clicking the icon under his name.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: Can VB6 code direct Windows to create a new file?

    Many thanks, garrcomm, for this rapid reply. It works a treat!

    The idea was that a program might generate (if not already in existence) a text file and then use that as a "low overhead" database to remember information between instances of shutting down the program and / or shutting down the whole machine. It saves setting up ADODC, DataGrids and so forth.

    The attached is the test program I have written to explore the technique.

    It creates a new file, then allows the user to open it into a RichTextBox, edit it while in that box and save it back to the file location. A program could very well do just the same.

    Various 'errors' need to be catered for. In particular, it needs to be checked that the specified file path / name does not already exist. Otherwise the routine will not generate any error message, but will blank the previous file content.

    In using this concept in a program, it would be important to specify a file name which is highly obscure and virtually impossible to exist already in any other context.

    I note that if the user fails to add an extension to the file path, VB defaults to .txt.

    Thank you for such a useful answer.

    camoore

    ps. I see that you work in NL. I have also done so. Lovely country and lovely people was my finding.
    Attached Files Attached Files
    Last edited by camoore; Jul 14th, 2009 at 07:31 AM. Reason: typos

  4. #4
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    Re: [RESOLVED] Can VB6 code direct Windows to create a new file?

    Thanks for the compliment. I love my country and the diversity of people in it.

    Code:
    Private Sub Command2_Click()    'open file
    
    PATH = Text1.Text
    
    On Error GoTo command2_click_error
    RichTextBox1.LoadFile PATH, rtfText
    Exit Sub
    command2_click_error:
        If Err.Number = 75 Then MsgBox "The file does not exist", vbCritical
        Resume Next
    End Sub
    If you use that, you get a nice notice that the file does not exist.

    If you be a little more creative, you can retry the action with .txt added, like this:
    Code:
    Private Sub Command2_Click()    'open file
        Dim failed As Boolean
        
        PATH = Text1.Text
        
        On Error GoTo command2_click_error
        RichTextBox1.LoadFile PATH, rtfText
        If failed Then RichTextBox1.LoadFile PATH & ".txt", rtfText
        Exit Sub
        
    command2_click_error:
        If Err.Number = 75 And failed Then
            MsgBox "The file does not exist", vbCritical
            Resume Next
        ElseIf Err.Number = 75 Then
            failed = True
            Resume Next
        End If
        MsgBox "Unknown error occured: " & Err.Number & vbCrLf & Err.Description
    End Sub
    If a thread is solved, please click on Thread Tools / Mark Thread Resolved .
    If someone helped you very good, consider rating his post by clicking the icon under his name.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: [RESOLVED] Can VB6 code direct Windows to create a new file?

    Thank you Garrcomm for another very useful piece of code about error handling. I will print it out when I get home and place it in my notes.

    The scope for errors in the way I plan to use this file creation routine in a program will be much less than in the test program - in which a user could enter all manner of 'silly' paths.

    The intended program logical sequence will be something like this.

    1. The program will depend on a long obscure file name which is HIGHLY unlikely to have been used by the user or any other program. For now let us just call it XYZ.txt

    2. On opening, the program will try to transfer (by Loadfile) XYZ.txt to a RichTextBox.

    3. If no error is generated, the program will assume that file XYZ.txt previously existed (from a previous instance of the program). The program will proceed to parse out of the information now in the RT Box the stored information it requires.

    4. If on the other hand an error occurs at Loadfile, then the program will assume that XYZ.txt did not formerly exist and will, i.a.w. your method, generate XYZ.txt. Thus knowing that this is the first time through, the program will not seek to place any dependancy on the text content (since it will be blank).

    5. Prior to program close-down, the software will write a data set to the RichTextBox and use the Savefile method to transfer this to XYZ.txt.

    6. The program can then close down, and machine be switched off.

    7. Next time the program runs, XYZ.txt will exist on that machine, and everything written there will be imported into the RichTextBox.

    8. Thus XYZ.txt will act as a mini database, requiring minimum effort to set up and allowing me compete freedom as to format, layout and content.

    9. I regard that as a GREAT achievement for today, thanks to you for your invaluable assistance.

    Tot Siens

    camoore

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width