Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Deleting Info From Write File

Posted on 2004-10-01
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

sgayatri earned 120 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 120 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 90 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 90 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

721 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