diff --git a/components/tvplanit/examples/datastores/flex/access/empty db/data.accdb b/components/tvplanit/examples/datastores/flex/access/empty db/data.accdb new file mode 100644 index 000000000..bdbabb9af Binary files /dev/null and b/components/tvplanit/examples/datastores/flex/access/empty db/data.accdb differ diff --git a/components/tvplanit/examples/datastores/flex/access/empty db/data.mdb b/components/tvplanit/examples/datastores/flex/access/empty db/data.mdb new file mode 100644 index 000000000..0bfc47c2c Binary files /dev/null and b/components/tvplanit/examples/datastores/flex/access/empty db/data.mdb differ diff --git a/components/tvplanit/examples/datastores/flex/access/empty db/readme.txt b/components/tvplanit/examples/datastores/flex/access/empty db/readme.txt new file mode 100644 index 000000000..d12595fa7 --- /dev/null +++ b/components/tvplanit/examples/datastores/flex/access/empty db/readme.txt @@ -0,0 +1,10 @@ +Copy one of these two empty Access database file to the parent folder. + +If you copied "data.mdb" then you must activate the define MDB in the head of +unit "Unit". + +If you copied "data.accdb" then you must activate the define ACCDB. + +Don't activate both! + +Note: On 32-bit systems the ACCDB format has issues. \ No newline at end of file diff --git a/components/tvplanit/examples/datastores/flex/access/project1.lpi b/components/tvplanit/examples/datastores/flex/access/project1.lpi index 1e6cfe103..b1083a85e 100644 --- a/components/tvplanit/examples/datastores/flex/access/project1.lpi +++ b/components/tvplanit/examples/datastores/flex/access/project1.lpi @@ -1,7 +1,7 @@ - + @@ -17,9 +17,10 @@ - - - + + + + @@ -58,7 +59,6 @@ - @@ -73,7 +73,7 @@ - + @@ -83,6 +83,9 @@ + + + diff --git a/components/tvplanit/examples/datastores/flex/access/tools/CreateAccessDB.lpi b/components/tvplanit/examples/datastores/flex/access/tools/CreateAccessDB.lpi index 3968e19d3..30a5aa63e 100644 --- a/components/tvplanit/examples/datastores/flex/access/tools/CreateAccessDB.lpi +++ b/components/tvplanit/examples/datastores/flex/access/tools/CreateAccessDB.lpi @@ -1,7 +1,7 @@ - + @@ -9,11 +9,7 @@ <ResourceType Value="res"/> <UseXPManifest Value="True"/> - <Icon Value="0"/> </General> - <VersionInfo> - <StringTable ProductVersion=""/> - </VersionInfo> <BuildModes Count="1"> <Item1 Name="Default" Default="True"/> </BuildModes> @@ -21,17 +17,21 @@ <Version Value="2"/> </PublishOptions> <RunParams> - <local> - <FormatVersion Value="1"/> - </local> + <FormatVersion Value="2"/> + <Modes Count="1"> + <Mode0 Name="default"/> + </Modes> </RunParams> - <RequiredPackages Count="2"> + <RequiredPackages Count="3"> <Item1> - <PackageName Value="SQLDBLaz"/> + <PackageName Value="FCL"/> </Item1> <Item2> - <PackageName Value="LCL"/> + <PackageName Value="SQLDBLaz"/> </Item2> + <Item3> + <PackageName Value="LCL"/> + </Item3> </RequiredPackages> <Units Count="2"> <Unit0> @@ -39,7 +39,7 @@ <IsPartOfProject Value="True"/> </Unit0> <Unit1> - <Filename Value="..\..\..\odbcdatastore\camain.pas"/> + <Filename Value="camain.pas"/> <IsPartOfProject Value="True"/> <ComponentName Value="Form1"/> <HasResources Value="True"/> diff --git a/components/tvplanit/examples/datastores/flex/access/tools/CreateAccessDB.lpr b/components/tvplanit/examples/datastores/flex/access/tools/CreateAccessDB.lpr index 2d790caa9..516f7ec25 100644 --- a/components/tvplanit/examples/datastores/flex/access/tools/CreateAccessDB.lpr +++ b/components/tvplanit/examples/datastores/flex/access/tools/CreateAccessDB.lpr @@ -7,8 +7,8 @@ uses cthreads, {$ENDIF}{$ENDIF} Interfaces, // this includes the LCL widgetset - Forms, caMain - { you can add units after this }; + Forms, + caMain; {$R *.res} diff --git a/components/tvplanit/examples/datastores/flex/access/tools/camain.lfm b/components/tvplanit/examples/datastores/flex/access/tools/camain.lfm index 3fa54e228..a0f33d92d 100644 --- a/components/tvplanit/examples/datastores/flex/access/tools/camain.lfm +++ b/components/tvplanit/examples/datastores/flex/access/tools/camain.lfm @@ -1,7 +1,7 @@ object Form1: TForm1 - Left = 262 + Left = 413 Height = 285 - Top = 155 + Top = 171 Width = 400 AutoSize = True Caption = 'Access database creator' @@ -9,7 +9,7 @@ object Form1: TForm1 ClientWidth = 400 Constraints.MinWidth = 400 OnCreate = FormCreate - LCLVersion = '1.6.4.0' + LCLVersion = '1.9.0.0' object StatusBar1: TStatusBar Left = 0 Height = 23 @@ -103,28 +103,13 @@ object Form1: TForm1 TabOrder = 0 Text = '.\data.mdb' end - object Label1: TLabel - AnchorSideLeft.Control = FileNameEdit - AnchorSideTop.Control = FileNameEdit - AnchorSideTop.Side = asrBottom - AnchorSideRight.Control = FileNameEdit - AnchorSideRight.Side = asrBottom - Left = 16 - Height = 30 - Top = 47 - Width = 368 - Anchors = [akTop, akLeft, akRight] - BorderSpacing.Top = 8 - Caption = 'Use extension .mdb for old Access 97/2000 file format,'#13#10'.accdb for new Access 2007+ file format.' - ParentColor = False - end object CbCreateVPFields: TCheckBox AnchorSideLeft.Control = FileNameEdit - AnchorSideTop.Control = Label1 + AnchorSideTop.Control = RgFormat AnchorSideTop.Side = asrBottom Left = 16 Height = 19 - Top = 85 + Top = 125 Width = 140 BorderSpacing.Top = 8 BorderSpacing.Bottom = 16 @@ -133,13 +118,42 @@ object Form1: TForm1 State = cbChecked TabOrder = 1 end + object RgFormat: TRadioGroup + AnchorSideLeft.Control = FileNameEdit + AnchorSideTop.Control = FileNameEdit + AnchorSideTop.Side = asrBottom + Left = 16 + Height = 62 + Top = 55 + Width = 267 + AutoFill = True + AutoSize = True + BorderSpacing.Top = 16 + Caption = 'Access database format' + ChildSizing.LeftRightSpacing = 16 + ChildSizing.TopBottomSpacing = 2 + ChildSizing.EnlargeHorizontal = crsHomogenousChildResize + ChildSizing.EnlargeVertical = crsHomogenousChildResize + ChildSizing.ShrinkHorizontal = crsScaleChilds + ChildSizing.ShrinkVertical = crsScaleChilds + ChildSizing.Layout = cclLeftToRightThenTopToBottom + ChildSizing.ControlsPerLine = 1 + ClientHeight = 42 + ClientWidth = 263 + ItemIndex = 0 + Items.Strings = ( + '.mdb (Access 97, 2000, 2003), 32 bit only' + '.accdb (Access 2007+), 32 bit and 64 bit' + ) + OnClick = RgFormatClick + TabOrder = 2 + end end object ODBCConnection1: TODBCConnection Connected = False LoginPrompt = False KeepConnection = False Transaction = SQLTransaction1 - Options = [] left = 48 top = 168 end @@ -147,7 +161,6 @@ object Form1: TForm1 Active = False Action = caCommit Database = ODBCConnection1 - Options = [] left = 152 top = 168 end diff --git a/components/tvplanit/examples/datastores/flex/access/tools/camain.pas b/components/tvplanit/examples/datastores/flex/access/tools/camain.pas index 4e3e1ce31..9f0cfb97c 100644 --- a/components/tvplanit/examples/datastores/flex/access/tools/camain.pas +++ b/components/tvplanit/examples/datastores/flex/access/tools/camain.pas @@ -18,17 +18,19 @@ type BtnClose: TButton; CbCreateVPFields: TCheckBox; FileNameEdit: TFileNameEdit; - Label1: TLabel; ODBCConnection1: TODBCConnection; Panel1: TPanel; Panel2: TPanel; + RgFormat: TRadioGroup; SQLTransaction1: TSQLTransaction; StatusBar1: TStatusBar; procedure BtnCreateDBClick(Sender: TObject); procedure BtnCloseClick(Sender: TObject); procedure FormCreate(Sender: TObject); + procedure RgFormatClick(Sender: TObject); private - function CreateAccessDatabase(DatabaseFile: string): boolean; + function CreateAccessDatabase(ADatabaseFile: string; + out AErrorMsg: String): boolean; procedure CreateContactsTable; procedure CreateEventsTable; procedure CreateResourceTable; @@ -48,7 +50,16 @@ implementation uses LCLType, LazFileUtils; -Const +const + DB_DRIVERS: array[0..1] of String = ( + 'Microsoft Access Driver (*.mdb)', + 'Microsoft Access Driver (*.mdb, *.accdb)' + ); + EXT: array[0..1] of String = ( + '.mdb', + '.accdb' + ); + ODBC_ADD_DSN = 1; ODBC_CONFIG_DSN = 2; ODBC_REMOVE_DSN = 3; @@ -69,26 +80,34 @@ function SQLInstallerError(iError: integer; pfErrorCode: PInteger; procedure TForm1.BtnCreateDBClick(Sender: TObject); var fn: String; + errMsg: String; begin if FileNameEdit.FileName = '' then exit; - fn := ExpandFileNameUTF8(FilenameEdit.FileName); + + fn := ChangeFileExt(FilenameEdit.FileName, EXT[RgFormat.ItemIndex]); + fn := ExpandFileNameUTF8(fn); if FileExistsUTF8(fn) then DeleteFileUTF8(fn); // Create empty database file - CreateAccessDatabase(fn); - StatusMsg('Database file created'); + if CreateAccessDatabase(fn, errMsg) then + StatusMsg('Database file created') + else begin + MessageDlg('Database file could not be created:' + LineEnding + errMsg, + mtError, [mbOK], 0); + exit; + end; if CbCreateVPFields.Checked then begin //connection - ODBCConnection1.Driver := 'Microsoft Access Driver (*.mdb, *.accdb)'; + ODBCConnection1.Driver := DB_DRIVERS[RgFormat.ItemIndex]; ODBCConnection1.Params.Add('DBQ=' + fn); - ODBCConnection1.Params.Add('Locale Identifier=1031'); - ODBCConnection1.Params.Add('ExtendedAnsiSQL=1'); - ODBCConnection1.Params.Add('CHARSET=ansi'); - ODBCConnection1.Connected := True; +// ODBCConnection1.Params.Add('Locale Identifier=1031'); +// ODBCConnection1.Params.Add('ExtendedAnsiSQL=1'); +// ODBCConnection1.Params.Add('CHARSET=ansi'); ODBCConnection1.KeepConnection := True; + ODBCConnection1.Connected := True; //transaction SQLTransaction1.DataBase := ODBCConnection1; @@ -113,55 +132,64 @@ begin Close; end; -function TForm1.CreateAccessDatabase(DatabaseFile: string): boolean; +function TForm1.CreateAccessDatabase(ADatabaseFile: string; + out AErrorMsg: String): boolean; var dbType: string; driver: string; ErrorCode, ResizeErrorMessage: integer; ErrorMessage: PChar; retCode: integer; + L: TStrings; begin - driver := 'Microsoft Access Driver (*.mdb, *.accdb)'; + Result := false; + AErrorMsg := ''; - { With this driver, + driver := DB_DRIVERS[rgFormat.ItemIndex]; + + { With the new accdb driver, CREATE_DB/CREATE_DBV12 will create an .accdb format database; CREATE_DBV4 will create an mdb http://stackoverflow.com/questions/9205633/how-do-i-specify-the-odbc-access-driver-format-when-creating-the-database } - case Lowercase(ExtractFileExt(DatabaseFile)) of - '', '.', '.mdb': - dbType := 'CREATE_DBV4="' + DatabaseFile + '"'; - '.accdb': - dbtype := 'CREATE_DBV12="' + DatabaseFile + '"'; - else - raise Exception.CreateFmt('File format "%s" not supported.', [ExtractFileExt(DatabaseFile)]); + case rgFormat.ItemIndex of + 0 : dbtype := 'CREATE_DB="' + ADatabaseFile + '"'; + 1 : case Lowercase(ExtractFileExt(ADatabaseFile)) of + '', '.', '.mdb': dbType := 'CREATE_DBV4="' + ADatabaseFile + '"'; + '.accdb' : dbtype := 'CREATE_DBV12="' + ADatabaseFile + '"'; + else + raise Exception.CreateFmt('File format "%s" not supported.', [ExtractFileExt(ADatabaseFile)]); + end; end; -// DBPChar := 'CREATE_DBV4="' + DatabaseFile + '"'; retCode := SQLConfigDataSource(Hwnd(nil), ODBC_ADD_DSN, PChar(driver), PChar(dbType)); - if retCode <> 0 then + // returns 1 in case of success, 0 in case of failure + if retCode <> 0 then begin + if not FileExists(ADatabaseFile) then + AErrorMsg := 'Successful creation reported, but file not found.' + else + Result := true + end else begin - //try alternate driver - driver := 'Microsoft Access Driver (*.mdb)'; - dbType := 'CREATE_DB="' + DatabaseFile + '"'; - retCode := SQLConfigDataSource(Hwnd(nil), ODBC_ADD_DSN, PChar(driver), PChar(dbType)); - end; - if retCode = 0 then - result := true - else - begin - result := false; ErrorCode := 0; ResizeErrorMessage := 0; // todo: verify how the DLL is called - use pointers?; has not been tested. GetMem(ErrorMessage, 512); try SQLInstallerError(1, @ErrorCode, ErrorMessage, SizeOf(ErrorMessage), @ResizeErrorMessage); + L := TStringList.Create; + try + L.Delimiter := ';'; + L.StrictDelimiter := true; + L.DelimitedText := ErrorMessage; + AErrorMsg := L.Text; + finally + L.Free; + end; finally FreeMem(ErrorMessage); end; - raise Exception.CreateFmt('Error creating Access database: %s', [ErrorMessage]); end; end; @@ -321,6 +349,12 @@ begin FilenameEdit.ButtonWidth := FilenameEdit.Height; end; +procedure TForm1.RgFormatClick(Sender: TObject); +begin + if FilenameEdit.Filename <> '' then + FilenameEdit.FileName := ChangeFileExt(FileNameEdit.FileName, EXT[RgFormat.ItemIndex]); +end; + procedure TForm1.StatusMsg(const AText: String); begin Statusbar1.SimpleText := AText; diff --git a/components/tvplanit/examples/datastores/flex/access/tools/readme.txt b/components/tvplanit/examples/datastores/flex/access/tools/readme.txt new file mode 100644 index 000000000..3675bd399 --- /dev/null +++ b/components/tvplanit/examples/datastores/flex/access/tools/readme.txt @@ -0,0 +1,23 @@ +Use the program CreateAccessDB to create an empty access databank, or one +which already contains the empty VisualPlanIt tables. + +The database file can be created in two versions + +* an *.mdb file of the old Access 97-2003. 32-bit only +* an *.accdb file for the newer Access 2007+ versions, 32 bit or 64 bit. + +Note: On Win 10 the accdb driver is not installed by default. Install the +AccessDatabaseEngine (free Microsoft download): + +* Office 2010: https://www.microsoft.com/en-us/download/details.aspx?id=13255 +* Office 2007: https://www.microsoft.com/en-us/download/confirmation.aspx?id=23734 + +Note: At the time of this writing the program does not run without error, +reason unknown. However, before the error appears, the database file already +has been successfully created. + +--------- + +The VisualPlanIt test program in the parent folder requires the database file +to be named either data.mdb or data.accdb. Copy the created mdb or accdb files +into the parent folder. \ No newline at end of file diff --git a/components/tvplanit/examples/datastores/flex/access/unit1.lfm b/components/tvplanit/examples/datastores/flex/access/unit1.lfm index 7bdf5af2c..5c30f01de 100644 --- a/components/tvplanit/examples/datastores/flex/access/unit1.lfm +++ b/components/tvplanit/examples/datastores/flex/access/unit1.lfm @@ -8,7 +8,7 @@ object Form1: TForm1 ClientWidth = 980 OnCreate = FormCreate OnDestroy = FormDestroy - LCLVersion = '1.6.4.0' + LCLVersion = '1.9.0.0' object Panel1: TPanel Left = 0 Height = 33 @@ -162,7 +162,6 @@ object Form1: TForm1 Align = alBottom TabStop = True TabOrder = 1 - KBNavigation = True DateLabelFormat = 'mmmm yyyy' DayHeadAttributes.Font.Height = -13 DayHeadAttributes.Font.Name = 'Tahoma' @@ -171,6 +170,7 @@ object Form1: TForm1 DrawingStyle = dsFlat EventDayStyle = [] HeadAttributes.Color = clBtnFace + KBNavigation = True OffDayColor = clSilver SelectedDayColor = clRed ShowEvents = True @@ -357,7 +357,7 @@ object Form1: TForm1 top = 168 end object VpResourceEditDialog1: TVpResourceEditDialog - Version = 'v1.05' + Version = 'v1.12' DataStore = VpFlexDataStore1 Options = [] Placement.Position = mpCenter @@ -401,6 +401,7 @@ object Form1: TForm1 DataSources.ContactsDataSource = DsContacts DataSources.TasksDataSource = DsTasks ResourceID = 0 + OnCreateTable = VpFlexDataStore1CreateTable left = 136 top = 101 ResourceFieldMappings = ( @@ -1294,15 +1295,14 @@ object Form1: TForm1 '' ) Transaction = SQLTransaction1 - Options = [] Driver = 'Microsoft Access Driver (*.mdb)' left = 136 top = 304 end object SQLTransaction1: TSQLTransaction Active = False + Action = caCommitRetaining Database = ODBCConnection1 - Options = [] left = 136 top = 368 end diff --git a/components/tvplanit/examples/datastores/flex/access/unit1.pas b/components/tvplanit/examples/datastores/flex/access/unit1.pas index 45df3ec5b..b0f61c016 100644 --- a/components/tvplanit/examples/datastores/flex/access/unit1.pas +++ b/components/tvplanit/examples/datastores/flex/access/unit1.pas @@ -2,6 +2,10 @@ unit Unit1; {$mode objfpc}{$H+} +// Select one of these +{$DEFINE MDB} +{.$DEFINE ACCDB} + interface uses @@ -68,8 +72,13 @@ type procedure QryGridAfterInsert(DataSet: TDataSet); procedure QryGridAfterEdit(DataSet: TDataSet); procedure TabControl1Change(Sender: TObject); + procedure VpFlexDataStore1CreateTable(Sender: TObject; TableName: String); private { private declarations } + procedure CreateContactsTable; + procedure CreateEventsTable; + procedure CreateResourceTable; + procedure CreateTasksTable; public { public declarations } end; @@ -82,9 +91,18 @@ implementation {$R *.lfm} uses - LazFileUtils; + LazFileUtils, + VpConst; +const + {$IFDEF MDB} + DB_NAME = '.\data.mdb'; // Access 97 file format + {$ENDIF} + {$IFDEF ACCDB} + DB_NAME = '.\data.accdb'; // Access 2007+ file format + {$ENDIF} + { TForm1 } // Adds a new resource @@ -125,22 +143,175 @@ begin QryAllTasks.Open; end; +procedure TForm1.CreateContactsTable; +begin + ODBCConnection1.ExecuteDirect( + 'CREATE TABLE Contacts ('+ + 'RecordID COUNTER, ' + + 'ResourceID INTEGER,' + + 'FirstName VARCHAR(50) ,'+ + 'LastName VARCHAR(50) , '+ + 'Birthdate DATE, '+ + 'Anniversary DATE, '+ + 'Title VARCHAR(50), '+ + 'Company VARCHAR(50), '+ + 'Department VARCHAR(50), '+ + 'Job_Position VARCHAR(30), '+ + 'AddressType1 INTEGER, '+ + 'Address1 VARCHAR(100), '+ + 'City1 VARCHAR(50), '+ + 'State1 VARCHAR(25), '+ + 'Zip1 VARCHAR(10), '+ + 'Country1 VARCHAR(25), '+ + 'AddressType2 INTEGER, '+ + 'Address2 VARCHAR(100), '+ + 'City2 VARCHAR(50), '+ + 'State2 VARCHAR(25), '+ + 'Zip2 VARCHAR(10), '+ + 'Country2 VARCHAR(25), '+ + 'Notes VARCHAR, '+ + 'EMail1 VARCHAR(100), '+ + 'EMail2 VARCHAR(100), '+ + 'EMail3 VARCHAR(100), '+ + 'EMailType1 INTEGER, '+ + 'EMailType2 INTEGER, '+ + 'EMailType3 INTEGER, '+ + 'Phone1 VARCHAR(25), '+ + 'Phone2 VARCHAR(25), '+ + 'Phone3 VARCHAR(25), '+ + 'Phone4 VARCHAR(25), '+ + 'Phone5 VARCHAR(25), '+ + 'PhoneType1 INTEGER, '+ + 'PhoneType2 INTEGER, '+ + 'PhoneType3 INTEGER, '+ + 'PhoneType4 INTEGER, '+ + 'PhoneType5 INTEGER, '+ + 'Website1 VARCHAR(100), '+ + 'Website2 VARCHAR(100), '+ + 'WebsiteType1 INTEGER, '+ + 'WebsiteType2 INTEGER, '+ + 'Category INTEGER, '+ + 'Custom1 VARCHAR(100), '+ + 'Custom2 VARCHAR(100),'+ + 'Custom3 VARCHAR(100), '+ + 'Custom4 VARCHAR(100) )' + ); + ODBCConnection1.ExecuteDirect( + 'CREATE UNIQUE INDEX piCRecordID ON Contacts(RecordID) WITH PRIMARY'); + ODBCConnection1.ExecuteDirect( + 'CREATE INDEX siCResourceID ON Contacts(ResourceID)'); + ODBCConnection1.ExecuteDirect( + 'CREATE INDEX siCName ON Contacts(LastName, FirstName)' ); + ODBCConnection1.ExecuteDirect( + 'CREATE INDEX siCCompany ON Contacts(Company)'); +end; + +procedure TForm1.CreateEventsTable; +begin + ODBCConnection1.ExecuteDirect( + 'CREATE TABLE Events ('+ + 'RecordID COUNTER, ' + + 'ResourceID INTEGER, '+ + 'StartTime DATETIME, '+ + 'EndTime DATETIME, '+ + 'Description VARCHAR(255), '+ + 'Location VARCHAR(255), '+ + 'Notes VARCHAR, ' + + 'Category INTEGER, '+ + 'AllDayEvent LOGICAL, '+ + 'DingPath VARCHAR(255), '+ + 'AlarmSet LOGICAL, '+ + 'AlarmAdvance INTEGER, '+ + 'AlarmAdvanceType INTEGER, '+ + 'SnoozeTime DATETIME, '+ + 'RepeatCode INTEGER, '+ + 'RepeatRangeEnd DATETIME, '+ + 'CustomInterval INTEGER)' + ); + ODBCConnection1.ExecuteDirect( + 'CREATE UNIQUE INDEX piERecordID ON Events(RecordID) WITH PRIMARY'); + ODBCConnection1.ExecuteDirect( + 'CREATE INDEX EResourceID ON Events(ResourceID)'); + ODBCConnection1.ExecuteDirect( + 'CREATE INDEX EStartTime ON Events(StartTime)'); + ODBCConnection1.ExecuteDirect( + 'CREATE INDEX EEndTime ON Events(EndTime)'); +end; + +procedure TForm1.CreateResourceTable; +begin + ODBCConnection1.ExecuteDirect( + 'CREATE TABLE Resources ( '+ + 'ResourceID COUNTER, ' + + 'Description VARCHAR(255), '+ + 'Notes VARCHAR, '+ // 1024 --> - + 'ImageIndex INTEGER, '+ + 'ResourceActive LOGICAL, '+ // BOOL --> LOGICAL + 'UserField0 VARCHAR(100), '+ + 'UserField1 VARCHAR(100), '+ + 'UserField2 VARCHAR(100), '+ + 'UserField3 VARCHAR(100), '+ + 'UserField4 VARCHAR(100), '+ + 'UserField5 VARCHAR(100), '+ + 'UserField6 VARCHAR(100), '+ + 'UserField7 VARCHAR(100), '+ + 'UserField8 VARCHAR(100), '+ + 'UserField9 VARCHAR(100) )' + ); + ODBCConnection1.ExecuteDirect( + 'CREATE UNIQUE INDEX piRResourceID ON Resources(ResourceID) WITH PRIMARY' + ); +end; + +procedure TForm1.CreateTasksTable; +begin + ODBCConnection1.ExecuteDirect( + 'CREATE TABLE Tasks ('+ + 'RecordID COUNTER, ' + + 'ResourceID INTEGER, '+ + 'Complete LOGICAL, '+ + 'Description VARCHAR(255), '+ + 'Details VARCHAR, '+ + 'CreatedOn DATETIME, '+ + 'Priority INTEGER, '+ + 'Category INTEGER, '+ + 'CompletedOn DATETIME, '+ + 'DueDate DATETIME)' + ); + ODBCConnection1.ExecuteDirect( + 'CREATE UNIQUE INDEX piTRecordID ON Tasks(RecordID) WITH PRIMARY' + ); + ODBCConnection1.ExecuteDirect( + 'CREATE INDEX siTDueDate ON Tasks(DueDate)' + ); + ODBCConnection1.ExecuteDirect( + 'CREATE INDEX siTCompletedOn ON Tasks(CompletedOn)' + ); +end; + // Setting up the database connection and the datastore. Preselect a resource // in the resource combo. procedure TForm1.FormCreate(Sender: TObject); begin - if not FileExists('data.mdb') then begin - MessageDlg('Database file "data.mdb" does not exist. ' + LineEnding + - 'Please run "CreateAccessDB" to create an empty Access database file.', + if not FileExists(DB_NAME) then begin + MessageDlg('Database file "' + DB_NAME + '" does not exist. ' + LineEnding + + 'Please run "CreateAccessDB" to create an empty Access database file.' + LineEnding + + 'Or copy an empty database file, data.mdb or data.accdb, from the '+ + 'folder "empty_db" to the current directory.', mtError, [mbOK], 0); Close;exit; end; try // Connection + {$IFDEF MDB} ODBCConnection1.Driver := 'Microsoft Access Driver (*.mdb)'; + {$ENDIF} + {$IFDEF ACCDB} + ODBCConnection1.Driver := 'Microsoft Access Driver (*.mdb, *.accdb)'; + {$ENDIF} ODBCConnection1.Params.Clear; - ODBCConnection1.Params.Add('DBQ=.\data.mdb'); + ODBCConnection1.Params.Add('DBQ=' + DB_NAME); ODBCConnection1.Connected := true; ODBCConnection1.KeepConnection := true; @@ -227,5 +398,14 @@ begin Grid.Columns[i].Width := 100;; end; +procedure TForm1.VpFlexDataStore1CreateTable(Sender: TObject; TableName: String + ); +begin + if TableName = ResourceTableName then CreateResourceTable; + if TableName = ContactsTableName then CreateContactsTable; + if TableName = EventsTableName then CreateEventsTable; + if TableName = TasksTableName then CreateTasksTable; +end; + end.