Recent

Author Topic: Problem connecting to a MS Access Database with ODBC  (Read 86995 times)

anis2505

  • Full Member
  • ***
  • Posts: 201
Re: Problem connecting to a MS Access Database with ODBC
« Reply #30 on: November 03, 2012, 09:40:05 pm »
Dear All,

How can I add 'file.dsn' into codes?

In http://wiki.lazarus.freepascal.org/MS_Access and  sample project from http://www.mediafire.com/file/ne1jx3zpnwzefq3/msaccesstest2.zip

There is a separate file namely 'file.dsn' but I would like to write it in codes and do not need to drag many controls(TODBCConnection, TSQLTransaction and TSQLQuery) onto form.

And another question, now I use MS Access 2010. The extention is '.accdb', not '.mdb' . How should I write codes for those ?

My codes (it does not work)...

Code: [Select]
implementation

{$R *.lfm}

{ TForm1 }

var
  cn : TODBCConnection;
  sq : TSQLQuery;
  tr : TSQLTransaction;

procedure TForm1.FormCreate(Sender: TObject);

begin

  // Set up
  cn := TODBCConnection.Create(nil);
  tr := TSQLTransaction.Create(nil);
  tr.DataBase := cn;
  tr.Action   := caCommit;

  cn.Transaction := tr;

  sq := TSQLQuery.Create(nil);
  sq.DataBase:= cn;
  sq.Transaction := tr;

  sq.UsePrimaryKeyAsKey:=false;

  cn.Driver := 'Microsoft Access Driver (*.accdb)';
  cn.Params.Add('DBQ=D:\KTH.accdb');

  cn.CloseDataSets;

  sq.SQL.Clear;
  sq.sql.text:='select FSpersonnelID from FromFingerScan';
  sq.open;

  while not sq.EOF do begin
  RadioGroup1.Items.Add(trim(uppercase(sq.FieldByName('FSpersonnelID').Text)));
  sq.Next;
  end;

  sq.close
 
                       



Hi,

I'm using MS Access 2010 with accdb extention

I'm using controls it works for me all I do is on the property fileDSN of the ODBCConnection control i provide the FULL PATH (X:\...\file.dsn)

nothing more I don't provide the driver or anything else just the full file path of the dsn file

found this too

http://wiki.lazarus.freepascal.org/MS_Access

------------------------------
Lazarus #1.0.2 on Windows 7 64 bits
« Last Edit: November 03, 2012, 10:18:23 pm by anis2505 »
Dear Confucius you said {A picture is worth a thousand words}
I say {a good example is worth a thousand words}

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Problem connecting to a MS Access Database with ODBC
« Reply #31 on: November 04, 2012, 12:33:20 am »
Thank you,
but what I need is moving all codes inside 'file.dsn' and write in code of project.
Not like this...
Code: [Select]
  //connection
 ODBCConnection1.FileDSN := ExtractFilePath(Application.ExeName) + 'file.dsn';
 

Lazarus 1.2.4 / Win 32 / THAILAND

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Problem connecting to a MS Access Database with ODBC
« Reply #32 on: November 04, 2012, 12:04:34 pm »
Only AnyDAC -> http://www.da-soft.com/anydac/ can do.  :'(


Finally after reading in Lazarus wiki, I have to create '.dsn'.

Are there any other non-commercial components equivalent to AnyDAC ?
« Last Edit: November 04, 2012, 12:49:40 pm by asdf »
Lazarus 1.2.4 / Win 32 / THAILAND

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Problem connecting to a MS Access Database with ODBC
« Reply #33 on: November 04, 2012, 02:25:44 pm »
I'm a bit unclear about what you did. Did you try entering the ODBC details to create a DSN-less connection?
http://wiki.lazarus.freepascal.org/ODBCConn#Without_a_DSN

Did you get error messages then?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Problem connecting to a MS Access Database with ODBC
« Reply #34 on: November 04, 2012, 04:45:06 pm »
ํYes, what I only need is dsn-less.

I tried

Code: [Select]
  sq.UsePrimaryKeyAsKey:=false;
  cn.Driver := 'Microsoft Access Driver (*.accdb)';
  cn.Params.Add('DBQ=D:\KTH.accdb')

But error.
Lazarus 1.2.4 / Win 32 / THAILAND

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Problem connecting to a MS Access Database with ODBC
« Reply #35 on: November 04, 2012, 05:30:46 pm »
Ok. 2 ideas (haven't used accdb myself though)
1. Make sure the .accdb driver supports the DBQ=bla notation for specifying the database
2. If you haven't already: make sure you have a 64 bit accdb ODBC driver if compiling a 64 bit program and a 32 bit ODBC driver if compiling a 32 bit program.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Problem connecting to a MS Access Database with ODBC
« Reply #36 on: November 04, 2012, 05:37:50 pm »
Also, make sure the driver name you use in cn.Driver is correct. I could only find this example:
http://www.connectionstrings.com/access-2007#microsoft-access-accdb-odbc-driver
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

anis2505

  • Full Member
  • ***
  • Posts: 201
Re: Problem connecting to a MS Access Database with ODBC
« Reply #37 on: November 04, 2012, 08:37:36 pm »
Hi,

I used the code below and it works fine

Code: [Select]
cn.Driver:='Microsoft Access Driver (*.mdb, *.accdb)';
  cn.Params.Add('DBQ='+ExtractFilePath(Application.ExeName) + 'yourdb.accdb');
  cn.Connected:=True;
  cn.KeepConnection:=True;
  SQLQuery1.SQL.Text:='SELECT * FROM table1';
  SQLQuery1.Open;     
Dear Confucius you said {A picture is worth a thousand words}
I say {a good example is worth a thousand words}

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Problem connecting to a MS Access Database with ODBC
« Reply #38 on: November 05, 2012, 04:04:18 am »
Today I tried DSN-less again, but it worked. I am so  8-) now.

Thank you for all kind suggestions from all of you.

Maybe I placed codes in wrong lines or wrong sequence, or misspelled the data field name or table name.

 ;D
« Last Edit: November 05, 2012, 10:11:23 am by asdf »
Lazarus 1.2.4 / Win 32 / THAILAND

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Re: Problem connecting to a MS Access Database with ODBC
« Reply #39 on: November 12, 2012, 09:47:28 am »
Hi,

I used the code below and it works fine

Code: [Select]
cn.Driver:='Microsoft Access Driver (*.mdb, *.accdb)';
  cn.Params.Add('DBQ='+ExtractFilePath(Application.ExeName) + 'yourdb.accdb');
  cn.Connected:=True;
  cn.KeepConnection:=True;
  SQLQuery1.SQL.Text:='SELECT * FROM table1';
  SQLQuery1.Open;     

Sorry for my stepping in but I've encountered the same issue.
Are you all talking about the dsn-less connection via ODBC with Access database (*.mdb)?With the above connection code,it still failed to connect to an access database if dsn is not defined in ODBC source panel a 'database =' property is not provided in IDE.

All I want is just to connect an access database (*.mdb) with sqlDB component(TODBCconnection,QSqlTransaction,TSqlQuery...),the most important point is there is NO need to set up a dsn in the ODBC Source panel!Something like in C# below code is sufficient to connect to an access *.mdb database:
Code: [Select]
OleDbConnection nwindConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;")

Solution would be appreciated.

Regards,
Sam
« Last Edit: November 12, 2012, 09:59:28 am by Goodman H__ »
fpc:2.6.1 Lazarus:1.1 SVN39277
OS:win 7

anis2505

  • Full Member
  • ***
  • Posts: 201
Re: Problem connecting to a MS Access Database with ODBC
« Reply #40 on: November 12, 2012, 11:55:08 am »
Hi,

I used the code below and it works fine

Code: [Select]
cn.Driver:='Microsoft Access Driver (*.mdb, *.accdb)';
  cn.Params.Add('DBQ='+ExtractFilePath(Application.ExeName) + 'yourdb.accdb');
  cn.Connected:=True;
  cn.KeepConnection:=True;
  SQLQuery1.SQL.Text:='SELECT * FROM table1';
  SQLQuery1.Open;     

Sorry for my stepping in but I've encountered the same issue.
Are you all talking about the dsn-less connection via ODBC with Access database (*.mdb)?With the above connection code,it still failed to connect to an access database if dsn is not defined in ODBC source panel a 'database =' property is not provided in IDE.

All I want is just to connect an access database (*.mdb) with sqlDB component(TODBCconnection,QSqlTransaction,TSqlQuery...),the most important point is there is NO need to set up a dsn in the ODBC Source panel!Something like in C# below code is sufficient to connect to an access *.mdb database:
Code: [Select]
OleDbConnection nwindConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;")

Solution would be appreciated.

Regards,
Sam

Hi,

The C# code means this
For the database driver
Code: [Select]
Provider=Microsoft.Jet.OLEDB.4.0;
Your database
Code: [Select]
Data Source=C:\mydatabase.mdb;


It's the same here

you set the driver like this:
Code: [Select]
cn.Driver:='Microsoft Access Driver (*.mdb, *.accdb)';

The database like this
Code: [Select]
cn.Params.Add('DBQ=C:\mydatabase.mdb');

I hope that will help
Dear Confucius you said {A picture is worth a thousand words}
I say {a good example is worth a thousand words}

anis2505

  • Full Member
  • ***
  • Posts: 201
Re: Problem connecting to a MS Access Database with ODBC
« Reply #41 on: November 12, 2012, 12:01:27 pm »
Today I tried DSN-less again, but it worked. I am so  8-) now.

Thank you for all kind suggestions from all of you.

Maybe I placed codes in wrong lines or wrong sequence, or misspelled the data field name or table name.

 ;D

Hi,
Could you provide the code you used.

Thanks in advance.
Dear Confucius you said {A picture is worth a thousand words}
I say {a good example is worth a thousand words}

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Problem connecting to a MS Access Database with ODBC
« Reply #42 on: November 12, 2012, 12:42:30 pm »
Note: I'm sure people would appreciate an update of
http://wiki.lazarus.freepascal.org/ODBCConn#Connecting_to_MS_Access
... and/or a rewrite of
http://wiki.lazarus.freepascal.org/MS_Access
... which seems a bit well... one sided at least (focus on File-based DSN connection)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

anis2505

  • Full Member
  • ***
  • Posts: 201
Re: Problem connecting to a MS Access Database with ODBC
« Reply #43 on: November 12, 2012, 02:48:31 pm »
Note: I'm sure people would appreciate an update of
http://wiki.lazarus.freepascal.org/ODBCConn#Connecting_to_MS_Access
... and/or a rewrite of
http://wiki.lazarus.freepascal.org/MS_Access
... which seems a bit well... one sided at least (focus on File-based DSN connection)

GREAT THANKS
Dear Confucius you said {A picture is worth a thousand words}
I say {a good example is worth a thousand words}

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Problem connecting to a MS Access Database with ODBC
« Reply #44 on: November 13, 2012, 05:24:32 am »
in MS Access database, there is a field and its data type is 'Date/time'.

Code: [Select]
  sq.SQL.Text := 'select FSDT from TimeTable order by FSDT'; // FSDT is a field of Date/Time data type
  sq.Open;
  stringgrid2.RowCount:=sq.RecordCount+1;

    while not sq.EOF do
      begin
        stringgrid2.Cells[1,sq.RecNo]:=sq.FieldByName('FSDT').Text;
        sq.Next;
      end;
  sq.Free;
  cn.Free;

It crashed, and could you please teach me how to design the database upon Date/Time type ?
And what is the correct sql statement for Date/Time type field ?
« Last Edit: November 13, 2012, 07:17:58 am by asdf »
Lazarus 1.2.4 / Win 32 / THAILAND

 

TinyPortal © 2005-2018