Managing MySQL Database with Microsoft Access Problem - Plan - Procedure: Managing MySQL Database with Microsoft Access

Thursday, June 4, 2009

Managing MySQL Database with Microsoft Access

There are many advantages of having proper SQL Database engine from using a normal Access database. I won't go through the comparison, but the one that really looks attractive, is that you can have many users working on the same database concurrently. However Access have a lot good features, such as GUI front-end for queries, table management, and even programmable GUI forms. So in this little article I'd like to describe a quite a common method of using Microsoft Access as a front-end for the MySQL database.
Here are the pre-requisites of this procedure
Server
  • MySQL database with remote connections allowed
Client
  • MySQL ODBC driver
  • Microsoft Access
In this example I am using Microsoft Access XP (2002) but it should work exactly the same way as on 2003 (and maybe 2007(?)).

Step 1: Setting up the Server
This step is not needed if you run MySQL on the same machine as your client. Let's assume that you run it on machine within your network. Hence the following topology:
In this case what you have to do first is to make sure that your mysql daemon is listening to its network address rather then localhost.
Check your MySQL configuration file which is on OpenSUSE (and a lot of other distros) is located in /etc/my.cnf
Find the following line:
bind-address:
If you have there your server network address (192.168.0.100 in this example), then leave it as it is. If the address is 127.0.0.1 - then change it to your network address. If you have made changes, restart the server.
# /etc/init.d/mysql restart

Second thing to do is to give permission to a database from a remote host. In our case it would be 192.168.0.101 machine. Log in to mysql using the command line client as root:
$ mysql -uroot -p
enter your password, and you should get a mysql terminal. In this example I want to be able to manage a database called movietime, and the username is admin. So, to sum up all I want to achieve here is to allow full access of user admin to database movietime from a host 192.168.0.100. This can be done with the Grant command the following way:
> GRANT ALL ON movietime.* TO 'admin'@'192.168.0.100';
then run Flush Privileges for server to reload GRANT tables
> FLUSH PRIVILEGES
quit the shell
> exit
You can read more on Database Grant Syntax and Security here

By this stage, you should have a MySQL service listening to its port (3306) on its network address (192.168.0.100) and accept connections from 192.168.0.100 for a user admin, giving him all privileges on database movietime.
Step 2: Testing the remote connection
If you have text based client installed on your workstation, try to connect with it to a database server
mysql.exe -u admin -h 192.168.0.100 -d movietime -p

Omit option -p if you allow admin to connect without a password. After a password prompt you should get a text based interface to your database, pretty much the same as back on your server side.
Step 3: Getting MySQL ODBC driver and configuring driver source


You can get ODBC driver for MySQL from here. Download and install it, nothing much else to do with it. Once its installed go to Start -> Control Panel (or Settings -> Control panel if in classic view) -> Administrative Tools -> Data Sources -> System DSN
Then Click on Add, select MySQL ODBC 5.1 Driver (bottom of the list) fill out the form like on the picture (adjust your settings, obviously).

Click on Test, should expect it to work by now. If test is successful, click OK, and move on to the last stage.
Step 4: Linking Access Database to ODBC
Last step is to link the database in access. Open Microsoft Access and create a new database. This database (mdb) is going to be a front-end wrapper for the MySQL ODBC source. Then click on File -> Get External Data-> Import -> ODBC Databases -> Machine Data Import -> select MovieTime database.
Then do File -> Get External Data -> Link Tables -> ODBC Databases -> Machine Data Import -> select MovieTime database
This step might not be needed, though you'll have to do it everytime you change database schema. You should see all your database tables in Access now. Try adding a record by clicking one of tables and adding one more row:
The result should instantly be in the MySQL. Say, I add a record called '5, Brave Heart' into the movies table.
Unfortunately, does not work dynamically other way around. You'll have to refresh the table in Access to see changes made on MySQL side.
So this is how you can use Microsoft Access as a front end for MySQL database.





0 comments:

Post a Comment