By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

Deleting Row from Data File

P: 63
Hey, I'm using the following code to write entries to a data file and then read them in an msflexgrid. I now would like to add code under a delete button to use the table(grid) to delete rows from the file. How can I do that. I have some code for deleting a row from the table, but it's only deleting the row from the table, and not the entry itself. Please help! Is there a relatively easy way to do this ?

Private Sub cmdAddDelivery_Click()

Open App.Path & "\deliveries.txt" For Append As #11
Write #11, lblAccNum.Caption, txtPName.Text, txtPAddress1.Text & ", " & txtPAddress2.Text & ", " & cboPTown.Text, cboPPrefix.Text & " " & txtPTelephone.Text & ", " & cboPMPrefix.Text & " " & txtPMobile.Text, txtDName.Text, txtDAddress1.Text & ", " & txtDAddress2.Text & ", " & cboDTown.Text, cboDPrefix.Text & " " & txtDTelephone.Text & ", " & cboDMPrefix.Text & " " & txtDMobile.Text, txtWeightOfPackage.Text, txtDeliveryPrice.Text, txtPickupDate.Text, txtDeliveryDate.Text, lblPriorityLevel.Caption

MsgBox "New Delivery has been Added !", vbInformation, "Success!!!"

txtPName.Text = ""
txtPAddress1.Text = ""
txtPAddress2.Text = ""
cboPTown.Text = ""
cboPPrefix.Text = "021"
txtPTelephone.Text = ""
cboPMPrefix.Text = "085"
txtPMobile.Text = ""

txtDName.Text = ""
txtDAddress1.Text = ""
txtDAddress2.Text = ""
cboDTown.Text = ""
cboDPrefix.Text = "021"
txtDTelephone.Text = ""
cboDMPrefix.Text = "085"
txtDMobile.Text = ""
txtWeightOfPackage.Text = ""
txtDeliveryPrice.Text = ""
txtPickupDate.Text = ""
txtDeliveryDate.Text = ""

txtPName.SetFocus

Close #11

End Sub

Private Sub tabAdmin_Click(PreviousTab As Integer)

If tabAdmin.Tab = 1 Then

Open App.Path & "\deliveries.txt" For Input As #12

Dim oldRow As Long
Dim lngRow As Long
Dim lngCol As Long

Dim tRow As Long
Dim tCol As Long

Dim strID As String
Dim strPickupName As String
Dim strPickupAdd As String
Dim strPickupTel As String
Dim strDelName As String
Dim strDelAdd As String
Dim strDelTel As String
Dim strWoP As String
Dim strDelPrice As String
Dim strPickupDate As String
Dim strDelDate As String
Dim strPriority As String
Dim counter As Integer


MSFlexGrid1.Rows = 2

Do Until EOF(12)
Input #12, strID, strPickupName, strPickupAdd, strPickupTel, strDelName, strDelAdd, strDelTel, strWoP, strDelPrice, strPickupDate, strDelDate, strPriority

MSFlexGrid1.TextMatrix(1, 0) = strID
MSFlexGrid1.TextMatrix(1, 1) = strPickupName
MSFlexGrid1.TextMatrix(1, 2) = strPickupAdd
MSFlexGrid1.TextMatrix(1, 3) = strPickupTel
MSFlexGrid1.TextMatrix(1, 4) = strDelName
MSFlexGrid1.TextMatrix(1, 5) = strDelAdd
MSFlexGrid1.TextMatrix(1, 6) = strDelTel
MSFlexGrid1.TextMatrix(1, 7) = strWoP
MSFlexGrid1.TextMatrix(1, 8) = strDelPrice
MSFlexGrid1.TextMatrix(1, 9) = strPickupDate
MSFlexGrid1.TextMatrix(1, 10) = strDelDate
MSFlexGrid1.TextMatrix(1, 11) = strPriority

MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
For lngRow = MSFlexGrid1.Rows - 2 To MSFlexGrid1.Row Step -1
For lngCol = 0 To MSFlexGrid1.Cols - 1
MSFlexGrid1.TextMatrix(lngRow + 1, lngCol) = MSFlexGrid1.TextMatrix(lngRow, lngCol)
If lngRow = MSFlexGrid1.Row Then
MSFlexGrid1.TextMatrix(lngRow, lngCol) = "" 'Make the current row empty
End If
Next
Next

Loop

End If

Close #12

End Sub
Feb 27 '08 #1
Share this Question
Share on Google+
4 Replies


VBWheaties
100+
P: 145
My approach would be to write a routine that reads values off the grid into a temp file. Once its done reading values off the grid, save the Temp file as the primary file.
Feb 27 '08 #2

P: 63
How can I do that ? Could you please give a coded example ? Thanks
Feb 27 '08 #3

VBWheaties
100+
P: 145
How can I do that ? Could you please give a coded example ? Thanks
It's real easy to do this.
I see you already know how to reference TextMatrix and you know how to open/close files. So basically

1. Create a sub (call it WriteGridToFile or something)
2. Open any arbitrary file giving a temp file name. Open for writing (or output)
2. In a loop, enumerate the rows of the grid.
3. For each row, read the TextMatrix values into variables, or simply write to the file directly using TextMatrix. Your choice.
4. Once done looping the grid rows, writing the values to file, close the file.
5. Save the Temp file as your primary source file, overwriting the primary file.
6. Call WriteGridToFile whenever you need to persist (save) changes.

Let me know if you need help on anything else.
Feb 27 '08 #4

P: 63
Hey :) Thanks a lot for your help. I have one last thing to do, and that is to replace the files, which I don't know how to do. Once again, thanks !
Feb 27 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.