• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Saving/Loading Data Record

I have the following datarecord

Private Type DataRecord
    ID As Long
    Name As String
end type

Dim Clients(10) As DataRecord

'Add DataRecord

I need to be able to save/load the data in Clients quickly (200+/minute) with minimul CPU usage.  Clients is typically less than 10 total records.  Each record has about 10 fields.  Please provide sample code for BOTH SAVE AND LOAD.  Thanks.
  • 2
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is a PAQ where I show how to read/write a Type to a Random access file:

In both the Get and Put statement, you can specify the record number to read:
    Get #1, 1, PupilString ' Read 1st record
    Put #1, 5, PupilString ' update 5th record
In the example above, I omit the record number which causes read writes to begin at the beginning and move sequentially.


A couple of questions...

1. How secure does it need to be?  e.g.: can the data file be a text file, such as a csv
2. Are there the same no. of fields for each client and does each field have a particular fixed length?
3. Is using a dedicated database, such as Access or MSDE, to store clients out of the question?
adorenbaumAuthor Commented:

Thanks for your post.  In answer to your questions:

1.  not an issue, can be CSV, Text
2.  same number of fields.  Length of client name is a string of 3 characters, ID is a double (on any length)
3.  Access is an option however I would like to keep the project simple and the load(read)/save(write) function must be fast and cannot use up much CPU resources.

Best regards.
The following is a simple sequential text reader/writer I mocked up in Excel/VBA and its performance results.  If you are looking for simplicity, with reasonable speed, this will probably do.

I do think an adaptation of Idle_Mind's code would be cleaner/better/use less resources -- just not quite so simple.

The below code reads 20,000 records into memory from an Excel sheet, saves it to CSV, clears the memory, loads the CSV and writes it back to a sheet:


Type record
    ID          As Long
    Name        As String * 50
    Address1    As String * 128
    Address2    As String * 128
    Town        As String * 64
    County      As String * 64
    PostCode    As String * 16
    Telephone   As String * 16
    ProdCode    As String * 16
    Quantity    As Integer
End Type

Global Records(1 To 20000) As record

Sub DoIt()

    Call loadfromsheet
    Call savetocsv
    Call clearrecords
    Call loadfromcsv
    Call writetosheet

End Sub

Sub loadfromsheet()

    Sheets("Sheet1").Cells(2, 1) = Time
    lr = 1
    Do Until Sheets("Sheet2").Cells(lr, 1).Value = Empty Or lr > UBound(Records)
        Records(lr).ID = Sheets("Sheet2").Cells(lr, 1).Value
        Records(lr).Name = Sheets("Sheet2").Cells(lr, 2).Value
        Records(lr).Address1 = Sheets("Sheet2").Cells(lr, 3).Value
        Records(lr).Address2 = Sheets("Sheet2").Cells(lr, 4).Value
        Records(lr).Town = Sheets("Sheet2").Cells(lr, 5).Value
        Records(lr).County = Sheets("Sheet2").Cells(lr, 6).Value
        Records(lr).PostCode = Sheets("Sheet2").Cells(lr, 7).Value
        Records(lr).Telephone = Sheets("Sheet2").Cells(lr, 8).Value
        Records(lr).ProdCode = Sheets("Sheet2").Cells(lr, 9).Value
        Records(lr).Quantity = Sheets("Sheet2").Cells(lr, 10).Value
        lr = lr + 1
    Sheets("Sheet1").Cells(3, 1) = Time

End Sub

Sub savetocsv()

    Open InputBox("Enter file") For Output As #1
    Sheets("Sheet1").Cells(2, 2).Value = Time
        For lr = LBound(Records()) To UBound(Records())
            Print #1, _
                        Records(lr).ID & "," & _
                        Records(lr).Name & "," & _
                        Records(lr).Address1 & "," & _
                        Records(lr).Address2 & "," & _
                        Records(lr).Town & "," & _
                        Records(lr).County & "," & _
                        Records(lr).PostCode & "," & _
                        Records(lr).Telephone & "," & _
                        Records(lr).ProdCode & "," & _
        Next lr
    Close #1
    Sheets("Sheet1").Cells(3, 2).Value = Time

End Sub

Sub clearrecords()

        For lr = LBound(Records()) To UBound(Records())
            With Records(lr)
                .ID = 0
                .Name = ""
                .Address1 = ""
                .Address2 = ""
                .Town = ""
                .County = ""
                .PostCode = ""
                .Telephone = ""
                .ProdCode = ""
                .Quantity = 0
            End With
        Next lr

End Sub

Sub loadfromcsv()

    Open InputBox("Enter file") For Input As #1
        Sheets("Sheet1").Cells(2, 3).Value = Time
        For lr = LBound(Records()) To UBound(Records())
            Input #1, _
                        Records(lr).ID, _
                        Records(lr).Name, _
                        Records(lr).Address1, _
                        Records(lr).Address2, _
                        Records(lr).Town, _
                        Records(lr).County, _
                        Records(lr).PostCode, _
                        Records(lr).Telephone, _
                        Records(lr).ProdCode, _
        Next lr
    Close #1
    Sheets("Sheet1").Cells(3, 3).Value = Time

End Sub

Sub writetosheet()

    Sheets("Sheet1").Cells(2, 4) = Time
    For lr = LBound(Records()) To UBound(Records())
        Sheets("Sheet3").Cells(lr, 1).Value = Records(lr).ID
        Sheets("Sheet3").Cells(lr, 2).Value = Records(lr).Name
        Sheets("Sheet3").Cells(lr, 3).Value = Records(lr).Address1
        Sheets("Sheet3").Cells(lr, 4).Value = Records(lr).Address2
        Sheets("Sheet3").Cells(lr, 5).Value = Records(lr).Town
        Sheets("Sheet3").Cells(lr, 6).Value = Records(lr).County
        Sheets("Sheet3").Cells(lr, 7).Value = Records(lr).PostCode
        Sheets("Sheet3").Cells(lr, 8).Value = Records(lr).Telephone
        Sheets("Sheet3").Cells(lr, 9).Value = Records(lr).ProdCode
        Sheets("Sheet3").Cells(lr, 10).Value = Records(lr).Quantity
    Next lr
    Sheets("Sheet1").Cells(3, 4) = Time

End Sub


The resulting times were :

5000 records
readfromsheet            6:52:37 PM      6:52:40 PM
savetocsv                  6:52:46 PM      6:52:47 PM
loadfromcsv            6:52:53 PM      6:52:53 PM
writetosheet            6:52:53 PM      6:53:08 PM

10000 records            
readfromsheet            6:58:03 PM      6:58:09 PM
savetocsv                  6:58:16 PM      6:58:17 PM
loadfromcsv            6:58:24 PM      6:58:25 PM
writetosheet            6:58:25 PM      6:58:59 PM

20000 records            
readfromsheet            7:01:28 PM      7:01:39 PM
savetocsv                  7:02:04 PM      7:02:11 PM
loadfromcsv            7:02:17 PM      7:02:18 PM
writetosheet            7:02:18 PM      7:03:50 PM

As you can see, it does it fairly quickly, but the downside to sequential files (csv) is that you have to load them into memory to begin with in order to search them effectively.

But if you're only lookng for simple, it's probably an OK way...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now