Tải bản đầy đủ - 0 (trang)
Hack 68. Use Access as a Front End to MySQL

Hack 68. Use Access as a Front End to MySQL

Tải bản đầy đủ - 0trang

MySQLdoeshaveaWindows-basedutilityyoucanusefor

creatingandeditingthestructureofthetablesinyourMySQL

databases.TheolderversionoftheprogramiscalledMySQL

ControlCenter,andithasbeenreplacedbyMySQLQuery

Browser.YoucandownloadeitherprogramfromtheMySQLweb

site(http://dev.mysql.com/downloads/)forfree.Themanual

forMySQLQueryBrowserisavailableonlineat

http://dev.mysql.com/doc/querybrowser/en/,oryoucan

downloaditasaPDForWindowsHelpfile.

WhenyoustartMySQLQueryBrowser,youspecifytheserver

onwhichMySQLruns,yourMySQLusername,andyour

password.Onceconnected,youseealistofthetablesforwhich

youhaveaccesspermission,andyoucanvieworeditthedata

orstructureofthetables,asshowninFigure7-44.



Figure7-44.MySQLQueryBrowser



MySQLQueryBrowserisuseful,butit'snotthetooltogiveto

yourdatabaseusers.Forexample,youcan'tcreateformswith

datavalidation,comboboxes,orsubforms,andyoucan'tcreate

formattedreports.Forthis,youneedAccess.

ForAccesstoconnecttoaMySQLdatabase,youneedtoinstall

theMySQLConnector/ODBCdriver(alsocalledtheMySQL

ODBCorMyODBCdriver).ThisdriverletsAccesscommunicate

withMySQLviaOpenDataBaseConnectivity(ODBC).Youcan

downloadtheMySQLODBCdriverfrom

(http://dev.mysql.com/downloads/)forfree.Afteryouinstallit,

anewoptionappearswhenyoulinktoexternaltablesfroman

Accessdatabase.

Youdon'tneedtoinstallMySQLQueryBrowseronevery

computeronwhichyourAccessdatabasewillrun;youneedit

onlyifyouplantouseittolookatorchangethestructureof

thetablesinyourMySQLdatabase.Buteverycomputeron

whichyourAccessdatabaserunsneedstheMySQLODBCdriver

installedbecauseAccessusesthedrivereverytimeyouopena

linkedMySQLtable.

IfyouplantoconnecttoaMySQLdatabaseovertheInternet,

yourrequestswillprobablyneedtopassthroughoneormore

firewalls.TheMySQLODBCdrivercommunicatesoverport

3306,sothisportmustbeopenonallthefirewallsbetween

yourcomputerandtheMySQLserver.Youcanspecifya

differentportwhenyoucreatethelinkfromAccesstoMySQL,

incaseyourMySQLserverisconfiguredtouseanonstandard

portnumber.



7.11.2.LinkingtoMySQLTables

Onceyou'vegottheMySQLODBCdriverinstalled,linking



AccesstoMySQLtablesrequirestwosteps:makingsurethe

tablescontaintherightfieldsandmakingthelink.Forthe

Access/MySQLlinktoworkrightwheneditingdataintothe

tables,eachtabletowhichyoulinkneedstohavethefollowing

twofields(thenamesofthefieldsdon'tmatter):



AutoNumber

InMySQL,thisisanINT(integer)fieldofsize11withthe

UNSIGNEDandAUTOINC(autoincrement)optionsselected.This

fieldmustbetheprimarykeyforthetable.



Date

Thisfieldisupdatedautomaticallyanytimetherecordis

edited.InMySQL,thisisaTIMESTAMPfield.

Mosttableshavethesetwofieldsanyway;gooddatabase

designsuggestsusinganAutoNumberfieldastheprimarykey

formosttables.However,ifyourMySQLtablesdon'thavethese

fields,youneedtouseMySQLQueryBrowserorsomeother

tooltoaddthem.

CreatingthelinksinAccessisasnap.ChooseFile Get

ExternalData LinkTablestodisplaytheLinkdialogbox.Set

thefiletypetoODBCDatabases,andyouseetheSelectData

Sourcedialogbox,whichlistsODBCdatabasesyou'veused

before,intheformofDataSourceName(DSN)filesthat

containtheconnectioninformationforthedatabase.Ifyouare

openingatableinadatabaseyou'veusedbefore,choosethe

DSNfileforthedatabase,clickOK,andchoosethetablesto

link.

IfyouarelinkingtoaMySQLdatabaseforthefirsttime,click

theNewbuttonintheSelectDataSourcedialogbox,choose



MySQLODBCDriverfromthedriverlist(it'sneartheend),click

Next,specifyanamefortheDSNfileyouarecreatingtostore

theconnectioninformation,andclickFinish.You'llseethe

MySQLODBCDriverDSNConfigurationdialogbox,shownin

Figure7-45.



Figure7-45.Specifyingconnectioninformation

foraMySQLdatabase



FillinthehostnameoftheMySQLserver,thenameofthe

databasethatcontainsthetablestowhichyouwanttolink,and

yourMySQLusernameandpassword.IfyourMySQLserver

doesn'tcommunicateoverport3306(thedefault),enterthe

portnumber,too.Ifyouwanttomakesureyourconnection

informationiscorrect,clickTestDataSource,andAccesswill

trytoconnecttotheMySQLdatabaseandtellyouwhetherit

succeeded.



WhenyouclickOK,AccessdisplaystheLinkTablesdialogbox

(thesamedialogboxyouusewhenlinkingtotablesinother

Accessdatabases).However,inadditiontothelistoftables,

youcanselecttheSavePasswordcheckbox.Thisoptionis

misnamedbecauseAccessstorestheMySQLpasswordno

matterwhat;thischeckboxactuallycontrolswhetheritstores

theMySQLusername.Ifyoudon'tselectthisoption,youhave

toentertheMySQLusernameeachtimeyourAccessdatabase

makesitsinitialconnectiontotheMySQLdatabase.



Ifyouhaveanysecurityconcernsabouttheinformationinthetable,

don'tchecktheSavePasswordcheckboxwhenyoucreatealinktoa

MySQLtable.IfyousaveboththeMySQLusernameandpasswordin

theAccessdatabase,anyonewhocanopentheAccessdatabasecan

makechangestotheinformationinyourMySQLdatabase.



LinkedtablesfromMySQLdatabasesappearontheTableslistin

theAccessDatabasewindowwithablue-greenglobeicon

ratherthantheusualboxicon.Youcan'tchangethestructure

oflinkedtables,andyoucan'tcreaterelationshipsthatenforce

referentialintegritybetweentables,butotherwise,youcanuse

thedatajustasifitwereinyourAccessdatabase.Ifyou

changethestructureofatableintheMySQLtable,besureto

relinkitbychoosingTools DatabaseUtilities LinkedTable

Manager.



7.11.3.HackingtheHack

WhenyouspecifytheinformationaboutaMySQLdatabase,

AccesscreatesaDSNfileandstoresitintheC:\Program

Files\CommonFiles\ODBC\DataSourcesfolder(assuming

WindowsisinstalledonyourC:drive).Strangely,Accessalso

storestheinformationinthedatabase(MDB)file,soitdoesn't



readthisDSNfileagainafteritcreatesit.Ifyousetupan

AccessdatabasewithMySQLlinksandthentakethedatabase

toanothermachine,allyouneedistheMySQLODBCdriver

installed.Youdon'tneedtobringalongtheDSNfile,too.

AccessstorestheMySQLconnectioninformationasa

connectionstringthatlookslikethis(thelinebreaksaftereach

semicolonareincludedforreadabilityonly):

ODBC;

DRIVER={MySQLODBC3.51Driver};

DESC=;

DATABASE=financial;

SERVER=data.gurus.com;

UID=odbc-margy;

PASSWORD=ziasti;

PORT=;

OPTION=;

STMT=;

TABLE=Categories



Toseetheconnectionstringforalinkedtable,selectthetable

intheDatabasewindow,clickDesign,clickYeswhenAccess

pointsoutthatthetablestructurewillberead-only,right-click

anywhereintheDesignwindow,andchoosePropertiesfromthe

menuthatappears.Asyoucansee,boththeusernameandthe

password(ifyouhavechosentosavethepassword)appearin

plaintextsomuchforsecurity.Youcan'tedittheconnection

stringbecausethetablestructurecan'tbeedited.

IfyouopenaDSNfilewithNotepadoranothertexteditor,you

seethesameconnectionstring,butwithoutthesemicolons.

YoucanedittheDSNfile,butitwon'taffectexistinglinked

tables;itaffectsonlytablesthatyoulinkusingtheDSNfilein

thefuture.



7.11.4.SeeAlso

MySQLdocumentationathttp://dev.mysql.com/doc/

MySQLConnector/ODBCdriverdocumentationat

http://dev.mysql.com/doc/mysql/en/ODBC_Connector.html

PHPdocumentationathttp://www.php.net/docs.php

MargaretLevineYoung



Hack69.SendAccessDataThroughOutlook

Automatically



Implementbulkemailingofyourdatabytappinginto

Outlookobjects.

Thepurposeofmostdatabasesistostoreandreport

information.Often,itisnecessarytosendthereportsthatare

generatedbyadatabasetomultipleusers.Thisdoesn'thaveto

beamanualprocess.ByautomatingMicrosoftOutlookfrom

AccessVBA,itispossibletoautomaticallygeneratereportsand

sendthemviaemail.

Thefirstitemyouneedtodetermineiswhetheryouaregoing

tosendemailsonlythroughyouraddressbook.Ifyoudecideto

dothat,youdon'tneedtoadjustanyofthedefaultsettingsin

Outlook.If,however,youwanttosendtoanyaddressthrough

yourapplication,youneedtomakeachangeinOutlook.

Bydefault,Outlookautomaticallycheckstheemailaddresses

whenyousendanemail.Whenyouaredoingthisinan

automatedfashion,youwillhaveerrorstodealwithifanemail

addressdoesn'texistinyouraddressbook.Toshutoffthis

featureinOutlook,gototheTools Optionsdialog.

OntheOptionsdialog,showninFigure7-46,clicktheE-mail

OptionsbuttoninthePreferencestab,andthenclickthe

AdvancedE-mailOptionsbuttonshowninFigure7-47.

Thisactionbringsupadialogboxwiththreesections:"Save

messages,""WhennewitemsarriveinmyInbox,"and"When

sendingamessage,"asshowninFigure7-48.



The"Whensendingamessage"sectioncontainsacheckboxfor

"Automaticnamechecking,"asshowninFigure7-48.Checkthe

boxifyouwantOutlooktocheckaddresses,anduncheckitif

youwanttosimplysendthemessageswithoutchecking.

NowthatyouhavedeterminedhowyouwantOutlooktohandle

addresses,youarereadytobuildemailfunctionalityintoyour

application.Althoughyouwilleventuallywanttohavereports

basedonparameterizedqueriesthatgotodifferentusers,this

exampleshowshowtosendindividualreportstomultiple

recipients.



Figure7-46.Outlook'sOptionsdialog



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 68. Use Access as a Front End to MySQL

Tải bản đầy đủ ngay(0 tr)

×