Database

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Guest

Database

Post by Guest »

Okay, I am working at my dealership making an inventory program that is more functional than the proprietary version we have. the problem I am running across is writing the information to a database file. here is the code I have...
TYPE VehicleInfo
Make AS STRING * 10
Model AS STRING * 15
Year AS INTEGER
Body AS STRING * 30
Shade AS STRING * 20
Mileage AS LONG
Stock AS STRING * 8
Price AS INTEGER
Fuel AS STRING * 5
Tran AS STRING * 10
END TYPE
DIM Vehicle AS VehicleInfo
Item = 0

MainMenu:
CLS
title$ = "Inventory Editor"
numberofchoices = 5 'number of choices in your menu (1 to numofchoices)
DIM menuchoices(numberofchoices) AS STRING

'your menu choices as text:
menuchoices(1) = "Add a Vehicle"
menuchoices(2) = "Remove a Vehicle"
menuchoices(3) = "Search inventory"
menuchoices(4) = "View Vehicle Info"
menuchoices(5) = "Quit"

CALL menu(numberofchoices, title$, menuchoices$(), Choice)
ERASE menuchoices 'makes the array inexistant

' choice is the selection now you can use : select case or other code

SELECT CASE Choice
CASE 1
GOTO Addcar
CASE 2
GOTO Removecar
CASE 3
GOTO Search
CASE 4
GOTO ViewCar
CASE 5
GOTO Quit
CASE ELSE
GOTO MainMenu
END SELECT

Addcar:
recordLen# = LEN(Vehicle)
OPEN "Invent.Txt" FOR RANDOM AS #1 LEN = recordLen#
CLS
LOCATE 5, 8
INPUT "Vehicle Make - ", Vehicle.Make
LOCATE 6, 8
INPUT "Vehicle Model - ", Vehicle.Model
LOCATE 7, 8
INPUT "Vehicle Year - ", Vehicle.Year
LOCATE 8, 8
INPUT "Vehicle Color - ", Vehicle.Shade
LOCATE 9, 8
INPUT "Vehicle Body - ", Vehicle.Body
LOCATE 10, 8
INPUT "Vehicle Mileage - ", Vehicle.Mileage
LOCATE 11, 8
INPUT "Stock Number - ", Vehicle.Stock
LOCATE 12, 8
INPUT "Price - ", Vehicle.Price
LOCATE 13, 8
INPUT "Fuel Type - ", Vehicle.Fuel
LOCATE 14, 8
INPUT "Transmission - ", Vehicle.Tran
PUT #1, , Vehicle
CLOSE #1
CLS
LOCATE 10, 10
PRINT "Vehicle added to inventory"

As one can see I have made the user defined variable for keeping the record of the vehicles information. My problem is that the program keeps overwriting the 1st record everytime I add a vehicle instead of appendin it to the next position in the file. What do I need to do to fix this?
User avatar
Michael Calkins
Veteran
Posts: 76
Joined: Tue Apr 05, 2005 8:40 pm
Location: Floresville, Texas
Contact:

Post by Michael Calkins »

your problem, I think, is here:

PUT #1, , Vehicle

I think maybe you should specify a record number there, something like

PUT #1, recordnumber, Vehicle

If you want it at the end of the file, you can do something like LOF(1) \ LEN(Vehicle) to determine the number of records already in the file, so if there are 6 records, write the new one to record number 7. Your OPEN statement has already told QBASIC how long you want your records to be.

One more recommendation: change recordLen# to an INTEGER instead of a DOUBLE. This won't adversly affect program function, but it will be an optimization.

Nice job, by the way. I think you are on the right track. :-)
Regards,
Michael
Last edited by Michael Calkins on Wed Sep 21, 2005 10:45 pm, edited 1 time in total.
Bring on the Maulotaurs! oops...
I like to slay Disciples of D'Sparil...
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

Michael is quite close.

The main problem is you are closing the file and reopening it everything time you are creating a vehicle. maybe you should go into a loop that keeps the file open until you are done addding vehicles, then your PUT without record numbers will work.

However if you want to know how many records you have you can get the file size and divide it by the length of your TYPE definition

Code: Select all


OPEN "Vehicle.txt" FOR RANDOM AS #1

SizeOfFile = LOF(1)
SizeOfRecord = LEN(Vehicle)
NumberOfRecords = SizeOfFile/SizeOfRecord

' If this gives you 1, you have one record.  
' Then your Put will be something like:
SEEK #1, NumberOfRecords  ' Position to last record
GET #1, ,TempVehicle           ' read last record (position to EOF)
PUT #1, , Vehicle               ' write the new vehicle

' you might want to try to just do this too
PUT #1, NumberOfRecords + 1, Vehicle

When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

How do we know this isnt homework? Really, what kind of car dealership uses QB... we don't mind since you showed an effort, but please, if you need help on your homework please say so... Also, try to use the

Code: Select all

[code]
[/code]

tags when posting code. But if it is homework (and you show an effort) I would be more likely to help you.
Image
User avatar
Michael Calkins
Veteran
Posts: 76
Joined: Tue Apr 05, 2005 8:40 pm
Location: Floresville, Texas
Contact:

Post by Michael Calkins »

Really, what kind of car dealership uses QB...
The best ones.

His code was good enough I don't think it was homework. He had one tiny little bug... No reason to think he is a cheater...
Regards,
Michael
Bring on the Maulotaurs! oops...
I like to slay Disciples of D'Sparil...
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Michael Calkins wrote:
Really, what kind of car dealership uses QB...
The best ones.

His code was good enough I don't think it was homework. He had one tiny little bug... No reason to think he is a cheater...
Regards,
Michael
Im just saying...

And what kind of car dealership uses QB? Not the best ones. Only the damn good ones.
Image
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 »

myself and moneo already made a small database maker if you want we can customize it for your dealership...in QB...
Post Reply