I am hoping someone can help me with this. I am trying to figure out how to update data in a csv file with changes I make in a userform. I use one form to create the csv file, the code looks like this:
I have an identical userform that retrieves the data, the code looks like this:
I want to be able to change a record in one of those text boxes and have it save the change to the csv file. For example, if an agent's BidRank changes I want to update the existing record with the new BidRank. I tried this:
and it looks like it works (i.e. it pops no errors) but when I check the csv file it still has the old data. Any suggestions? Also, I am using Visual Studio.net 2008, forgot to mention this originally.
Code:
Dim out As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("Agents.csv", True)
out.WriteLine(txtAgID.Text & "," & txtFName.Text & "," & txtLName.Text & "," & txtHDate.Text & "," & txtTDate.Text & "," & txtSup.Text & "," & txtShift.Text & "," & txtBRank.Text)
out.Close()
Code:
Dim myLine As String
myLine = txtAgID.Text
Dim sr As System.IO.StreamReader
sr = System.IO.File.OpenText("Agents.csv")
sr.ReadLine()
Dim values As String()
While sr.Peek() >= 0
values = sr.ReadLine().Split(",")
If values(0) = myLine Then
txtFName.Text = values(1)
txtLName.Text = values(2)
txtHDate.Text = values(3)
txtTDate.Text = values(4)
txtSup.Text = values(5)
txtShift.Text = values(6)
txtBRank.Text = values(7)
Exit While
Else
lblStatus.Text = "Agent is not found in the database"
End If
End While
lblStatus.Text = "Record Located"
sr.Close()
Code:
Dim myLine As String
myLine = txtAgID.Text
Dim sr As System.IO.StreamReader
sr = System.IO.File.OpenText("Agents.csv")
sr.ReadLine()
Dim values As String()
While sr.Peek() >= 0
values = sr.ReadLine().Split(",")
If values(0) = myLine Then
values(1) = txtFName.Text
values(2) = txtLName.Text
values(3) = txtHDate.Text
values(4) = txtTDate.Text
values(5) = txtSup.Text
values(6) = txtShift.Text
values(7) = txtBRank.Text
Exit While
Else
lblStatus.Text = "Agent is not found in the database"
End If
End While
lblStatus.Text = "Record updated"
sr.Close()
Comment