Solved

edit a notepad file

Posted on 2003-11-25
6
1,729 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
ID: 9819638
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
ID: 9819647
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
ID: 9819678
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

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

Expert Comment

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

Author Comment

by:stevenlewis
ID: 9828607
worked great!
tried the others, but I'm not good enough to get them to work LOL
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

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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

685 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