Results 1 to 6 of 6

Thread: [RESOLVED] Save file without using save dialog?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    348

    Resolved [RESOLVED] Save file without using save dialog?

    Im using this to load items into a listbox without open dialog how would i save the updated list to the same file, using the same kind of code?

    Code:
    Private Sub cmdLoad_Click()
    Dim fnum As Integer
    Dim file_line As String
    
        fnum = FreeFile
        Open txtFile.Text For Input As fnum
        Do While Not EOF(fnum)
            Line Input #fnum, file_line
            lstLines.AddItem file_line
        Loop
    End Sub
    Please Help Us To Save Ana

    -If you have been Helped, Please Mark Thread [Resolved] using Thread Tools

    -If someone has helped you, Please rate their post.

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Save file without using save dialog?

    vb Code:
    1. Private Sub cmdSave_Click()
    2. Dim fnum As Integer
    3. Dim intLoop As Integer
    4.    
    5.     fnum = FreeFile
    6.    
    7.     Open txtFile.Text For Append As #fnum
    8.         For intLoop = 0 To lstLines.ListCount - 1
    9.             Print #fnum, lstLines.List(intLoop)
    10.         Next intLoop
    11.     Close #fnum
    12. End Sub

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Save file without using save dialog?

    vb Code:
    1. Dim fnum as Integer, i as Integer, entire_list as String
    2.   'loop through list adding a line break (vbCrLf) after each value, saving it all to 'entire_list'
    3.   For i = 0 To lstLines.ListCount - 1
    4.     entire_list = entire_list & vbCrLf & lstLines.List(i)
    5.   Next i
    6.  
    7.   'write the text file
    8.   fnum = FreeFile
    9.   Open txtFile.Text For Output as fnum
    10.     Print #fnum, entire_list
    11.   Close fnum

    edit: digirev, I don't believe he wants to append to the file, seeing as how it's an updated list made up of that file.

    edit2: added a bit more code to my example :/
    Last edited by kows; Mar 28th, 2007 at 09:27 PM.

  4. #4
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Save file without using save dialog?

    You need to Open ... For Output to overwrite the original with the updated list. Open ... For Append adds to the existing file.

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Save file without using save dialog?

    Quote Originally Posted by kows
    edit: digirev, I don't believe he wants to append to the file, seeing as how it's an updated list made up of that file.
    Oops, didn't catch that.

  6. #6
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [RESOLVED] Save file without using save dialog?

    If speed is an issue, this might be the fastest method.
    vb Code:
    1. Dim fnum As Integer
    2. Dim i As Long
    3. Dim sLines() As String
    4.  
    5. ' Populate the listbox
    6. fnum = FreeFile
    7. Open txtFile.Text For Input As #fnum
    8.   sLines() = Split(Input(LOF(fnum), fnum), vbCrLf)
    9. Close #fnum
    10. lstLines.Clear
    11. For i = 0 To UBound(sLines)
    12.   lstLines.AddItem sLines(i)
    13. Next i
    14.  
    15. ' Save the list
    16. ReDim sLines(lstLines.ListCount - 1)
    17. For i = 0 To UBound(sLines)
    18.   sLines(i) = lstLines.List(i)
    19. Next i
    20. fnum = FreeFile
    21. Open txtFile.Text For Output As #fnum
    22.   Print #fnum, Join(sLines, vbCrLf);
    23. Close #fnum

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