Next & Prev Records Help !!! (and amend/edit help to actually)-VBForums
Results 1 to 6 of 6

Thread: Next & Prev Records Help !!! (and amend/edit help to actually)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    7

    Exclamation Next & Prev Records Help !!! (and amend/edit help to actually)

    Hi again,
    after the recent failure of my last post i thought wed forget about that and start fresh, this time i want to be able browse through all my records that have been written in and also be able to to edit the records that were stored,
    how do i go abouts doing this ????

    code :
    Public Class Qoutes
    Public Structure Account
    Dim CustomerID As Integer 'contains whole qoutes variables
    Dim Forname As String
    Dim surname As String
    Dim dob As Date
    Dim telephone As String
    Dim address As String
    Dim town As String
    Dim postcode As String
    Dim financeyes As Boolean '
    Dim financeno As Boolean
    Dim workyes As Boolean
    Dim workno As Boolean
    Dim salary As Integer
    Dim interestrate As Integer
    Dim carmake As String
    Dim carmodel As String
    Dim carcost As Integer
    Dim Totalqoute As Integer

    End Structure
    Dim Accounts() As Account

    Dim qoutesfile As String = "E:\Visual Studio 2010\Projects\!! ERROR !!\accounts.txt"

    Dim CurrentRecord As Integer = 1

    Dim Answer As Integer = 0
    Dim NumberofRecords As Integer = 0



    Private Sub btnaddqoute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddqoute.Click
    If NumberofRecords = 0 Then
    ReDim Accounts(1)
    Else
    ReDim Preserve Accounts(UBound(Accounts) + 1)
    End If

    NumberofRecords = NumberofRecords + 1
    ReDim Preserve Accounts(NumberofRecords)

    Accounts(CurrentRecord).Forname = txtforename.Text
    Accounts(CurrentRecord).surname = txtsurname.Text
    Accounts(CurrentRecord).dob = txtdob.Text
    Accounts(CurrentRecord).telephone = txttelephone.Text
    Accounts(CurrentRecord).address = txtaddress.Text
    Accounts(CurrentRecord).town = txttown.Text
    Accounts(CurrentRecord).postcode = txtpostcode.Text
    Accounts(CurrentRecord).salary = txtsalary.Text
    Accounts(CurrentRecord).interestrate = txtintrestrate.Text
    Accounts(CurrentRecord).carmake = txtcarmake.Text
    Accounts(CurrentRecord).carmodel = txtcarmodel.Text
    Accounts(CurrentRecord).carcost = txtcarcost.Text
    Accounts(CurrentRecord).Totalqoute = txttotalqoute.Text

    FileOpen(1, qoutesfile, OpenMode.Output)

    WriteLine(1, NumberofRecords)

    For Count = 1 To NumberofRecords
    WriteLine(1, Accounts(Count).CustomerID)
    WriteLine(1, Accounts(Count).Forname)
    WriteLine(1, Accounts(Count).surname)
    WriteLine(1, Accounts(Count).dob)
    WriteLine(1, Accounts(Count).telephone)
    WriteLine(1, Accounts(Count).address)
    WriteLine(1, Accounts(Count).town)
    WriteLine(1, Accounts(Count).postcode)
    WriteLine(1, Accounts(Count).salary)
    WriteLine(1, Accounts(Count).interestrate)
    WriteLine(1, Accounts(Count).carmake)
    WriteLine(1, Accounts(Count).carmodel)
    WriteLine(1, Accounts(Count).carcost)
    WriteLine(1, Accounts(Count).Totalqoute)


    Next
    FileClose(1)



    End Sub

    Private Sub btnqoutesback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnqoutesback.Click
    Me.Hide()
    MainMenu.Show()

    End Sub

    Private Sub Qoutes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btncalcqoute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalcqoute.Click
    Dim totalcost As Integer
    Dim monthlycost As Integer

    totalcost = txtcarcost.Text - txtdeposit.Text

    totalcost = ((txtcarcost.Text / 100) * 100 * txtintrestrate.Text)


    End Sub

    Private Sub btnammenddetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnammenddetail.Click


    End Sub

    Private Sub btnprevquote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprevquote.Click

    End Sub

    Private Sub btnnextquote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnextquote.Click

    End Sub
    End Class

    still new to this if you could give any suggestions that'd be great !!!

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    7

    Re: Next & Prev Records Help !!! (and amend/edit help to actually)



    this is the actual form

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,845

    Re: Next & Prev Records Help !!! (and amend/edit help to actually)

    First up, I suggest that you ditch that VB6 style of file I/O and use VB.NET as it was intended to be used. In general, if you want to write text to a file then use a StreamWriter. Likewise, if you want to read text from a file then use a StreamReader.

    That said, there are various other options that basically sit on top of a StreamReader and provide more simplicity or greater control. For instance, if you want to read an entire file into a single String then just call File.ReadAllText and call File.ReadAllLines to read the lines of a file into a String array. File.WriteAllText and File.WriteAllLines are the converse operations. If you want to read delimited data, e.g. a CSV file, then it's a good idea to use a TextFieldParser. The MSDN documentation for the class provides complete examples.

    In your case, I would suggest using a TextFieldParser to read the file, creating an instance of your Account type for each record. By the way, your Account type is too big to be a structure and should be a class.

    As you create them, you should add the Account objects to a collection, not an array. Collections are specifically designed to provide array-like behaviour but to also resize dynamically as items are added and removed. They are far more efficient than using ReDim Preserve repeatedly on an array. In your case you might use a List(Of Account).

    As for navigation, for that you should use a BindingSource. You can assign your collection to the DataSource of the BindingSource and then bind the BindingSource to each of your controls. You can then simply call MoveNext and MovePrevious on the BindingSource to move through the records. You may need a little bit of code on the CurrentChanged event of the BindingSource to handle controls that can't be bound, e.g. your RadioButtons. That's simple enough though.

    Finally, when posting code, please use the Code or VBCode buttons provided to wrap your snippets in formatting tags to make them comfortable to read.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    7

    Re: Next & Prev Records Help !!! (and amend/edit help to actually)

    Public Class Qoutes
    Public Structure Account
    Dim CustomerID As Integer
    Dim Forname As String
    Dim surname As String
    Dim dob As Date
    Dim telephone As String
    Dim address As String
    Dim town As String
    Dim postcode As String
    Dim financeyes As Boolean '
    Dim financeno As Boolean
    Dim workyes As Boolean
    Dim workno As Boolean
    Dim salary As Integer
    Dim interestrate As Integer
    Dim carmake As String
    Dim carmodel As String
    Dim carcost As Integer
    Dim Totalqoute As Integer
    Dim deposit As Integer
    Dim monthlyrate As Integer
    Dim carreg As String
    Dim monthstopay As Integer


    End Structure

    Public Accounts() As Account

    Public qoutesfile As String = "E:\Visual Studio 2010\Projects\!! ERROR !!\accounts.txt"

    Public NumberofRecords As Integer = 0

    Public CurrentRecord As Integer = 1

    Public Rate As Integer = 17.0

    Public InterestPayment As Decimal


    Public Sub Displaydata()

    qoutesfile = "E:\Visual Studio 2010\Projects\!! ERROR !!\accounts.txt"

    txtcustomerid.Text = Accounts(CurrentRecord).CustomerID
    txtforename.Text = Accounts(CurrentRecord).Forname
    txtsurname.Text = Accounts(CurrentRecord).surname
    txtdob.Text = Accounts(CurrentRecord).dob
    txttelephone.Text = Accounts(CurrentRecord).telephone
    txtaddress.Text = Accounts(CurrentRecord).address
    txttown.Text = Accounts(CurrentRecord).town
    txtpostcode.Text = Accounts(CurrentRecord).postcode
    txtsalary.Text = Accounts(CurrentRecord).salary
    txtcarmake.Text = Accounts(CurrentRecord).carmake
    txtcarmodel.Text = Accounts(CurrentRecord).carmodel
    txtcarcost.Text = Accounts(CurrentRecord).carcost
    txttotalquote.Text = Accounts(CurrentRecord).Totalqoute
    txtdeposit.Text = Accounts(CurrentRecord).deposit
    txtmonthlyrate.Text = Accounts(CurrentRecord).monthlyrate
    txtcarreg.Text = Accounts(CurrentRecord).carreg
    txtmonthstopay.Text = Accounts(CurrentRecord).monthstopay


    End Sub

    Private Sub Qoutes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    FileOpen(1, "E:\Visual Studio 2010\Projects\!! ERROR !!\accounts.txt", OpenMode.Input) 'Open file

    Input(1, NumberofRecords) 'Input first line to get number of records

    ReDim Accounts(NumberofRecords) 'Make array big enough to hold records

    For Count = 1 To NumberofRecords 'Fill each field of record with required info from file

    Input(1, Accounts(Count).CustomerID)
    Input(1, Accounts(Count).Forname)
    Input(1, Accounts(Count).surname)
    Input(1, Accounts(Count).dob)
    Input(1, Accounts(Count).telephone)
    Input(1, Accounts(Count).address)
    Input(1, Accounts(Count).town)
    Input(1, Accounts(Count).postcode)
    Input(1, Accounts(Count).salary)
    Input(1, Accounts(Count).interestrate)
    Input(1, Accounts(Count).carmake)
    Input(1, Accounts(Count).carmodel)
    Input(1, Accounts(Count).carcost)
    Input(1, Accounts(Count).carreg)
    Input(1, Accounts(Count).deposit)
    Input(1, Accounts(Count).Totalqoute)
    Input(1, Accounts(Count).monthlyrate)
    Input(1, Accounts(Count).monthstopay)
    Next

    FileClose(1) 'Close file'

    Displaydata()
    End Sub

    Private Sub btnaddqoute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddqoute.Click
    If NumberofRecords = 0 Then
    ReDim Accounts(1)
    Else
    ReDim Preserve Accounts(UBound(Accounts) + 1)
    End If

    NumberofRecords = NumberofRecords + 1
    ReDim Preserve Accounts(NumberofRecords)

    FileOpen(1, "E:\Visual Studio 2010\Projects\!! ERROR !!\accounts.txt", OpenMode.Input)

    Accounts(CurrentRecord).Forname = txtforename.Text
    Accounts(CurrentRecord).surname = txtsurname.Text
    Accounts(CurrentRecord).dob = txtdob.Text
    Accounts(CurrentRecord).telephone = txttelephone.Text
    Accounts(CurrentRecord).address = txtaddress.Text
    Accounts(CurrentRecord).town = txttown.Text
    Accounts(CurrentRecord).postcode = txtpostcode.Text
    Accounts(CurrentRecord).salary = txtsalary.Text
    Accounts(CurrentRecord).interestrate = txtintrestrate.Text
    Accounts(CurrentRecord).carmake = txtcarmake.Text
    Accounts(CurrentRecord).carmodel = txtcarmodel.Text
    Accounts(CurrentRecord).carcost = txtcarcost.Text
    Accounts(CurrentRecord).carreg = txtcarreg.Text
    Accounts(CurrentRecord).deposit = txtdeposit.Text
    Accounts(CurrentRecord).monthlyrate = txtmonthlyrate.Text
    Accounts(CurrentRecord).monthstopay = txtmonthstopay.Text
    Accounts(CurrentRecord).Totalqoute = txttotalquote.Text

    FileOpen(1, qoutesfile, OpenMode.Output)

    WriteLine(1, NumberofRecords)

    For Count = 1 To NumberofRecords
    WriteLine(1, Accounts(Count).CustomerID)
    WriteLine(1, Accounts(Count).Forname)
    WriteLine(1, Accounts(Count).surname)
    WriteLine(1, Accounts(Count).dob)
    WriteLine(1, Accounts(Count).telephone)
    WriteLine(1, Accounts(Count).address)
    WriteLine(1, Accounts(Count).town)
    WriteLine(1, Accounts(Count).postcode)
    WriteLine(1, Accounts(Count).salary)
    WriteLine(1, Accounts(Count).interestrate)
    WriteLine(1, Accounts(Count).carmake)
    WriteLine(1, Accounts(Count).carmodel)
    WriteLine(1, Accounts(Count).carcost)
    WriteLine(1, Accounts(Count).carreg)
    WriteLine(1, Accounts(Count).deposit)

    WriteLine(1, Accounts(Count).monthlyrate)
    WriteLine(1, Accounts(Count).monthstopay)
    WriteLine(1, Accounts(Count).Totalqoute)

    Next
    FileClose(1)

    CurrentRecord = NumberofRecords

    Displaydata()
    MsgBox("Data Has Been Saved", MsgBoxStyle.Information, "Quick Cars Qoute")

    End Sub



    Private Sub btnammenddetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnammenddetail.Click


    End Sub

    Private Sub btnprevquote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprevquote.Click

    If CurrentRecord = 1 Then
    CurrentRecord = NumberofRecords
    Else
    CurrentRecord = CurrentRecord - 1
    End If

    Displaydata()
    End Sub

    Private Sub btnnextquote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnextquote.Click

    If CurrentRecord = NumberofRecords Then
    CurrentRecord = 1
    Else
    CurrentRecord = CurrentRecord + 1
    End If

    Displaydata()
    End Sub
    End Class

    still a total beginner so......
    will deleting the current records array then saving it over in one routine work ????

  5. #5
    Addicted Member
    Join Date
    Jan 2011
    Posts
    139

    Re: Next & Prev Records Help !!! (and amend/edit help to actually)

    Please use "Quote tags" in the future.
    Im guessing that this is for college - if so, then i might have my old coursework left over. Ill go and look now

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    7

    Re: Next & Prev Records Help !!! (and amend/edit help to actually)

    i did use the code tags and on editing it is all correctly formatted, for some reason its not showing up in the post ???? dunno anyway, if you could thatd be great mate, i have idiots code block :P cant think how to ammend a record at all , i got the next and previous buttons to work by

    prev button Code:
    1. Private Sub btnprevquote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprevquote.Click
    2.  
    3.         If CurrentRecord = 1 Then
    4.             CurrentRecord = NumberofRecords
    5.         Else
    6.             CurrentRecord = CurrentRecord - 1
    7.         End If
    8.  
    9.         Displaydata()
    10.     End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.