Recent

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

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Problem connecting to a MS Access Database with ODBC
« Reply #75 on: November 18, 2012, 09:47:34 am »
Thank you so much for all my great teachers,
help me please  %) and please see the project as attached herewith.
« Last Edit: November 18, 2012, 09:51:05 am 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 #76 on: November 18, 2012, 10:04:52 am »
Having the .lfm, .lpr, and lpi files would have been nice but ok I'll try and recreate the project.

Edit: one small hint: you have
Code: [Select]
procedure TForm1.MenuItem_ExitClick(Sender: TObject);
begin
  sq.Free;
  tr.Free;
  cn.Free;
end;

You could put that code in the Form's OnDestroy event instead. IIUC, if you press Alt-F4 or the close button at the top of the form, the menu code will not run and you'll have a memory leak.

Please also get rid of the free stuff here:
Code: [Select]
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  cn.Free;;
  tr.Free;
  sq.Free;
  application.Terminate;
end;
Of course nothing to do with your Access problems...
« Last Edit: November 18, 2012, 10:14:37 am by BigChimp »
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

Nebula

  • Jr. Member
  • **
  • Posts: 88
Re: Problem connecting to a MS Access Database with ODBC
« Reply #77 on: November 18, 2012, 10:13:56 am »
After a VERY quick look, I suspect it's to do with the freeing of the sql and connection objects, and them not existing when the sql text is being set.
Newbie testing Lazarus v1.0 - very impressed
Win 7 at work, XP and Linux Mint at home.
It all started with a ZX80 on a b/w telly........
Code: [Select]
Uses Smiles, GoodHumour, WantsToHelp;
{ never liked C - curly brackets are for comments! }

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 #78 on: November 18, 2012, 10:15:13 am »
After a VERY quick look, I suspect it's to do with the freeing of the sql and connection objects, and them not existing when the sql text is being set.
Exactly: e.g. TForm1.BitBtn2Click is freeing sq while it's used in button3.
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 #79 on: November 18, 2012, 10:41:25 am »
Corrected version attached with remarks in the code. You'll have to change the db location back to your place though, forgot that one.

Items include:
- parameter names spelled wrong (using field names instead of parameter names)
- the freeing mentioned above

Hope the remarks are clear enough, if not, please let me knoe.

PS: <rant>IF YOU HAD SUPPLIED A SMALL SAMPLE PROJECT THAT DEMONSTRATES YOUR PROBLEMS IN THE FIRST PLACE, WE WOULD HAVE FOUND THESE PROBLEMS DAYS AGO!</rant> Don't talk about your code, POST the code (and not snippets, but everything that is relevant)!

PS2: Did like your posting of screenshots of errors etc, that was very helpful.
« Last Edit: November 18, 2012, 10:43:30 am by BigChimp »
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 #80 on: November 18, 2012, 06:06:38 pm »
Thank you so much for your kind support, I will never forget this. From many replies in this topic, I saw that the SQL is not so easy  :o . Could you please introduce the sources for me to learn for more ?
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 #81 on: November 18, 2012, 06:11:53 pm »
Hope it works ;)

If you're asking about how to use SQL databases in Lazarus/FPC: have you read the wiki articles on databases I linked to? Especially the sqldb tutorial articles can be helpful...

For a general introduction to SQL, see the wiki article on documentation - there's a link to a pdf by Marco Cantu, Essential SQL, which can help you. Also there's some nice info on http://www.w3schools.com/SQl/
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 #82 on: November 18, 2012, 07:34:02 pm »
Thank you so much for your kind support, I will never forget this. From many replies in this topic, I saw that the SQL is not so easy  :o . Could you please introduce the sources for me to learn for more ?

On my opinion Lazarus/Freepascal database access is very simple in comparaison to .NET and Java DB access.

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

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Re: Problem connecting to a MS Access Database with ODBC
« Reply #83 on: November 19, 2012, 05:16:46 am »
I have uploaded the file on UploadHero

http://uploadhero.com/dl/jq8Thret

Hope that works now. anyway it's just contains how to connect to MS Access DB and to use dbnavs to manage it

regards

Appreciated.
Finally I make it work!!!I am very  :D
Attached my homework if it helps.I used a database name test.mdb and try to view each tables when select the table name in a combobox.It works (at least partly,that is when only view on table and then close the form,no exception caught) but exception thrown if I viewed one table and then select another table to view.Attached also the error mesage.Appreciated if anybody would like to take a look at the code and figure me out why the exception occurred.

What's more,when I tried to select  table(artists,person)  from database(test.mdb),I hard coded all table strings one by one ,but I am wondering if there is any more general way (say SQL statement) to retrieve the table names from test.mdb database and then added them to the combobox automatically.

Appreciated again for all the help!
« Last Edit: November 19, 2012, 10:49:07 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 #84 on: November 19, 2012, 10:43:52 am »
Thank you so much for your kind support, I will never forget this. From many replies in this topic, I saw that the SQL is not so easy  :o . Could you please introduce the sources for me to learn for more ?

Hi,

I think Lynda.com SQL essential trainning would be e good help for you to learn SQL

http://www.lynda.com/SQL-tutorials/essential-training/769-2.html

Hope that help.

regards
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 #85 on: November 21, 2012, 12:52:30 pm »
Updated the ms access wiki page:
http://wiki.freepascal.org/MS_Access
is UsePrimaryKeyAsKey: False needed? I seem to remember there was another, better workaround for that.

Additions welcome.
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 #86 on: November 21, 2012, 08:40:20 pm »
Updated the ms access wiki page:
http://wiki.freepascal.org/MS_Access
is UsePrimaryKeyAsKey: False needed? I seem to remember there was another, better workaround for that.

Additions welcome.

I think there should be a section for databases management comment error and how to fixe them. I mean the problem that the programmer can do in using databases component.
That would a good and necessary topic.

regards
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 #87 on: November 22, 2012, 08:55:54 am »
@anis: feel free to add/edit the wiki pages. There are also articles in French and Arabic if you prefer to edit those (assuming you speak those languages).
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 #88 on: November 24, 2012, 10:34:34 am »
From the same database,
Code: [Select]
sq.SQL.Text:='select FSDateTime from FromFingerScanner';

How can I rewrite the above code to get only date or only time of FSDateTime ?
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 #89 on: November 24, 2012, 10:47:32 am »
Perhaps it's a good idea to start a new thread if you're opening a new subject?

That might interest more people to answer & make it clearer it's not part of your original problem?

As for the answer: look into the SQL needed for Access to return only date or time (something with CAST perhaps? Or a date() function? Can't remember right now). I'd say it's much easier to use Pascal to convert the Tdatetime to only date or time later though.
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

 

TinyPortal © 2005-2018