CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2001
    Location
    PA, U.S.
    Posts
    3

    Error When Saving To Text File - Please Help

    I am making a program in VB5 for my High School senior project, this question is relating to the vocab section of the program. In this section, the user has the option to selct from a number of premade vocab lists which are stored in a folder in the same directory as the progem, when one of these premade lists are selected it is opened and loaded into a listbox. This part of the vocab section works, but the second option that the user can do is to create there own lists. Everything works fine except, when the user goes to save the new list a path not found error pops up. Heres my question, is there a way to create a .txt file and then save the new list to that .txt file so that the path will exist?

    Here's my code:



    private Sub SaveBtn_Click()
    Dim ListNum as Integer
    Dim SFileName as string
    Dim SFilePath as string
    ListNum = ListNum + 1
    SFileName = "NewList" + CStr(ListNum)
    SFilePath = App.Path + "\EngVocab\NewLists\"
    Open SFilePath + SFileName for Output as #1
    for X = 0 to NewList.ListCount - 1
    print #1, NewList.List(X)
    next X
    Close #1
    End Sub




    Please respond as soon as possible



  2. #2
    Join Date
    Mar 2000
    Location
    Calif
    Posts
    21

    Re: Error When Saving To Text File - Please Help

    I have not put a lot a thought into this but this may get you going in the right direction.
    First you need to create a new text file for the user to save their personal data to, then you can write to it.
    Create a file called "MyFile_Temp.txt" - this will be unchanged, only used to create the new file inwhich the user will name with an Input box.

    vNewFile = InputBox("Input a Name for the New File..." + vbCrLf + "Do not duplicate an existing name.", " Create New File", "")

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    'this will create the new file named by the user, and now you can write to it
    fso.CopyFile App.Path & "\MyFile_Temp.txt", App.Path & "\" & vNewFile & ".txt"



  3. #3
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    78

    Re: Error When Saving To Text File - Please Help

    I'm also a "beginner", in fact, I've only used VB6, so I think the same goes for VB5.

    To run the code posted by Blazin Wild, you would need to go to the References and add "Microsoft Scriping Runtime" to use FSO.


  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Error When Saving To Text File - Please Help

    A couple of things.
    1). Replace those PLUS signs (+) in your string handling statements with Ampersands (&). The (+) sign means add things together. You want to concatenate string parts. If your path names contained numbers, VB would try to add them instead of concatenating them. The (&) means Concatenate together which is more appropriate in the case of building Path and Filenames.
    2). The Path must exist before you can open it which is probably why you are getting the Path not found error.
    Opening your file for output will only create the File, not the Path to it so you need to use the MKDIR statement to create the path. You need to create the parent directory before the child something like this:

    MKDIR App.Path & "\EngVocab"
    ' then
    MKDIR app.path & "\EngVocab\NewLists"
    ' then
    Open SFilePath & SFileName for Output as #1




    John G

  5. #5
    Join Date
    Mar 2000
    Location
    Calif
    Posts
    21

    Re: Error When Saving To Text File - Please Help

    Yes Raptors is correct I left a couple of things out when using "fso" scriping...
    Click... Project | References | and check "Windows Scripting Host Object Model (Ver 1.0) "
    With set up install also include WSHOM.OCX and SCRRUN.DLL (allow to self register).

    Use of the '+' or '&' I do not see where it makes a difference they both work. A finer point of edict I suppose - seems to be more important things in life to worry about.



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

Featured