Solved

how to delete one line of text

Posted on 2002-05-24
4
143 Views
Last Modified: 2010-05-02
I have a program that records an invoice. I need to have code to delete one line based upon the first field of the line. That is , the invoice number.
example:

"114778  ","4.15     ","160.00 ","8      ","dj                  ","   "
"114597  ","4.12     ","187.50 ","9.375  ","kachina             ","   "
[["115170  ","4.12     ","180.00 ","9      ","kachiina            ","   "]]
"115290  ","4.17     ","191.25 ","9.5625 ","kachina             ","   "
"115429  ","4.24     ","240.00 ","12     ","kachina             ","   "
"115228  ","4.12     ","78.75  ","3.9375 ","kales               ","   "
"114973  ","4.3      ","150.00 ","7.5    ","sup shut            ","   "

I want to delete iinvoice number [["115170..."]] and all data in that line and leave the other lines alone.
 
The line to delete is :

[["115170  ","4.12     ","180.00 ","9      ","kachiina            ","   "]]

This program is written as a sequencial data in vb4
0
Comment
Question by:hjaycox
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7033848
You will have to read the whole file and write it out to a new file, skipping the invoice you wish to delete.  If you need code let me know.

Anthony
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 200 total points
ID: 7033861
If we assume that the invoice file is in a folder called Invoices and is called Invoice.txt then here is the code:

Sub DeleteInvoice(ByVal DeleteInvoiceNumber As String)
Const InvoiceFileName = "c:\Invoices\Invoice.txt"
Const TempFileName = "c:\Invoices\Temp.txt"
Dim InHandle As Integer
Dim OutHandle As Integer
Dim InvoiceNumber As String
Dim a As String, b As String, c As String, d As String, e As String

InHandle = FreeFile
Open InvoiceFileName For Input As #InHandle

OutHandle = FreeFile
Open TempFileName For Output As #OutHandle

Do While Not EOF(InHandle)
   Input #InHandle, InvoiceNumber, a, b, c, d, e
   If InvoiceNumber <> DeleteInvoiceNumber Then
      Write #OutHandle, InvoiceNumber, a, b, c, d, e
   End If
Loop
Close InHandle
Close OutHandle
Kill InvoiceFileName
Name TempFileName As InvoiceFileName

End Sub

You would call it as follows (notice that I am spelling it exactly as in the file, you can trim it or convert it to a number to avoid this):

DeleteInvoice "115170  "

Anthony
0
 
LVL 4

Expert Comment

by:mcoop
ID: 7034064
Of course you could just 'clear' the line, and that would save reprocessing the whole file until later (end of day/week/mnth), when you can do a cleanup to compact the file (remove the blank lines).

This assumes your program will handle blank lines gracefully - you may need to do a little extra coding, but the end result is simpler than recompacting the file each time...

If your code is tidy enough, you can also go back and re-use the empty 'records' when adding new lines - or-

To be even more sophisticated - perhaps you can negate the invoice number ( invNum = 0-invnum ), to indicate that the record is no longer 'active'.  This allows you to see 'deleted records' after they have been posted - and undo the deletion if necessary (restore the number to it's original value ( invNum = abs(invNum) ).

Carry on !
0
 
LVL 4

Expert Comment

by:mcoop
ID: 7034069
Also - I the occurrence of embedded 'white space' in variable values is not good form...  you may prefer to pack these fields for consistency.

e.g. (shown as XXX vs original text record above)

115170  ","4.12XXXXX","180.00X","9XXXXXX","kachiinaXXXXXXXXXXXX","  
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now