Page 1 of 1

Database

Posted: Wed Sep 21, 2005 9:57 am
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?

Posted: Wed Sep 21, 2005 1:21 pm
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

Posted: Wed Sep 21, 2005 1:53 pm
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


Posted: Wed Sep 21, 2005 2:14 pm
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.

Posted: Wed Sep 21, 2005 2:33 pm
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

Posted: Wed Sep 21, 2005 4:13 pm
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.

Posted: Thu Sep 22, 2005 1:11 pm
by sid6.7
myself and moneo already made a small database maker if you want we can customize it for your dealership...in QB...