Solved

how to delete one line of text

Posted on 2002-05-24
4
141 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA filters 2 50
How to make an ADE file by code? 11 79
Using an encrypted  value to decrypt and display contents in vb6 9 49
vbModal 12 31
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 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

20 Experts available now in Live!

Get 1:1 Help Now