Solved

edit a notepad file

Posted on 2003-11-25
6
1,732 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
[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
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
Industry Leaders: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2013 combo box not working 3 74
VB6 ListBox Question 4 62
VBA: copy range dynamically based on config sheet v2 3 60
Collapse and expand table in Word 2010 2 159
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

734 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