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

x
?
Solved

how to delete one line of text

Posted on 2002-05-24
4
Medium Priority
?
147 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
[X]
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
  • 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 800 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

705 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