Create MS-Access Table via Delphi

Posted on 2013-01-17
Last Modified: 2013-02-08
Hello all,

First of all, I didn't write this question except after hammering the entire section and the Internet in general for days looking for the right answer, but unfortunately, some answers are too old some are not complete etc.

I saw the chapters in Delphi section but it requires ADOX 2.x dll which is not in my system. I have msadox.dll (ver 6) and I tried to import it but Delphi XE2 seems not to recognize the file. So I decided to go with the old-fashioned way.

Anyway, the program that will create a table after clicking a button. Everything is set up perfectly except the query sentence that will be sent to the database.

Here is the code:

procedure TAddIndivdualForm.OkClick(Sender: TObject);
Name: string;
QString: string;
Name := NameEdit.Text;

QString:= 'CREATE TABLE  ' + Name + ' (ProcessCaleneder DateTime, IndivName text(30), 
Position text(30), Mobile text(20), Salary Currency, Notes text(255))';

with MainForm.IndivdualsQuery do
  ConnectionString := 
'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mark\Desktop\Database.mdb;';

Open in new window

The result after clicking the button is: Syntax error in field definition. (because of the query)

the SQL.Add parameters were those of QString variable. But I took the string out, chopped it up a little and added string variables in between. String concatenation could be a huge mess sometimes in my personal opinion.

I tried to find the MS-Access queries reference on the net but I couldn't. I want to know how the sql queries were formed ?

So, to keep the post organized, here are my questions:

1. If you can correct the query sentence, I appreciate that.
2. Where do I find the queries reference ?
3. How do I make the XE2 IDE recognize ADOX 6.0 ?
4. The jet engine used in my example is ver 4.0 and .mdb, how do I make the IDE recognize access 2010 files ? will this enforce the other users of my app to install the distribution package ?
I appreciate your help even if you answer a portion of the questions.

Question by:gxs
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 38789946
1 - What is the error when you execute the sql string, first point of attack
2 - ?
3 - ADOX from memory (its been a while! was long winded - I would stick with SQL!)
4 - Make the connection string based on a setting for the IDE either from an ini file, xml file or registry setting when you install the application

Author Comment

ID: 38789984
Thanks MMTadmin for you reply,

1. It is in the SQL sentence, because when I delete some parts, It works fine.
2. I mean the access sql queries reference ( Every sql language has its own sql queries methods )
3. I considered many solutions such as SQL, Absolute Database but the program is so simple and the database is local.
4. The connection string wouldn't be valid if the IDE doesn't recognize the appropriate database driver.

Author Comment

ID: 38790010
Regarding the recognition of *.accdb files.

I have seen this video a couple of days ago. It is about this specific subject.

My IDE doesn't show the same driver when the guy in the video navigates to Microsoft Access Driver (*.accdb).

I have installed Microsoft Access Database Engine 2010 Redistributable, but still, no luck :p

and by the way, the language in the video is Portuguese and I don't speak it.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 38790150
AAAh, finally, I have stabbed the query problem in the heart X__X

The word ([Position] text(30)) appears to be somehow a reserved word ? Because of that I was asking about the queries reference.

Hope you answer the rest of the questions

Accepted Solution

gxs earned 0 total points
ID: 38790196
Well, Today was a weird one to me,

Above all of the problems above, the final problem was replacing the fields names in the SQL query string  with Arabic ones.

I modified the string accidentally into:

QString:= 'CREATE TABLE ' + Name + ' ([¿¿¿¿¿¿¿] DateTime, [¿¿¿¿¿] text(30), [¿¿¿¿¿¿ ¿¿¿¿¿¿¿] text(30), [¿¿¿¿¿¿] text(20), [¿¿¿¿¿¿] Currency, [¿¿¿¿¿¿¿] text(255))';

It worked like charm! without the square brackets, it would give me a ms-access syntax error due to the string being manipulated by the Arabic words.

I'm just amazed by how such a very tiny modification solved the problem X__X

Author Comment

ID: 38790199
^^^ experts-exchange doesn't support UNICODE? ooooh you kidding me :"(
LVL 38

Expert Comment

by:Geert Gruwez
ID: 38791929
in google type "ms access sql reference"
or any other "database type sql reference"

i have to admit, it's not user friendly navigating the websites to find the accurate statement on msdn for access
an index somewhere would be way more helpful

this list the syntax on the statements

reserverd word list

looks like position is not in this list

the square brackets are usually a must in access

Author Comment

ID: 38795258
No one is willing to help :"(


For days and years to come and for those who are looking for creating tables:

Here is a simpler way using TADOCommander:

procedure TAddStaffForm.OkClick(Sender: TObject);
Name: string;
QString: string;

Name := NameEdit.Text;
QString:= 'CREATE TABLE ' + '[' + Name + ']' + ' ([¿¿¿¿¿¿¿] DateTime, [¿¿¿¿¿¿ ¿¿¿¿¿¿¿] text(30), [¿¿¿¿¿¿] text(20), [¿¿¿¿¿¿] Currency, [¿¿¿¿¿¿¿] text(255))';

// StaffCommander is a TADOCommander component
MainForm.StaffCommander.CommandText := QString;

// StaffPuller is a TADOTable component
MainForm.StaffPuller.Active := False;
MainForm.StaffPuller.TableName := name;
MainForm.StaffPuller.Active := True;

Open in new window

The square brackets are added to avoid foreign language mistakes in the SQL string. They are not needed if the language is English, French etc. But recommended.
LVL 19

Expert Comment

ID: 38797339
I've been using KaDao Components for Delphi and MS Access for years without any problems.

Perhaps this may also be an option for you...
LVL 46

Expert Comment

ID: 38807983
You probably need to cast all your field and table names into ASCII.  The database engine you are using is probably old enough that it is not unicode capable.

Author Closing Comment

ID: 38867424
The best solution

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question