Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

how to delete one line of text

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
hjaycox
Asked:
hjaycox
  • 2
  • 2
1 Solution
 
Anthony PerkinsCommented:
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
 
Anthony PerkinsCommented:
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
 
mcoopCommented:
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
 
mcoopCommented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now