Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying lines from 1 txt to another

Status
Not open for further replies.

OzzieBloke

Programmer
Mar 28, 2004
31
AU
I have a program which can have multiple users. I have created it so that extra users can be added. This is done by creating a txt document which I have called source and placing the users name and ID number within. However I am having some trouble removing the users from the text document.

This is my code so far:
Private Sub Remove_Click()
Open "source.txt" For Input As #1
Open "target.txt" For Output As #2

Do While Not EOF(1)
nIndex = 0
Input #1, UserName, UserID
If Not NewUser.User.Selected(nIndex) = UserName Then
Write #2, UserName, UserID
Else
nIndex = nIndex + 1
End If
Loop
Close
End Sub
 
Can you show the construction of this bit:

If Not NewUser.User.Selected(nIndex) = UserName Then

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
NewUser is the form name, User is the name of the list box in which the users names are displayed.
 
I was just checking that I understood the question. Thank you for the clarification. The NewUser.User.Selected(nIndex) will only return True or False so your test will always succeed.

You probably want to test the NewUser.User.List(User.ListIndex) property, which returns the value of the selected item in the listbox thus:
[tt]
If Not NewUser.User.List(List2.ListIndex) = UserName Then
[/tt]
Also take your:
[tt]nIndex = 0
[/tt]
out of the loop otherwise you won't move through the list.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
all i needed was

If Not NewUser.User.List(List2.ListIndex)

That made all the difference.

Thanx
 
You'll need to check for :
NewUser.User.Selected(nIndex) = True

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
So far i have:

Dim i As Integer
Open "source.txt" For Input As #1
Open "target.txt" For Output As #2
Do While Not EOF(1)
Input #1, UserName, UserID
For i = 0 To NewUser.User.ListCount - 1
If NewUser.User.List(Recipients.ListIndex) = UserName Then
Exit For
End If Write #2, UserName, UserID
Next
Loop
Close


Why isn't this working properly?? This code only removes the highlighted item in the list box, not the selected items.
I have tried usin List in the for statement instead of ListCount, and i still get the same result.
Also, this code makes repeated copies of the items not highlighted.
 
Dim i As Integer
Open "source.txt" For Input As #1
Open "target.txt" For Output As #2
Do While Not EOF(1)
Input #1, UserName, UserID
For i = 0 To NewUser.User.ListCount - 1
If NewUser.User.List(Recipients.ListIndex) = UserName Then
Exit For
End If Write #2, UserName, UserID
Next
Loop
Close

>>
Why isn't this working properly?? This code only removes the highlighted item in the list box, not the selected items.
<<
You have moved away from using the 'selected' property, and used the 'listindex' which only pertaisn to one row.

>>
Also, this code makes repeated copies of the items not highlighted.

<<

You have the Write #2 inside the loop you use to check the items in your listbox.


Am I correct in thinking that you have a listbox and it shows everyone in SOURCE.
You want to highlight a few and ensure that they get removed?

Perhaps a different approach:

When you fill the list, you can associate an additional value with each item, rather like a TAG property.
This data goes in the ITEMDATA array, one for each item in the list.

When you fill the listbox with item(n), fill the ITEMDATA(n) at the same time with the ID Number.

Now your listbox holds **all** the information about everyone.

If you want to remove someone, simply remove them from the listbox.
If you want to add someone, add them to the listbox.

After all your adding and removing is done, open the source file for writing, loop through the listbox and write ONLY the people in the listbox.
Close the file.

No need to try and match from the file to what is selected/visible etc.



 
I got it working after all. I used this:
Open "source.txt" For Input As #1
Open "target.txt" For Output As #2
Do While Not EOF(1)
For n = 0 To (User.ListCount - 1)
Input #1, UserName, UserID
If Not User.Selected(n) = True Then
Write #2, UserName, UserID
End If
Next
Loop
Close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top