Solved

edit a notepad file

Posted on 2003-11-25
6
1,726 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

832 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