Results 1 to 7 of 7

Thread: Confused in Variable arrays

  1. #1

    Thread Starter
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256

    Unhappy

    Hi, I have been working on a personal directory program. This program can axcept up to 10000 entries which store them on an a database file and at the start up it store each line to the correct variables. forexample if there is 6 names it would be

    name(1) = "something"
    name(2) = "somthing"
    so on
    name(6) = "somthing"

    So now I have created a new future for the program which the user is able to delete an entry. When the program is running the program works fine and it deletes the entery. The program is when you exit the program, it should update the database that is the part I am confused I don't know how to do it. Can someone please help me I am desperate.


  2. #2
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547

    Talking

    What exactly do you mean... when the program closes it wont update the database, well why dont you put the same controls you did to update it in the Form_Unload?...

    Specify exactly what your doing, i might be able to help out more. ?!
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  3. #3

    Thread Starter
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256

    Arrow Thanks

    Ok thanks for your time.

    Well if you delete a record off the program while it is running it will clean all the the text box's and then when you are leaving the program, the program check to see if you have delete anything off the records. Well if you did it will call a sub. The sub will delete the database and restore all the data's in the variables into the database again. I have seen this work before and I am sure this is the way it is seppose to work. ALthough there is something wrong in the sub. It does update the Database but mess up all the records. if this is not enough tell me I will give you the sub codes.

    Thanks again.

  4. #4
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547

    Cool No prob!

    Hehe no problem in replaying to your post, i know what its like to come upon a problem and be stuck for days and days without any answers

    If there is something wrong with the sub, email it to me or post it on here and i'll do my best to examine it.
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  5. #5
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Question

    How are you updating the file on close? You haven't mentioned what kind of file access you are using. Random would probably be best because you can have record numbers of UDTs, take a look at the MSDN under Random File Access, it shows you how to use the record numbers to update and delete various records on a user defines DB like flat file.

    Which means you could change the file live and not wait to App close, (what happens if you make 100 changes then the app crashes? you have to do it again?? ugly! )



  6. #6

    Thread Starter
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256
    Well the sub I have wrote doesn't exactly give much information of what I am doing. Well if you make 100 changes to the file. On the close up the application should delete the whole database and then rewrite all the information in the variables.
    I used
    Sequential access file.
    SUB
    ==============================

    Public Sub Updatation()
    Dim I As Double
    Dim D As Double
    D = frmpd.cboname.ListCount
    Kill (App.Path & "\database.pdd")
    Open App.Path & "\database.pdd" For Append As #1
    For I = 1 To D
    Write #1, People(I).strName, People(I).strPhone, People(I).strFax, People(I).strEmail, People(I).strAddress, People(I).strPager, People(I).strICQ, Business(I).strCompany, Business(I).strTitle, Business(I).strPhone, Business(I).strPager, Business(I).strOffice, Business(I).strDepartment, Business(I).strState, Business(I).strZip, Business(I).strCountry, Business(I).strAddress, Business(I).strMobile, Business(I).strComments
    Next
    frmgoodbye.Show
    Close #1
    End Sub
    =================================
    But the output of this is kind of messed up.
    ===========================
    Kourosh Gonabadi
    VB Programmer
    C++ Newbie
    Graphic Designer
    ===========================

  7. #7
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    This code will cause you problems in future I think.

    In the module create a User defined type, This is from MSDN and looks like what you need:

    The File System Object model does not provide random file creation or access methods. If you need to create or read random files, this information will help you do so.

    The bytes in random-access files form identical records, each containing one or more fields. A record with one field corresponds to any standard type, such as an integer or fixed-length string. A record with more than one field corresponds to a user-defined type. For example, the Worker Type defined below creates 19-byte records that consist of three fields:

    Code:
    Type Worker
       LastName As String * 10
       Title    As String * 7
       Rank     As String * 2
    End Type
    Declaring Variables
    Before your application opens a file for random access, it should declare all variables required to handle data from the file. This includes user-defined types, which correspond to records in the file, as well as standard types for other variables that hold data related to processing a file opened for random access.

    Defining Record Types
    Before opening a file for random access, define a type that corresponds to the records the file does or will contain. For example, an Employee Records file could declare a user-defined data type called Person as follows:

    Code:
    Type Person
       ID               As Integer
       MonthlySalary      As Currency
       LastReviewDate      As Long
       FirstName         As String * 15
       LastName            As String * 15
       Title            As String * 15
       ReviewComments      As String * 150
    End Type

    Declaring Field Variables in a Type Definition
    Because all records in a random-access file must have the same length, it is often useful for string elements in a user-defined type to have a fixed length, as shown in the Person type declaration above, where, for instance, FirstName and LastName have a fixed length of 15 characters.

    If the actual string contains fewer characters than the fixed length of the string element to which it is written, Visual Basic fills the trailing spaces in the record with blanks (character code 32). Also, if the string is longer than the field size, it is truncated. If you use variable-length strings, the total size of any record stored with Put or retrieved with Get must not exceed the record length specified in the Open statementfs Len clause.

    Declaring Other Variables
    After defining a type that corresponds to a typical record, declare any other variables that your application needs to process a file opened for random access. For example:

    Code:
    ' A record variable.
    Public Employee As Person
    ' Tracks the current record.
    Public Position As Long
    ' The number of the last record in the file.
    Public LastRecord As Long

    Opening Files for Random Access
    To open a file for random access, use the following syntax for the Open statement:

    Open pathname [For Random] As filenumber Len = reclength

    Because Random is the default access type, the For Random keywords are optional.

    The expression Len = reclength specifies the size of each record in bytes. Note that every string variable in Visual Basic stores a Unicode string and that you must specify the byte length of that Unicode string. If reclength is less than the actual length of the record written to the file, an error is generated. If reclength is greater than the actual length of the record, the record is written, although some disk space may be wasted.

    You could use the following code to open a file:
    Code:
    Dim FileNum As Integer, RecLength As Long, Employee As Person
    
    ' Calculate the length of each record.
    RecLength = LenB(Employee)
    ' Get the next available file number.
    FileNum = FreeFile
    ' Open the new file with the Open statement.
    Open "MYFILE.FIL" For Random As FileNum Len = RecLength
    Editing Files Opened for Random Access
    If you want to edit a random access file, first read records from the file into program variables, then change the values in the variables, and finally, write the variables back into the file. The following sections discuss how to edit files opened for random access.

    Reading Records into Variables
    Use the Get statement to copy records into variables. For instance, to copy a record from the Employee Records file into the Employee variable, you could use the following code:

    Get FileNum, Position, Employee

    In this line of code, FileNum contains the number that the Open statement used to open the file; Position contains the record number of the record to copy; and Employee, declared as user-defined type Person, receives the contents of the record.

    Writing Variables to Records
    Use the Put statement to add or replace records into files opened for random access.

    Replacing Records
    To replace records, use a Put statement, specifying the position of the record you want to replace; for example:

    Put #FileNum, Position, Employee

    This code will replace the record number specified by Position, with the data in the Employee variable.

    Adding Records
    To add new records to the end of a file opened for random access, use the Put statement shown in the preceding code fragment. Set the value of the Position variable equal to one more than the number of records in the file. For example, to add a record to a file that contains five records, set Position equal to 6.

    The following statement adds a record to the end of the file:
    Code:
    LastRecord = LastRecord + 1
    Put #FileNum, LastRecord, Employee
    Deleting Records
    You could delete a record by clearing its fields, but the record would still exist in the file. Usually you donft want empty records in your file, because they waste space and interfere with sequential operations. It is better to copy the remaining records to a new file, and then delete the old file.

    To remove a deleted record in a random-access file

    Create a new file.


    Copy all the valid records from the original file into the new file.


    Close the original file and use the Kill statement to delete it.


    Use the Name statement to rename the new file with the name of the original file.
    For More Information For additional information on random file access, see "Open Statement."

Posting Permissions

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



Click Here to Expand Forum to Full Width