Linking to MS Access Database on Local Drive

Creating desktop or client-server database solutions?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
SteveX
Posts: 5
Joined: Wed Jul 12, 2006 7:49 pm

Linking to MS Access Database on Local Drive

Post by SteveX » Thu Jul 20, 2006 6:03 pm

I am very new to Revolution and I would like some step by step instructions for accessing data in an existing MS Access database. I would like to eventually learn how to add/modify/remove/query this data. Basically I want to be able to build the database structure in MS Access and then use Revolution to build the User Interface. Is this possible? I know I have a ton of new terminology to learn during the conversion from Access to Revolution so be gentle.... :lol:

Thanks,

Steve X.
Steve X.
Programmer Wannabe

Lynn
Posts: 109
Joined: Thu Feb 23, 2006 7:43 pm

Re: Linking to MS Access Database on Local Drive

Post by Lynn » Tue Jul 25, 2006 10:41 pm

SteveX wrote:I am very new to Revolution and I would like some step by step instructions for accessing data in an existing MS Access database. I would like to eventually learn how to add/modify/remove/query this data. Basically I want to be able to build the database structure in MS Access and then use Revolution to build the User Interface. Is this possible? I know I have a ton of new terminology to learn during the conversion from Access to Revolution so be gentle.... :lol:

Thanks,

Steve X.
Hi Steve,

What version of Revolution are you using?
Best regards,


Lynn Fredricks
Paradigma Software - Ultra Fast Database & Reporting Platform for LiveCode
http://www.paradigmasoft.com

SteveX
Posts: 5
Joined: Wed Jul 12, 2006 7:49 pm

Version

Post by SteveX » Wed Jul 26, 2006 8:55 pm

Version 2.7.2

I have just recently downloaded MySQL server and associated tools. If that would be easier then I can give it a shot as well. I can get Revolution to connect to the MySQL server running locally, however, I haven't been able to figure out all the commands necessary to be able to write to the tables and then recall the information. I really want to learn to use Revolution, but thus far I have had a great deal of difficulty trying to transition from MS Access to Revolution.... I haven't given up yet.... I'm a glutton for punishment..

Thanks for any and all help you can give. (a step-by-step book would be nice :D

Steve X.
Steve X.
Programmer Wannabe

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Mon Jul 31, 2006 1:05 am

Hi Steve,

While you cannot directly connect to MS Access databases from Revolution, you can use Rev's generic ODBC driver to connect to a Data Source Name (DSN) that you setup in the ODBC Control Panel of your Windows computer.
Think of a DSN as a shortcut that you can reference by name.

So to connect to an MS Access database from Rev, use the following steps:
1) Create a DSN in the ODBC Control Panel
. Go to the ODBC Data Source Administrator - Control Panel
. Go to the tab 'System DSN' and click the 'Add' button
. Select the line 'Microsoft Access Driver (*.mdb)' and click 'Finish'
. Give it a name that helps you remember which database it is (you will use this later on) - for this example we'll use 'RevAccessTest'
. Select the .mdb file that is the actual Access database
2) Connect to the DSN from Revolution using the ODBC database type

Code: Select all

put revdb_connect("odbc","RevAccessTest",,,) into tConnectionID
-- check if we could connect; if not, show an error
if tConnectionID is not a number then
  answer error tConnectionID
else
  -- we're connected; execute some query
  put "SELECT * FROM customers" into tQuery
  put revdb_querylist(tab,return,tConnectionID,tQuery) into tQueryList
  -- close the connection
  get revdb_disconnect(tConnectionID)
end if
As for MySQL, you don't need to setup a DSN or anything to connect to it from Revolution. But you'll have to create the schema yourself. I believe you can download a graphical MySQL that will make this process a lot easier, in a way close to Access and SQLServer.

Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

SteveX
Posts: 5
Joined: Wed Jul 12, 2006 7:49 pm

Post by SteveX » Mon Jul 31, 2006 1:54 am

Thanks but I figured out how to connect to the database via ODBC. However, I don't know how to add, modify, delete records.

I'm getting closer but so far - no cigar....

Thanks for any help.. :)

Steve X
Steve X.
Programmer Wannabe

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Mon Jul 31, 2006 2:18 am

Hi Steve,

You're going to have to dig in and learn the SQL language:
- use SELECT to find records
- use INSERT to create a record
- use UPDATE to modify records
- use DELETE to delete records

For an introduction to SQL, see:
- http://www.w3schools.com/sql/default.asp
- http://www.quartam.com/tutorials/sqltutorial.htm

Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

SteveX
Posts: 5
Joined: Wed Jul 12, 2006 7:49 pm

Post by SteveX » Mon Jul 31, 2006 11:16 pm

Thank you Jan!

That is what I was most unsure about. Do I only use the SQL language for the database commands or will I be using SQL commands for all aspects of Revolution?:roll:

I've been away from the coding aspect of programming for a while now so I've got a lot to learn. MS Access doesn't necessarily require a huge amount of coding to create a database and build a user interface for that database.

I love to learn new things so - Here I Go... :lol:
Steve X.
Programmer Wannabe

SteveX
Posts: 5
Joined: Wed Jul 12, 2006 7:49 pm

Post by SteveX » Tue Aug 01, 2006 1:42 am

Jan,

I am working with the MySQL database and I am trying to use the SQL command to insert information but it's not working. Here is the code I am using in the script for my button:
revsqlexecute "Insert Into Members (Firstname,Lastname) values Field 'FirstName', Field 'LastName'"

I have also tried the following:
revsqlexecute "Insert Into Members values Field 'FirstName', Field 'LastName'"

What am I doing wrong? I am not getting an error when I apply the script but when I test the program I get the error:
Type Handler: can't find handler
Object NextBttn
Line revsqlexecute "Insert Into Members values Field 'FirstName', Field 'LastName'"
Hint revsqlexecute

This doesn't tell me anything!!

I am not getting any information sent to the SQL database.

HELP!!! :?
Steve X.
Programmer Wannabe

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Tue Aug 01, 2006 4:11 am

Hi Steve,

"Can't find handler" means exactly that: the interpreter cannot find a command (aka 'message handler') by the name of 'revexecutesql'

To execute SQL queries that modify the database contents (such as INSERT, UPDATE and DELETE), you can use either the 'revdb_execute' function or the 'revSQLExecute' command.
Both require as their first parameter a connection ID, and as their second parameter an SQL query. The difference between the two is that the first is a function and thus returns a result, and the second will update the global variable called 'the result' when it's finished.

Furthermore, Revolution doesn't really look at the query to fill it up with the contents of fields or variables. You havez to construct the complete query yourself as a string.

Code: Select all

on mouseUp
  put revdb_connect("odbc","RevAccessTest",,,) into tConnectionID
-- check if we could connect; if not, show an error
if tConnectionID is not a number then
  answer error tConnectionID
else
  -- we're connected; execute the INSERT query
  put "INSERT INTO members VALUES ('" & \
      field "FirstName" & "','" & field "LastName" & "')" \
      into tQuery
  put revdb_execute(tConnectionID,tQuery) into tQueryResult
  -- close the connection
  get revdb_disconnect(tConnectionID)
end if
end mouseUp
I recommend that you download Sarah Reichelt's tutorials about using MySQL databases from Revolution: http://www.troz.net/Rev/tutorials.php
This way, you can pick up all hte necessary skills to connect to a database from Revolution, and then deal with the idiosyncracies of connecting to Access databases.

Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

gimpeltf
Posts: 29
Joined: Wed Nov 08, 2006 5:14 pm

Update Query

Post by gimpeltf » Thu Jul 05, 2007 5:40 pm

I'm having trouble Updating a field when there's an apostrophe (single quote) in the field. The same code put directly into Access works fine so it has to do with the interface.

put "Update Campers Set LastName='" & fld LastName & "' where ID=" & fld WID & ";" into uQuery
put revdb_execute(tConnectionID,uQuery) into dummy

Of course, there would be a problem with unmatched quotes in the above if the Name was O'Boyle.
I've taken out the single quotes in the code and replaced with & quote.
the value in uquery wouls be
Update Campers Set LastName="O'Boyle" where ID=365;
This code would work directly in Access but returns an error of
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

Any thoughts?

Gimp

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Mon Jul 09, 2007 12:29 pm

In 'standard' SQL, strings are opened and closed with apostrophes, not quotes - some SQL databases may allow both quotes and apostrophes, but Access is not one of them.
Try 'escaping' the apstrophe instead:

Code: Select all

on mouseUp
  put "Update Campers Set LastName='" & SQL_Escaped(fld LastName) & "' where ID=" & fld WID & ";" into uQuery
  put revdb_execute(tConnectionID,uQuery) into dummy
  answer dummy
end mouseUp

function SQL_Escaped pData
  replace "'" with "''" in pData
  return pData
end SQL_Escaped
Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

gimpeltf
Posts: 29
Joined: Wed Nov 08, 2006 5:14 pm

Post by gimpeltf » Mon Oct 08, 2007 4:19 pm

First off, I found the answer to my previous question in this thread on another thread before coming back here.

But, I have a question about setting up the ODBC path and other info. I know how to do it through the Control Panel manually. Is there a way to do it via Revolution?
I would like to be able to have the application itself use an Ask File dialog to allow the user to locate his file and then plug in all of the ODBC stuff that's needed.

Gimp

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Tue Oct 09, 2007 12:02 pm

While you can't do it directly using Revolution commands, you can do this on Windows by using VBScript. I found a VBScript here: http://www.enterpriseitplanet.com/resou ... hp/3089341

The easiest way would be to keep a template VBScript in a property, replace placeholders (for the database path, etc.) with the actual contents, then spit that out to a temp file and execute it using the 'shell' function:

Code: Select all

on mouseUp
  answer file "Select an Access database"
  if it is empty then exit mouseUp
  put it into tDatabasePath
  -- now get the template script and fill it up
  put the uDSNCreationScript into tVBScript
  replace "[[DatabasePath]]" with tDatabasePath in tVBScript
  ## TIP: look into the 'merge' function!
  -- put the script into a temporary file
  put the tempName & ".vbs" into tScriptPath
  put tVBScript into URL ("file:" & tScriptPath)
  -- and execute it silently
  put the hideConsoleWindows into tSavedHCW
  set the hideConsoleWindows to true
  get shell("cscript.exe //nologo" && tScriptPath)
  set the hideConsoleWindows to tSavedHCW
  -- make sure to clean up the tempprary file
  send "delete file" && quote & tScriptPath & quote to me in 1 second
  -- this gives enough time for the script to run before you delete it
end mouseUp
Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

gimpeltf
Posts: 29
Joined: Wed Nov 08, 2006 5:14 pm

Post by gimpeltf » Mon Oct 29, 2007 12:33 am

Thanks, I'll give it a try if it isn't over my head. ;)

Adrien Bron
Posts: 5
Joined: Tue Oct 08, 2013 2:26 pm
Contact:

Re: Linking to MS Access Database on Local Drive

Post by Adrien Bron » Tue Oct 08, 2013 2:47 pm

Thanks for the information, has been very useful to me. :D

Post Reply

Return to “Databases”