'=================
SUB OpenDatabase()
'=================
	hStmt=NULL
	problem=0
	pDB=dbConnect("Microsoft Access Driver (*.mdb)",GETSTARTPATH+"bin\\Birchfield.mdb","",win)
IF pDB=NULL
'Couldn't open so try creating
	dbCreateMDB(GETSTARTPATH+"bin\\Birchfield.mdb")
	pDB=dbConnect("Microsoft Access Driver (*.mdb)",GETSTARTPATH+"bin\\Birchfield.mdb","",win)
IF pDB<>NULL
'Create addresses table
	iStmt="CREATE TABLE addresses (addrid counter(1,1),recID int,fName varchar(40),lName varchar(40),"
	iStmt+="street varchar(40),street2 varchar(40),city varchar(40),county varchar(15),"
	iStmt+="pcode varchar(9),phone varchar(40),dateChanged date,buddy char(1),"
	iStmt+="contact2 varchar(40),contact3 varchar(40),template int,template2 int,balance float,school float)"
	hStmt=dbExecSQL(pDB,iStmt)
'Check for errors
IF LEN(dbGetErrorCode(hStmt))
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,dbGetErrorCode(hStmt)
	problem=1
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
IF problem=1 THEN RETURN
'Create dates table
	iStmt="CREATE TABLE dates (sDate char(10) primary key,sDay char(3),sWeek int)"
	hStmt=dbExecSQL(pDB,iStmt)
'Check for errors
IF LEN(dbGetErrorCode(hStmt))
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,dbGetErrorCode(hStmt)
	problem=1
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
IF problem=1 THEN RETURN
'Create costs table
	iStmt="CREATE TABLE costs (costsID counter(1,1),pCost char(6),sCost char(6))"
	hStmt=dbExecSQL(pDB,iStmt)
'Check for errors
IF LEN(dbGetErrorCode(hStmt))
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,dbGetErrorCode(hStmt)
	problem=1
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
IF problem=1 THEN RETURN
'Insert record 1 - 7:30 am to 8:00 am (Nursery)
	hStmt=dbExecSQL(pDB,"INSERT INTO costs (pCost,sCost) VALUES('2.00','0.00')")
	temp=dbGetErrorCode(hStmt)
IF LEN(temp)
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,temp
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
'Insert record 2 - 7:30 am to 8:45 am (Mon-Fri) - NEW SESSION
	hStmt=dbExecSQL(pDB,"INSERT INTO costs (pCost,sCost) VALUES('5.20','0.00')")
	temp=dbGetErrorCode(hStmt)
IF LEN(temp)
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,temp
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
'Insert record 3 - 8:00 am to 8:45 am (Mon-Fri)
	hStmt=dbExecSQL(pDB,"INSERT INTO costs (pCost,sCost) VALUES('0.00','3.15')")
	temp=dbGetErrorCode(hStmt)
IF LEN(temp)
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,temp
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
'Insert record 4 - 8:45 am to 12:30 pm (Wed only)
	hStmt=dbExecSQL(pDB,"INSERT INTO costs (pCost,sCost) VALUES('16.00','15.75')")
	temp=dbGetErrorCode(hStmt)
IF LEN(temp)
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,temp
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
'Insert record 5 - 11:15 am to 3:15 pm (Wed only)
	hStmt=dbExecSQL(pDB,"INSERT INTO costs (pCost,sCost) VALUES('16.00','15.75')")
	temp=dbGetErrorCode(hStmt)
IF LEN(temp)
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,temp
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
'Insert record 6 - 3:00 pm to 6:00 pm (Mon-Fri)
	hStmt=dbExecSQL(pDB,"INSERT INTO costs (pCost,sCost) VALUES('10.50','12.60')")
	temp=dbGetErrorCode(hStmt)
IF LEN(temp)
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,temp
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
'Create attendance table
	iStmt="CREATE TABLE attendance (attid counter(1,1),recID int,sweek int,template int,template2 int)"
	hStmt=dbExecSQL(pDB,iStmt)
'Check for errors
IF LEN(dbGetErrorCode(hStmt))
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,dbGetErrorCode(hStmt)
	problem=1
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
IF problem=1 THEN RETURN
'Create invoices table
	iStmt="CREATE TABLE invoices (invid counter(1,1),recID int,yrmo int,sweek int,recNo int,"
	iStmt+="budDays int,schdays int,invText varchar(60),budVal float,schVal float,tDate char(10))"
	hStmt=dbExecSQL(pDB,iStmt)
'Check for errors
IF LEN(dbGetErrorCode(hStmt))
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,dbGetErrorCode(hStmt)
	problem=1
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
IF problem=1 THEN RETURN
'Create indxinvoices index
	iStmt="CREATE INDEX indxInvoices ON invoices (recID, sweek);"
	hStmt=dbExecSQL(pDB,iStmt)
'Check for errors
IF LEN(dbGetErrorCode(hStmt))
	MESSAGEBOX win,"("+STR$(__LINE__)+") "+dbGetErrorText(hStmt)+"\n"+iStmt,dbGetErrorCode(hStmt)
	problem=1
ENDIF
	dbFreeSQL(hStmt)
	hStmt=NULL
IF problem=1 THEN RETURN
ELSE
	MESSAGEBOX win,"("+STR$(__LINE__)+") Could not connect/create database","Error",@MB_ICONSTOP
ENDIF
ENDIF
'Are we open?
IF pDB<>NULL
'Get the initial number of records
	numRecs=dbCardinality(pDB,"addresses")
'Get last RecID
IF numRecs>0
	DoMax()
ENDIF
ENDIF
ENDSUB
