Results 1 to 12 of 12

Thread: Load/Save listbox data to .txt...[RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Load/Save listbox data to .txt...[RESOLVED]

    How do I do this? I want the user to be able to "save as" and not just save it to a file already specified.
    Last edited by abstrait; Jan 7th, 2006 at 04:34 PM.

  2. #2
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Load/Save listbox data to .txt

    Use the commondialog, you will find it in your components.

    casey.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Load/Save listbox data to .txt

    But what is the vb code I have to use for the command button?

  4. #4
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Load/Save listbox data to .txt

    try this
    VB Code:
    1. Dim str1 As String, i As Integer
    2. On Error GoTo errorhandler
    3.  
    4. With CommonDialog1
    5.  .CancelError = True
    6.  .DialogTitle = "Select Location to Save to"
    7.  .Filter = "Text File (*.txt)|*.txt"
    8.  .InitDir = App.Path
    9.  .ShowSave
    10.  str1 = .FileName
    11. End With
    12.  
    13. Open str1 For Output As #1
    14.  For i = 0 To List1.ListCount - 1
    15.   Print #1, List1.List(i)
    16.  Next i
    17. Close #1
    18.  
    19. Exit Sub
    20.  
    21. errorhandler:
    22. End Sub
    you could also set some flags with the commondialog, have a look in your help files.

    casey.

  5. #5
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Load/Save listbox data to .txt

    Command1 Loads you file to Listbox, Command2 Saves your file to text file..
    You choose file to open and file to save
    VB Code:
    1. Private Function FileOpen() As String
    2.     With CommonDialog1
    3.     On Error GoTo Err_Handler
    4.             .Filter = ".TXT Files .txt|*.txt"
    5.             .InitDir = App.Path
    6.             .Flags = &H1000 Or &H4 Or &H2000
    7.             .CancelError = True
    8.             .ShowOpen
    9.    
    10.             FileOpen = .FileName
    11.     End With
    12.  
    13. Exit Function
    14. Err_Handler:
    15. End Function
    16.  
    17. Private Function FileSaveAs() As String
    18. On Error GoTo Err_Handler
    19.    
    20.     With CommonDialog1
    21.         .Filter = ".TXT Files .txt|*.txt"
    22.         .InitDir = App.Path
    23.         .Flags = &H2 Or &H4 Or &H2000
    24.         .CancelError = True
    25.         .ShowSave
    26.         FileSaveAs = .FileName
    27.     End With
    28.    
    29. Exit Function
    30. Err_Handler:
    31. End Function
    32.  
    33. Private Sub Command1_Click()
    34. Dim lFile           As String
    35. Dim lFileContent    As String
    36. Dim lArrFile()      As String
    37. Dim i               As Long
    38.  
    39.     List1.Clear
    40.     lFile = FileOpen
    41.    
    42.     If Trim$(lFile) <> vbNullString Then
    43.         Open lFile For Binary As #1
    44.             lFileContent = Space(LOF(1))
    45.             Get #1, , lFileContent
    46.         Close #1
    47.         lArrFile = Split(lFileContent, vbNewLine)
    48.         For i = 0 To UBound(lArrFile)
    49.             List1.AddItem lArrFile(i)
    50.         Next
    51.     End If
    52.    
    53.     lFileContent = Empty
    54.     Erase lArrFile
    55. End Sub
    56.  
    57. Private Sub Command2_Click()
    58. Dim lStrFile As String
    59. Dim i        As Long
    60.  
    61.     lStrFile = FileSaveAs
    62.     If Trim$(lStrFile) <> vbNullString Then
    63.         Open lStrFile For Output As #1
    64.             For i = 0 To List1.ListCount - 1
    65.                 Print #1, List1.List(i)
    66.             Next
    67.         Close #1
    68.     End If
    69. End Sub
    Last edited by jcis; Jan 7th, 2006 at 03:41 PM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Load/Save listbox data to .txt

    Thanks!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Load/Save listbox data to .txt... One last question

    How would I save a textbox? Nearly the same code, I assume. I just don't know what to change ListCount from


    VB Code:
    1. For i = 0 To Text2.ListCount - 1
    Help?

  8. #8
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Load/Save listbox data to .txt... One last question

    you wouldnt need the loop.

    Print #1, Text1.Text

    casey.

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Load/Save listbox data to .txt... One last question

    Textboxes doesn't have a listcount, save its value directly, like:
    Print #1, Text2.Text

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Load/Save listbox data to .txt... One last question

    That works, thanks.

    How would I clear three textboxes and two ListBoses with one click?

  11. #11
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Load/Save listbox data to .txt... One last question

    For textboxes:
    Text1.Text = VbNullString

    For Listboxes:
    List1.Clear

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Re: Load/Save listbox data to .txt... One last question

    Wonderful. That was my last question, I swear.

    Thanks for all your help.

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