0

My code is made up of saving records to files and reading those records, the structure of my files are made up of modules, this code is from visual basic 2010. I'm having a hard time understanding why this particular part of my code doesn't work, I'm new to coding and this website so if you have any other questions please let me know.

 Private Sub btnupdatestaff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdatestaff.Click
        Dim NumberOfrecords As Integer
        Dim OneStaff As Staff
        Dim StaffFile As String
        StaffFile = "Staff.txt"
 
        If OneStaff.StaffID = searchstaffid.Text Then
            OneStaff.fname = fname.Text
            OneStaff.StaffID = txtstaffid.Text
            OneStaff.phonenum = phonenum.Text
            OneStaff.email = email.Text
            OneStaff.username = username.Text
            OneStaff.password = password.Text
            OneStaff.deleted = "N"
            FileOpen(1, StaffFile, OpenMode.Random, , , Len(OneStaff)) 'open the staff file for one staff'
            NumberOfrecords = LOF(1) / Len(OneStaff) 'work out the number of records in the file'
            FilePut(1, OneStaff, NumberOfrecords + 1) 'add the new staff to the end of the file'
            FileClose(1) 'close the file'
        Else

            MsgBox("", MsgBoxStyle.Information, "Error")

        End If

    End Sub
                
  • Define "doesn't work". – Lajos Arpad Mar 13 '22 at 12:28
  • @LajosArpad So what happens is it retrieves the data and puts in the textboxs, I edit the data press the update button and view the file again and the data I thought I edited does not change, no error messages pop up btw. – Ziyad Abdul sattar Mar 13 '22 at 12:35
  • To check strings are not equal in VB.NET, use the `<>` operator: `fname.Text <> ""`, etc. – Andrew Morton Mar 13 '22 at 18:38
  • Make sure to set [`Option Strict On`](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement) for this project—and set it as the default for new projects—so the Visual Studio can point out mistakes like `If OneStaff.StaffID...` when `OneStaff` has not been set to a value. – Andrew Morton Mar 13 '22 at 18:45
  • You should know about [What is the difference between And and AndAlso in VB.NET?](https://stackoverflow.com/questions/302047/what-is-the-difference-between-and-and-andalso-in-vb-net), and the same holds for `Or` vs. `OrElse`. – Andrew Morton Mar 13 '22 at 18:47
  • I would say the place to start with is a bit of debugging. Set at breakpoint at the start of your if statement and ensure it's actually executing as expected. There are 3 conditions in there that don't do anything, perhaps you unexpectedly falling into one of those and the file writing code is never being executed – Hursey Mar 13 '22 at 22:00
  • Thank you for your suggestions, I've deleted the validation checks that make sure that the data inputted is correct to make the code simpler for you guys to see and btw when I tried updating the data that was already in the text boxs this time it came up with the error message box. @AndrewMorton you said that onestaff has not been set a vaule, I've got warnings on this in my code, could you please explain as to why this is a problem I've looked in the internet but cant find an answer. – Ziyad Abdul sattar Mar 14 '22 at 17:03
  • @Hursey ok so I've deleted the validation checks and tried once more to update the file but now after doing that the message box pops up to indicate there's an error. – Ziyad Abdul sattar Mar 14 '22 at 17:07
  • Just to let you guys know this is a slightly modified version of my "add Record" code, not sure if that helps but just wanted to let you know In case I'm going about this the wrong way. – Ziyad Abdul sattar Mar 14 '22 at 17:50
  • @ZiyadAbdulsattar If `OneStaff` is `Nothing` (which it is) then `OneStaff.StaffID = searchstaffid.Text` will always be `False` (if it doesn't throw an exception). – Andrew Morton Mar 14 '22 at 19:57

0 Answers0