Solved

edit a notepad file

Posted on 2003-11-25
6
1,722 Views
Last Modified: 2010-05-01
OK, I have a file, named daily.dat
it's a list of times
0000AM,Sample Entries,0
0615AM,Sample Entries,0
1145AM,Sample Entries,0
1157AM,Add details here.,0
I can open it and populate my combo box (dropdown)
it reads
0000am
0615am
1145am
1157am
now I want to delete a value (the one highlighted in the dropdown box
and then move the others up
example if I have 0615  in the dropdown box
then the edited notepad file will read
0000AM,Sample Entries,0
1145AM,Sample Entries,0
1157AM,Add details here.,0

this is how I am opening the notepad file (daily.dat)
Private Sub Form_Load()
Dim intCounta As Integer
Dim intColumna As Integer
Dim myFile As Integer
Dim ControlName As ComboBox
Dim data As String
Dim x As Long
Dim PathName As String
Dim mydata As String

mydata = Left(mydata, 6)
PathName = App.Path & "\daily.dat"
                             

                               
    Set ControlName = cboDaily
    mydata = FillCombo(ControlName, PathName)

 
End Sub
Function FillCombo(ComboName As Object, PathName As String)

Dim mydata As String



                             

    Open PathName For Input As #1
      Do While Not EOF(1)
        Line Input #1, mydata
        mydata = Left(mydata, 6)
        ComboName.AddItem mydata
Loop
    cboDaily.ListIndex = 0
Close 1
End Function

TIA again
Steve
0
Comment
Question by:stevenlewis
6 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 250 total points
Comment Utility
You could create a sub-routine to read the lines from the file into an array(except the one you want to remove) then write the array to the file,then call your FillCombo routine to repopulate the Combo.Maybe something like this:

Private Sub Command1_Click()
RemoveItemFromFile cboDaily.Text, App.Path & "\daily.dat"
End Sub


Private Sub RemoveItemFromFile(ComboSelect As String, FileName As String)
Dim Ln As String, arrLn() As String
Dim ff As Integer, n As Integer
n = 0
ff = FreeFile
Open FileName For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
If Left$(Ln, 6) <> ComboSelect Then
    n = n + 1
    ReDim Preserve arrLn(n)
    arrLn(n) = Ln
End If
Loop
Close #ff

ff = FreeFile
Open FileName For Output As #ff
For n = 1 To UBound(arrLn)
Print #ff, arrLn(n)
Next n
Close #ff

cboDaily.Clear
FillCombo cboDaily, FileName

End Sub
0
 
LVL 27

Expert Comment

by:planocz
Comment Utility
Hi stevenlewis,
There is alot of ways to do  what you are talking about.
One way is to have a ID number at the beginning of each line in your daily.dat
Example : 1,0000AM,Sample Entries,0
               2,0000AM,Sample Entries,0
               3,0000AM,Sample Entries,0
then when you delete a line from your combobox you have a var that is holding the ID number that is to be removed
from your daily.dat.
Now you have to create a new TEMPdaily.dat file to hold the new data.
You then reopen your orignal daily.dat file and read each line into a new TEMPdaily.dat file.
Checking for the ID number. You will then skip over the deleted ID number and read the next line until you are finished (EOFinto  your new TEMPdaily.dat file. Then you can rename your new file back to the orig. file (daily.dat).
0
 
LVL 3

Expert Comment

by:magnus23
Comment Utility
HI stevenlewis
You cannot delete line from file. What you have to do is read entire content of a file into a variable, find a string that should be deleted, delete entire line until(including) Carrage return and Line feed chars (const vbCrLf) and rewrite then file.

dim temp as string
Do While Not EOF(1)
        Line Input #1, mydata
        if Left(mydata, 6)<>ComboName.SelectedItem.Text
             temp=temp & vbCrLf & mydata
        End IF
Loop


Now just write temp variable to a file.

Good Luck.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
I like a mix of planocz and magnus123 comments.
0
 
LVL 3

Expert Comment

by:magnus23
Comment Utility
When i was writing mine i didn't sow others yet :)
0
 
LVL 41

Author Comment

by:stevenlewis
Comment Utility
worked great!
tried the others, but I'm not good enough to get them to work LOL
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 …
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…

743 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

18 Experts available now in Live!

Get 1:1 Help Now