Home | Database

Viewing and saving a database to a text file


A Simple Way To Print Or Save Database Data:

I have used the MastApp demo that you should find in the :\Delphi5\Demos\Db\Mastapp directory for this example.

Add a memo (Memo1) to a form (Form1) and align Memo1 to alBottom.

Add a button (Button1) to form1.

Double-click on Button1 and add this code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;
  Memo1.Lines.Add('Sales Person =     ' + MastData.Orders.Fields[23].AsString);
  Memo1.Lines.Add('Order Number: =   ' + MastData.Orders.Fields[0].AsString);
  Memo1.Lines.Add(' ');
  Memo1.Lines.Add('Customer Number = ' + MastData.Orders.Fields[1].AsString);
end;'

Here is the result that you will see in the memo:
 

Sales Person =     Parker, Bill
Order Number: =   1003

Customer Number = 1351

In the Delphi IDE press Shift and the F12 keys and then look through the View Form list to find MastData.
Open MastData and then double-click on the TTable component called Orders.
Scroll through the Fields Editor list to find SalesPerson click on this.
Look in the Object Inspector and look at the Index property, you will notice that it should be 23.
0 is the index of the first field, 1 is the index of the second field, and so on.

All you need to do now is add some code to print or save the data that is shown in Memo1.


Here is how you can view database records and then save them to a text file.

First start a new project, save it in a new directory (folder).

Add a TTable component and a TDataSource component.

Set these properties in the Object Inspector:

Table1

DatabaseName = DBDEMOS
TableName = customer.db

Rename Table1 to Master, we changed the name from Table1 to show you that you can give meaningful names to your TTable components.

DataSource1

DataSet = Master

Next add a DBGrid component and then set its Align property in the Object Inspector to alBottom, then change its DataSource property to DataSource1.

Add a TMemo component and place it above the DBGrid, set the TMemo Align property to alBottom so it sits above DBGrid1.
Double-click to the right of the Lines property in the Object Inspector and delete any text in the String List Editor for Memo1. Click OK.

Set Master's Active property to True.

The Code:

Add a button, then double-click on the button (Button1) and then add this code:

procedure TForm1.Button1Click(Sender: TObject);
var S : String;
begin
     // Loop through the records
    while not Master.Eof do
        begin
            S := Master.FieldByName('CustNo').AsString + ' : ';
            S := S + Master.FieldByName('Company').AsString + ' , ';
            S := S + Master.FieldByName('Addr1').AsString + ' , ';
            S := S + Master.FieldByName('City').AsString + ' , ';
            S := S + Master.FieldByName('State').AsString + ' , ';
            S := S + Master.FieldByName('Zip').AsString + ' , ';
            S := S + Master.FieldByName('Phone').AsString + ' , ';
            S := S + Master.FieldByName('FAX').AsString;
            Memo1.Lines.Add(S);
            Master.Next;
       end;
  // Save to the text file
  Memo1.Lines.SaveToFile('mycustomers.txt');
  Button1.Enabled := True;
end;

You can add or remove any fields that you want.

Run the program and click on Button1.

Have a look in the programs directory and you should find a text file called 'mycustomers.txt'
and it should look like the example below.
 

Here is an example of the text file:



1221 : Kauai Dive Shoppe , 4-976 Sugarloaf Hwy , Kapaa Kauai , HI , 94766-1234 , 808-555-0269 , 808-555-0278
1231 : Unisco , PO Box Z-547 , Freeport ,  ,  , 809-555-3915 , 809-555-4958
1351 : Sight Diver , 1 Neptune Lane , Kato Paphos ,  ,  , 357-6-876708 , 357-6-870943
1354 : Cayman Divers World Unlimited , PO Box 541 , Grand Cayman , ,  , 011-5-697044 , 011-5-697064
1356 : Tom Sawyer Diving Centre , 632-1 Third Frydenhoj , Christiansted , St. Croix , 00820 , 504-798-3022 , -798-7772
1380 : Blue Jack Aqua Center , 23-738 Paddington Lane , Waipahu , HI , 99776 , 401-609-7623 , 401-609-9403
1384 : VIP Divers Club , 32 Main St. , Christiansted , St. Croix , 02800 , 809-453-5976 , 809-453-5932
1510 : Ocean Paradise , PO Box 8745 , Kailua-Kona , HI , 94756 , 808-555-8231 , 808-555-8450
1513 : Fantastique Aquatica , Z32 999 #12A-77 A.A. , Bogota ,  ,  , 057-1-773434 , 057-1-773421
1551 : Marmot Divers Club , 872 Queen St. , Kitchener , Ontario , G3N 2E1 , 416-698-0399 , 426-698-0399
1560 : The Depth Charge , 15243 Underwater Fwy. , Marathon , FL , 35003 , 800-555-3798 , 800-555-0353
1563 : Blue Sports , 203 12th Ave. Box 746 , Giribaldi , OR , 91187 , 610-772-6704 , 610-772-6898
1624 : Makai SCUBA Club , PO Box 8534 , Kailua-Kona , HI , 94756 , 317-649-9098 , 317-649-6787
1645 : Action Club , PO Box 5451-F , Sarasota , FL , 32274 , 813-870-0239 , 813-870-0282
1651 : Jamaica SCUBA Centre , PO Box 68 , Negril , Jamaica ,  , 011-3-697043 , 011-3-697043
1680 : Island Finders , 6133 1/3 Stone Avenue , St Simons Isle , GA , 32521 , 713-423-5675 , 713-423-5676
1984 : Adventure Undersea , PO Box 744 , Belize City ,  ,  , 011-34-09054 , 011-34-09064
2118 : Blue Sports Club , 63365 Nez Perce Street , Largo , FL , 34684 , 612-897-0342 , 612-897-0348
2135 : Frank's Divers Supply , 1455 North 44th St. , Eugene , OR , 90427 , 503-555-2778 , 503-555-2769
2156 : Davy Jones' Locker , 246 South 16th Place , Vancouver , BC , K8V 9P1 , 803-509-0112 , 803-509-0553
2163 : SCUBA Heaven , PO Box Q-8874 , Nassau ,  ,  , 011-32-09485 , 011-32-09485
2165 : Shangri-La Sports Center , PO Box D-5495 , Freeport ,  ,  , 011-32-08574 , 011-32-44938
2315 : Divers of Corfu, Inc. , Marmoset Place 54 , Ayios Matthaios , Corfu ,  , 30-661-88364 , 30-661-05943
2354 : Kirk Enterprises , 42 Aqua Lane , Houston , TX , 77079 , 713-556-6437 , 713-556-1073
2975 : George Bean & Co. , #73 King Salmon Way , Lugoff , NC , 29078 , 803-438-2771 , 803-438-3003
2984 : Professional Divers, Ltd. , 4734 Melinda St. , Hoover , AL , 32145 , 205-555-8333 , 205-555-4054
3041 : Divers of Blue-green , 634 Complex Ave. , Pelham , AL , 32145 , 205-555-7184 , 205-555-6059
3042 : Gold Coast Supply , 223-B Houston Place , Mobile , AL , 30696 , 205-555-2640 , 205-555-4094
3051 : San Pablo Dive Center , 1701-D N Broadway , Santa Maria , CA , 95443 , 823-044-2910 , 823-044-2990
3052 : Underwater Sports Co. , 351-A Sarasota St. , San Jose , CA , 92195 , 408-867-0594 , 408-867-0094
3053 : American SCUBA Supply , 1739 Atlantic Avenue , Lomita , CA , 91770 , 213-654-0092 , 213-654-0095
3054 : Catamaran Dive Club , Box 264 Pleasure Point , Catalina Island , CA , 90740 , 213-223-0941 , 213-223-2324
3055 : Diver's Grotto , 24601 Universal Lane , Downey , CA , 94220 , 213-432-0093 , 213-432-4821
3151 : Fisherman's Eye , PO Box 7542 , Grand Cayman ,  ,  , 809-555-4680 , 809-555-4689
3158 : Action Diver Supply , Blue Spar Box #3 , St. Thomas ,  , 00820 , 22-44-500211 , 22-44-500596
3615 : Marina SCUBA Center , PO Box 82438 Zulu 7831 , Caracas ,  ,  , 58-33-66222 , 58-33-66049
3984 : Blue Glass Happiness , 6345 W. Shore Lane , Santa Monica , CA , 90410 , 213-555-1984 , 213-555-1995
4312 : Divers of Venice , 220 Elm Street , Venice , FL , 39224 , 813-443-2356 , 813-443-9842
4531 : On-Target SCUBA , 7-73763 Nanakawa Road , Winnipeg , Manitoba , J2R 5T3 , 416-445-0988 , 416-445-0223
4652 : Jamaica Sun, Inc. , PO Box 643 , Runaway Bay , Jamaica ,  , 809-555-2746 , 809-555-0929
4684 : Underwater Fantasy , PO Box 842 , Ocho Rios , Jamaica ,  , 809-555-2214 , 809-555-2234
5132 : Princess Island SCUBA , PO Box 32 Waiyevo , Taveuni ,  ,  , 679-311923 , 679-311203
5151 : Central Underwater Supplies , PO Box 737 , Johannesburg ,  , 2042 , 27-11-4432458 , 27-11-4433259
5163 : Safari Under the Sea , PO Box 7456 , Grand Cayman ,  ,  , 809-409-4233 , 809-409-3002
5165 : Larry's Diving School , 3562 NW Bruce Street , Milwaukie , OR , 96277 , 503-403-7777 , 503-403-0059
5384 : Tora Tora Tora , PO Box H-4573 , Nassau ,  ,  , 809-898-0043 , 809-898-9864
5412 : Vashon Ventures , 743 Keyhole Court , Honolulu , HI , 92856 , 532-099-0423 , 532-099-6654
5432 : Divers-for-Hire , G.O. P Box 91 , Suva ,  ,  , 679-804576 , 679-059345
5515 : Ocean Adventures , PO Box 466 Kihei , Maui , HI , 95736 , 776-868-9334 , 776-868-9553
6215 : Underwater SCUBA Company , PO Box Sn 91 , Somerset ,  , SXBN , 809-555-1225 , 809-555-2445
6312 : Aquatic Drama , 921 Everglades Way , Tampa , FL , 30643 , 613-442-7654 , 613-442-7678
6516 : The Diving Company , PO Box 8535 , St. Thomas ,  , 00820 , 22-44-50098 , 22-44-09878
6582 : Norwest'er SCUBA Limited , PO Box 6834 , Paget ,  , PSBZ , 778-123-0745 , 778-123-9705
6812 : Waterspout SCUBA Center , 7865 NE Barber Ct. , Portland , OR , 99271 , 503-654-2434 , 503-654-9986
9841 : Neptune's Trident Supply , PO Box 129 , Negril , Jamaica ,  , 778-897-3546 , 778-897-6643

Home | Database