Deleting Info From Write File

Posted on 2004-10-01
Last Modified: 2010-05-02
Im working on an IRC program, Im trying now to save the servers, so the user wont have to remember them all
I have a list box, I put inside it the server names that are saved using this code
Text1 = Server
Text2 = Port
Dim Ser(2500) As String
Dim Por(2500) As String
Dim TotalWords As Integer

Private Sub Command6_Click()
'How To Save
Ser(TotalWords) = Text1.Text
Por(TotalWords) = Text2.Text
Open "Servers.dat" For Append As #1
    Write #1, Ser(TotalWords), Por(TotalWords)
TotalWords = TotalWords + 1

'How To Load
TotalWords = 0
Open "Servers.dat" For Input As #1
    Do Until EOF(1) = True
        Input #1, Ser(TotalWords), Por(TotalWords)
        TotalWords = TotalWords + 1
Dim x As Integer
For x = 0 To TotalWords - 1
    List1.AddItem Ser(x)
Next x
End Sub

Ok the problem is, I want to make the user able to remove some servers by highlighting them on the list and clicking command button, but then I have to remove them from the file it self and I must not keep any blank lines or spaces in the file (need that for another thing)
So how to do that?
Question by:deathman5

Accepted Solution

sgayatri earned 40 total points
ID: 12198313
The logic yu have to use is :
1. Whenever an item in the list box is clicked,
and delete button is pressed,
save the listindex of the item clicked.
2. delete that item from the list
3. create a temporary file servers_temp.dat
4. Read servers.dat line by line and copy each line to servers_temp.dat till you reach the line to be deleted
(keep incrementing a counter for each line)
5. When you reach the line to be deleted, skip that line without writing into servers_temp.dat
6. Copy all lines from servers.dat to servers_temp.dat
7. delete servers.dat
8. rename servers_temp.dat to servers.dat

Hope I am clear.
Shall try to post thecode , but need some time


Author Comment

ID: 12198613
Well I thought of that, but it doesn’t work in my case
This is the .dat file


In the list box I have:


The port is not shown, so I can’t copy from list box and paste in another file, this would only copy the server; I need to copy the port too…

Assisted Solution

sgayatri earned 40 total points
ID: 12198683
I never told you to copy from list box.
All you have to do is , note the "LISTINDEX"
whenever a delete is done

Add a command button for delete,

In command1_click
msgbox list1.listindex+1
list_index_number = list1.listindex+1

will give you the number of the line clicked.

use list_index_number  to delete that number line in your file.

LVL 76

Assisted Solution

by:David Lee
David Lee earned 30 total points
ID: 12198762
Why store the server names twice, once in the Ser array and again in the ListBox?  Instead, I'd create a second ListBox for the ports and set its Visible property to false.  Eliminate the Ser and Por arrays and read the data directly into the two ListBoxes.  When the user deletes one of the servers, you delete the values from both ListBox controls at the same time.  Something like:

    Private Sub DeleteServer()
            PorList.RemoveItem (SerList.ListIndex)
            SerList.RemoveItem (SerList.ListIndex)
    End Sub

We remove the entry from the Ports ListBox first because when we remove the entry from the Servers ListBox its ListIndex will change.

This will keep the two lists in sync.  Now when you exit the program all you need do is loop through the Servers ListBox and use its ListIndex value to get the corresponding Ports value and write them to the file.  Not only does this make your coding simpler, it also eliminates storing the same data twice, once in the array and again in the ListBox which will make your program smaller and faster.

Assisted Solution

VBtorment earned 30 total points
ID: 12199912

try this

on delete just do: Ser(index)=""
then save complete list, not just delete one from list...

'Load is good
Open "Servers.dat" For Input As #1
    Do Until EOF(1) = True
        Input #1, Ser(TotalWords), Por(TotalWords)
        TotalWords = TotalWords + 1

'for Saving try this
kill "Servers.dat"
Open "Servers.dat" For Append As #1
    for i%=1 to TotalWords
        if Ser(i%)<>"" then
            Write #1, Ser(i%), Por(i%)
        end if
    next i%

hope this helps

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question