Deleting Row from Data File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MiziaQ
    New Member
    • Nov 2007
    • 63

    Deleting Row from Data File

    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.tx t" For Append As #11
    Write #11, lblAccNum.Capti on, txtPName.Text, txtPAddress1.Te xt & ", " & txtPAddress2.Te xt & ", " & cboPTown.Text, cboPPrefix.Text & " " & txtPTelephone.T ext & ", " & cboPMPrefix.Tex t & " " & txtPMobile.Text , txtDName.Text, txtDAddress1.Te xt & ", " & txtDAddress2.Te xt & ", " & cboDTown.Text, cboDPrefix.Text & " " & txtDTelephone.T ext & ", " & cboDMPrefix.Tex t & " " & txtDMobile.Text , txtWeightOfPack age.Text, txtDeliveryPric e.Text, txtPickupDate.T ext, txtDeliveryDate .Text, lblPriorityLeve l.Caption

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

    txtPName.Text = ""
    txtPAddress1.Te xt = ""
    txtPAddress2.Te xt = ""
    cboPTown.Text = ""
    cboPPrefix.Text = "021"
    txtPTelephone.T ext = ""
    cboPMPrefix.Tex t = "085"
    txtPMobile.Text = ""

    txtDName.Text = ""
    txtDAddress1.Te xt = ""
    txtDAddress2.Te xt = ""
    cboDTown.Text = ""
    cboDPrefix.Text = "021"
    txtDTelephone.T ext = ""
    cboDMPrefix.Tex t = "085"
    txtDMobile.Text = ""
    txtWeightOfPack age.Text = ""
    txtDeliveryPric e.Text = ""
    txtPickupDate.T ext = ""
    txtDeliveryDate .Text = ""

    txtPName.SetFoc us

    Close #11

    End Sub

    Private Sub tabAdmin_Click( PreviousTab As Integer)

    If tabAdmin.Tab = 1 Then

    Open App.Path & "\deliveries.tx t" 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.Row s = 2

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

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

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

    Loop

    End If

    Close #12

    End Sub
  • VBWheaties
    New Member
    • Feb 2008
    • 145

    #2
    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.

    Comment

    • MiziaQ
      New Member
      • Nov 2007
      • 63

      #3
      How can I do that ? Could you please give a coded example ? Thanks

      Comment

      • VBWheaties
        New Member
        • Feb 2008
        • 145

        #4
        Originally posted by MiziaQ
        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.

        Comment

        • MiziaQ
          New Member
          • Nov 2007
          • 63

          #5
          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 !

          Comment

          Working...