Solved

Saving/Loading Data Record

Posted on 2004-09-19
5
184 Views
Last Modified: 2010-05-02
I have the following datarecord

Private Type DataRecord
    ID As Long
    Name As String
end type

Dim Clients(10) As DataRecord

'Add DataRecord
Clients(1).ID=456
Clients(1).Name="John"


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.
0
Comment
Question by:adorenbaum
  • 2
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12097987
Here is a PAQ where I show how to read/write a Type to a Random access file:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20884901.html

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.

Regards,

Idle_Mind
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12100740
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?
0
 

Author Comment

by:adorenbaum
ID: 12103675
jimbobmcgee:

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.
0
 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 250 total points
ID: 12115134
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
       
    Loop
   
    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 & "," & _
                        Records(lr).Quantity
                       
        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, _
                        Records(lr).Quantity
                       
        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...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now