Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21089

Creating Editing And Deleting Records from a Text file

$
0
0
Hey Guys I'm new to vb more or less, so how about a hand? I have an application that manages different sets of data using text files as the data bank. I have everything set up but my buttons half way
work. My save button will only save a new record in the text file if it is new, but i need to be able to edit records all ready entered. This is what i had attempted to do.
Code:

    Private Sub btnEmployeesSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployeesSave.Click
        Dim NoGo As Long

        NoGo = 0
        If newFlag Then 'if new
            If Trim(txtEID.Text) = "" Then
                MsgBox("You Can not leave EID blank")
                NoGo = 1
                txtEID.Focus()
            End If
            If Trim(txtFName.Text) = "" Then
                MsgBox("You Can not leave First Name blank")
                NoGo = 1
                txtFName.Focus()
            End If
            If Trim(txtLName.Text) = "" Then
                MsgBox("You Can not leave Last Name blank")
                NoGo = 1
                txtLName.Focus()
            End If


            If NoGo = 1 Then 'do nothing
            Else
                outFile = New StreamWriter("Employees.txt", True) 'pin to end of file
                strOut = txtEID.Text & ", " & txtFName.Text & ", " & txtLName.Text & ", " & txtEmail.Text & ", " & txtPNumber.Text _
                & ", " & txtMPNumber.Text & ", " & txtAdd.Text & ", " & txtCity.Text & ", " & txtState.Text & ", " & txtZip.Text
                outFile.WriteLine(strOut) 'write out string
                outFile.Close()
                getRecordCount()
                lngCurrentRecord = lngRecCount
            End If


            If newFlag Then 'this is a new record
                'do new stuff
            Else    'update and existing record
                If File.Exists("Employees.wrk") Then File.Delete("Employees.wrk")
                inFile = New StreamReader("Employees.txt")
                outFile = New StreamWriter("Employees.wrk", False)

                'fill work file up to this record
                For intx = 1 To lngCurrentRecord - 1
                    strLine = inFile.ReadLine
                    outFile.WriteLine(strLine)
                Next
                'read the current record to the bit bucket
                strLine = inFile.ReadLine
                'insert this changed record
                strLine = txtEID.Text & ", " & txtFName.Text & ", " & txtLName.Text & ", " & txtEmail.Text & ", " & txtPNumber.Text _
                & ", " & txtMPNumber.Text & ", " & txtAdd.Text & ", " & txtCity.Text & ", " & txtState.Text & ", " & txtZip.Text
                outFile.WriteLine(strLine)
                'write the remain records to the work file
                If lngCurrentRecord + 1 < lngRecCount Then
                    For intx = lngCurrentRecord + 1 To lngRecCount
                        strLine = inFile.ReadLine
                        outFile.WriteLine(strLine)
                    Next
                End If
                inFile.Close()
                outFile.Close()
                'write the work file back to the original
                inFile = New StreamReader("Employees.wrk")
                outFile = New StreamWriter("Employees.txt", False)
                For intx = 1 To lngRecCount
                    strLine = inFile.ReadLine
                    outFile.WriteLine(strLine)
                Next
                newFlag = False
                inFile.Close()
                outFile.Close()
                File.Replace("Employees.wrk", "Employees.txt", "Employees.bak")
            End If

        End If

    End Sub


Viewing all articles
Browse latest Browse all 21089

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>