Results 1 to 9 of 9

Thread: [RESOLVED] save/load

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Resolved [RESOLVED] save/load

    i just want a button to be save and one for load and when i click save it just saves a file with everything on the list and loads that again if i click on the load button

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: save/load

    Use the below code:
    Code:
    Private Sub cmdLoad_Click()
    Dim F
    Dim Temp As String
    F = FreeFile
    Open "c:\file1.dat" For Input As #F
    Do While Not EOF(F)
        Input #F, Temp
        List1.AddItem Temp
    Loop
    Close #F
    MsgBox "Loaded"
    End Sub
    
    Private Sub cmdSave_Click()
    Dim F
    F = FreeFile
    Open "c:\file1.dat" For Output As #F
    For i = 0 To List1.ListCount
        Print #F, Str(List1.List(i))
    Next i
    Close #F
    MsgBox "Saved"
    End Sub

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: save/load

    i got one error

    type missmatch

    Print #F, Str(List1.List(i))

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: save/load

    Here is one Sub that does both. Modify as necessary
    Code:
    Private Sub SaveLoadListbox(plstLB As ListBox, _
    pstrFileName As String, _
    pstrSaveOrLoad As String)
    
    Dim strListItems As String
    Dim i As Long
    
    Select Case pstrSaveOrLoad
       Case "save"
        Open pstrFileName For Output As #1
        For i = 0 To plstLB.ListCount - 1
            plstLB.Selected(i) = True
            Print #1, plstLB.List(plstLB.ListIndex)
        Next
        Close #1
    
       Case "load"
       plstLB.Clear
        Open pstrFileName For Input As #1
        While Not EOF(1)
          Line Input #1, strListItems
          plstLB.AddItem strListItems
        Wend
        Close #1
    End Select
    
    End Sub
    
    Private Sub Form_Load()
    Call SaveLoadListbox(List1, "c:\Listbox.txt", "load")
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Call SaveLoadListbox(List1, "c:\Listbox.txt", "save")
    End Sub

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: save/load

    i get an runtime error 53...

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: save/load

    Sorry, I used to str()... because in the sample program that I had created, listbox is filled with numbers only...

    You can replace it with the following line:
    Code:
    Print #F, List1.List(i)

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  7. #7
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: save/load

    Also, change the following line:
    Code:
    For i = 0 To List1.ListCount
    into
    Code:
    For i = 0 To List1.ListCount - 1

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: save/load

    Quote Originally Posted by Justa Lol
    i get an runtime error 53...
    On what line?

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: save/load

    Quote Originally Posted by Hack
    On what line?
    never mind akhileshbc made it work for me thanks anyways

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