[init]'define global variablesglobal MaxItems
'predefine item arraydim items$(1), search$(1)'get database contentscall OpenDB
call ReadDB
call CloseDB
[MainGUI]'Form created with the help of Freeform 3 v01-28-07'Generated on Jun 19, 2007 at 22:50:13nomainwinWindowWidth=440WindowHeight=230UpperLeftX=int((DisplayWidth-WindowWidth)/2)UpperLeftY=int((DisplayHeight-WindowHeight)/2)listbox#main.itemlist, items$(,[DisplayItem],5,5,175,185statictext#main.NumberTxt,"Item Number:",200,7,80,25statictext#main.NumberDisp,"",300,7,95,25statictext#main.NameTxt,"Item Name:",200,32,80,25statictext#main.NameDisp,"",300,32,95,25statictext#main.PrizeTxt,"Item Prize:",200,57,80,25statictext#main.PrizeDisp,"",300,57,95,25button#main.add,"Add Item", CheckButton, UL,200,112,63,25button#main.edit,"Edit Item", CheckButton, UL,275,112,63,25button#main.delete,"Delete Item",CheckButton, UL,350,112,75,25button#main.search,"Search",[search], UL,200,162,63,25button#main.exit,"EXIT",[quit.main], UL,350,162,39,25open"Simple Database Framework"forwindowas#main
print#main,"font ms_sans_serif 10"print#main,"trapclose [quit.main]"#main.itemlist "singleclickselect"wait[DisplayItem]'get index of selected item#main.itemlist "selectionindex? index"#main.NameDisp word$(items$(index),1,chr$(0))#main.NumberDisp word$(items$(index),2,chr$(0))#main.PrizeDisp word$(items$(index),3,chr$(0))wait[search]'search in the databaseWindowWidth=430WindowHeight=190'position of dialogs are relative to previous open windowUpperLeftX=1UpperLeftY=1textbox#search.String,5,5,175,25button#search.default,"Search",[doSearch], UL,200,5,75,25listbox#search.itemlist, search$(,[doDisplay],5,35,175,120statictext#search.NumberTxt,"Item Number:",200,35,80,25statictext#search.NumberDisp,"",300,35,95,25statictext#search.NameTxt,"Item Name:",200,60,80,25statictext#search.NameDisp,"",300,60,95,25statictext#search.PrizeTxt,"Item Prize:",200,85,80,25statictext#search.PrizeDisp,"",300,85,95,25button#search.cancel,"Close",[quit.search], UL,300,127,63,25'modal windows block access to the previous windowopen"Search Database for Name"for dialog_modal as#search
print#search,"font ms_sans_serif 10"print#search,"trapclose [quit.search]"#search.itemlist "singleclickselect"wait[doSearch]redim search$(MaxItems)
foundItem =0' search by name = field 1
FieldNumber =1#search.String "!contents? SearchString$"for Count =1to MaxItems
'ignore case using LOWER$()ifinstr(lower$(word$(items$(Count), FieldNumber,chr$(0))),lower$(SearchString$))>0then
foundItem = foundItem +1
search$(foundItem)= items$(Count)endifnext#search.itemlist "reload"#search.itemlist "selectindex 0"wait[doDisplay]'get index of selected item#search.itemlist "selectionindex? index"#search.NameDisp word$(search$(index),1,chr$(0))#search.NumberDisp word$(search$(index),2,chr$(0))#search.PrizeDisp word$(search$(index),3,chr$(0))wait[quit.search]close#search
wait[quit.main]close#main
ENDsub CheckButton handle$
'get extension of button
extension$ =word$(handle$,2,".")'get index of selected item#main.itemlist "selectionindex? index"'select action based on pushed buttonselectcase extension$
case"add"call DisplayDialog "Add Item", MaxItems
case"edit"if index >0thencall DisplayDialog "Edit Item", index
case"delete"if index >0thencall DeleteItem index
endselect'refresh listbox contents#main.itemlist "reload"'cancel selection to allow reselection of currently selected item#main.itemlist "selectindex 0"endsubsub DisplayDialog Caption$, ItemNumber
'Form created with the help of Freeform 3 v01-28-07'Generated on Jun 19, 2007 at 22:59:56WindowWidth=275WindowHeight=195'position of dialogs are relative to previous open windowUpperLeftX=1UpperLeftY=1statictext#item.NumberTxt,"Item Number:",10,7,80,25statictext#item.NameTxt,"Item Name:",10,42,80,25statictext#item.PrizeTxt,"Item Prize:",10,77,80,25textbox#item.Number,105,7,150,25textbox#item.Name,105,42,150,25textbox#item.Prize,105,77,150,25button#item.cancel,"Close",[quit.item], UL,95,127,63,25button#item.default,"Apply",[apply], UL,180,127,75,25'modal windows block access to the previous windowopen Caption$; " - "; ItemNumber for dialog_modal as#item
print#item,"font ms_sans_serif 10"print#item,"trapclose [quit.item]"if ItemNumber <> MaxItems then#item.Nameword$(items$(ItemNumber),1,chr$(0))#item.Number word$(items$(ItemNumber),2,chr$(0))#item.Prize word$(items$(ItemNumber),3,chr$(0))endif#item.Number "!setfocus"wait[apply]' apply changes#item.Number "!contents? Temp1$"#item.Name"!contents? Name$"#item.Prize "!contents? Temp2$"' Make sure info in boxes is the proper type of data (number/string)if Temp1$ =str$(val(Temp1$))then
Number =val(Temp1$)else' Item entered in the Number box is not a number !notice"Item Number must be numeric only."waitendifif Temp2$ =str$(val(Temp2$))then
Prize =val(Temp2$)else' Item entered in the Prize box is not a number !notice"Item Prize must be numeric only."waitendif'fill the array element with the data'separate fields by CHR$(0) to display only the first field in the listbox
items$(ItemNumber)=trim$(Name$); chr$(0); Number; chr$(0); Prize
call ApplyItemData
wait[quit.item]'exit dialogclose#item
endsubsub ApplyItemData
call BackupDB
call OpenDB
call WriteDB
call ReadDB
call CloseDB
endsubsub DeleteItem ItemIndex
confirm"Delete Item ... "+str$(ItemIndex)+chr$(13)+_
"Name ..... "+word$(items$(ItemIndex),1,chr$(0))+chr$(13)+_
"Number ... "+word$(items$(ItemIndex),2,chr$(0))+chr$(13)+_
"Prize .... "+word$(items$(ItemIndex),3,chr$(0)); answer
if answer then
items$(ItemIndex)=""call BackupDB
call OpenDB
call WriteDB
call ReadDB
call CloseDB
endifendsubsub OpenDB
'open database and define record lengthopen"database.dat"forrandomas#db len=150'set the fields, include some extra space for future usefield#db,_
40as ItemName$,_
10as ItemNumber,_
10as ItemPrize,_
90as Reserve$
endsubsub CloseDB
close#db
endsubsub ReadDB
'get the number of records in the database'= length of database file divided by the record length
TotalRecords =lof(#db)/150'check if the database is corruptedif TotalRecords <>int(TotalRecords)thennotice"Database corrupted"; chr$(13); "Please check its contents!"
TotalRecords =int(TotalRecords + .5)endif'dimension array to enable adding one record
MaxItems = TotalRecords +1redim items$(MaxItems)for Record =1to TotalRecords
get#db, Record
'fill the array with the data'separate fields by CHR$(0) to display only the first field in the listbox
items$(Record)=trim$(ItemName$); chr$(0); ItemNumber; chr$(0); ItemPrize
nextendsubsub WriteDB
Record =1for Count =1to MaxItems
if items$(Count)<>""then
ItemName$ =word$(items$(Count),1,chr$(0))
ItemNumber =val(word$(items$(Count),2,chr$(0)))
ItemPrize =val(word$(items$(Count),3,chr$(0)))put#db, Record
Record = Record +1endifnextendsubsub BackupDB
if FileExists("database.bak")thenkill"database.bak"name"database.dat"as"database.bak"endsubfunction FileExists(FilePath$)' returns zero if file does not exist' returns one if file existsdim FileExistsInfo$(1,1)files"", FilePath$, FileExistsInfo$(
FileExists =val(FileExistsInfo$(0,0))endfunction
[init]'predefine item arraydim items$(1)'get database contentsgosub[OpenDB]gosub[ReadDB]gosub[CloseDB][MainGUI]'Form created with the help of Freeform 3 v01-28-07'Generated on Jun 19, 2007 at 22:50:13nomainwinWindowWidth=440WindowHeight=230UpperLeftX=int((DisplayWidth-WindowWidth)/2)UpperLeftY=int((DisplayHeight-WindowHeight)/2)listbox#main.itemlist, items$(,[DisplayItem],5,5,175,185statictext#main.NumberTxt,"Item Number:",200,7,80,25statictext#main.NumberDisp,"",300,7,95,25statictext#main.NameTxt,"Item Name:",200,32,80,25statictext#main.NameDisp,"",300,32,95,25statictext#main.PrizeTxt,"Item Prize:",200,57,80,25statictext#main.PrizeDisp,"",300,57,95,25button#main.add,"Add Item",[add], UL,200,112,63,25button#main.edit,"Edit Item",[edit], UL,275,112,63,25button#main.delete,"Delete Item",[delete], UL,350,112,75,25button#main.search,"Search",[search], UL,200,162,63,25button#main.exit,"EXIT",[quit.main], UL,350,162,39,25open"Simple Database Framework"forwindowas#main
print#main,"font ms_sans_serif 10"print#main,"trapclose [quit.main]"#main.itemlist "singleclickselect"wait[add]
extension$ ="add"gosub[CheckButton]wait[edit]
extension$ ="edit"gosub[CheckButton]wait[delete]
extension$ ="delete"gosub[CheckButton]wait[DisplayItem]'get index of selected item#main.itemlist "selectionindex? SelectedItem"#main.NameDisp word$(items$(SelectedItem),1,chr$(0))#main.NumberDisp word$(items$(SelectedItem),2,chr$(0))#main.PrizeDisp word$(items$(SelectedItem),3,chr$(0))wait[search]'search in the databaseWindowWidth=430WindowHeight=190'position of dialogs are relative to previous open windowUpperLeftX=1UpperLeftY=1textbox#search.String,5,5,175,25button#search.default,"Search",[doSearch], UL,200,5,75,25listbox#search.itemlist, search$(,[doDisplay],5,35,175,120statictext#search.NumberTxt,"Item Number:",200,35,80,25statictext#search.NumberDisp,"",300,35,95,25statictext#search.NameTxt,"Item Name:",200,60,80,25statictext#search.NameDisp,"",300,60,95,25statictext#search.PrizeTxt,"Item Prize:",200,85,80,25statictext#search.PrizeDisp,"",300,85,95,25button#search.cancel,"Close",[quit.search], UL,300,127,63,25'modal windows block access to the previous windowopen"Search Database for Name"for dialog_modal as#search
print#search,"font ms_sans_serif 10"print#search,"trapclose [quit.search]"#search.itemlist "singleclickselect"wait[doSearch]redim search$(MaxItems)
foundItem =0' search by name = field 1
FieldNumber =1#search.String "!contents? SearchString$"for Count =1to MaxItems
'ignore case using LOWER$()ifinstr(lower$(word$(items$(Count), FieldNumber,chr$(0))),lower$(SearchString$))>0then
foundItem = foundItem +1
search$(foundItem)= items$(Count)endifnext#search.itemlist "reload"#search.itemlist "selectindex 0"wait[doDisplay]'get index of selected item#search.itemlist "selectionindex? index"#search.NameDisp word$(search$(index),1,chr$(0))#search.NumberDisp word$(search$(index),2,chr$(0))#search.PrizeDisp word$(search$(index),3,chr$(0))wait[quit.search]close#search
wait[quit.main]close#main
END[CheckButton]'select action based on pushed buttonselectcase extension$
case"add"
SelectedItem = MaxItems
DialogCaption$ ="Add Item"gosub[DisplayDialog]case"edit"
DialogCaption$ ="Edit Item"if SelectedItem >0thengosub[DisplayDialog]case"delete"if SelectedItem >0thengosub[DeleteItem]endselect'refresh listbox contents#main.itemlist "reload"'cancel selection to allow reselection of currently selected item#main.itemlist "selectindex 0"return[DisplayDialog]'Form created with the help of Freeform 3 v01-28-07'Generated on Jun 19, 2007 at 22:59:56WindowWidth=275WindowHeight=195'position of dialogs is relative to previous open windowUpperLeftX=1UpperLeftY=1statictext#item.NumberTxt,"Item Number:",10,7,80,25statictext#item.NameTxt,"Item Name:",10,42,80,25statictext#item.PrizeTxt,"Item Prize:",10,77,80,25textbox#item.Number,105,7,150,25textbox#item.Name,105,42,150,25textbox#item.Prize,105,77,150,25button#item.cancel,"Close",[quit.item], UL,95,127,63,25button#item.default,"Apply",[apply], UL,180,127,75,25'modal windows block access to the previous windowopen DialogCaption$; " - "; SelectedItem for dialog_modal as#item
print#item,"font ms_sans_serif 10"print#item,"trapclose [quit.item]"if SelectedItem <> MaxItems then#item.Nameword$(items$(SelectedItem),1,chr$(0))#item.Number word$(items$(SelectedItem),2,chr$(0))#item.Prize word$(items$(SelectedItem),3,chr$(0))endif#item.Number "!setfocus"wait[apply]' apply changes#item.Number "!contents? Temp1$"#item.Name"!contents? Name$"#item.Prize "!contents? Temp2$"' Make sure info in boxes is the proper type of data (number/string)if Temp1$ =str$(val(Temp1$))then
Number =val(Temp1$)else' Item entered in the Number box is not a number !notice"Item Number must be numeric only."waitendifif Temp2$ =str$(val(Temp2$))then
Prize =val(Temp2$)else' Item entered in the Prize box is not a number !notice"Item Prize must be numeric only."waitendif'fill the array element with the data'separate fields by CHR$(0) to display only the first field in the listbox
items$(SelectedItem)=trim$(Name$); chr$(0); Number; chr$(0); Prize
gosub[ApplyItemData]wait[quit.item]'exit dialogclose#item
return[ApplyItemData]gosub[BackupDB]gosub[OpenDB]gosub[WriteDB]gosub[ReadDB]gosub[CloseDB]return[DeleteItem]confirm"Delete Item ... "+str$(SelectedItem)+chr$(13)+_
"Name ..... "+word$(items$(SelectedItem),1,chr$(0))+chr$(13)+_
"Number ... "+word$(items$(SelectedItem),2,chr$(0))+chr$(13)+_
"Prize .... "+word$(items$(SelectedItem),3,chr$(0)); answer
if answer then
items$(SelectedItem)=""gosub[BackupDB]gosub[OpenDB]gosub[WriteDB]gosub[ReadDB]gosub[CloseDB]endifreturn[OpenDB]'open database and define record lengthopen"database.dat"forrandomas#db len=150'set the fields, include some extra space for future usefield#db,_
40as ItemName$,_
10as ItemNumber,_
10as ItemPrize,_
90as Reserve$
return[CloseDB]close#db
return[ReadDB]'get the number of records in the database'= length of database file divided by the record length
TotalRecords =lof(#db)/150'check if the database is corruptedif TotalRecords <>int(TotalRecords)thennotice"Database corrupted"; chr$(13); "Please check its contents!"
TotalRecords =int(TotalRecords + .5)endif'dimension array to enable adding one record
MaxItems = TotalRecords +1redim items$(MaxItems)for Record =1to TotalRecords
get#db, Record
'fill the array with the data'separate fields by CHR$(0) to display only the first field in the listbox
items$(Record)=trim$(ItemName$); chr$(0); ItemNumber; chr$(0); ItemPrize
nextreturn[WriteDB]
Record =1for Count =1to MaxItems
if items$(Count)<>""then
ItemName$ =word$(items$(Count),1,chr$(0))
ItemNumber =val(word$(items$(Count),2,chr$(0)))
ItemPrize =val(word$(items$(Count),3,chr$(0)))put#db, Record
Record = Record +1endifnextreturn[BackupDB]if FileExists("database.bak")thenkill"database.bak"name"database.dat"as"database.bak"returnfunction FileExists(FilePath$)' returns zero if file does not exist' returns one if file existsdim FileExistsInfo$(1,1)files"", FilePath$, FileExistsInfo$(
FileExists =val(FileExistsInfo$(0,0))endfunction
Below find a simple framework for a database program.
You can use it in any way in your own projects, there is no usage limitation.
There are two versions available,
-
Using SUB's
Back to Top
Using GOSUB's
Back to Top