Tải bản đầy đủ - 0 (trang)
Hack 67. Manage Word Documents from Access

Hack 67. Manage Word Documents from Access

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

7.10.2.TheCode

Thenextthingtodoisenterthecode.Thismustgointoan

Accesscodemodule:



SubAccess_to_Word()

DimconnAsADODB.Connection

Setconn=CurrentProject.Connection

DimrecsetAsADODB.Recordset

Setrecset=NewADODB.Recordset

Dimrow_numAsInteger

Dimcol_numAsInteger

Dimword_docAsObject

'AssumesWorddocisinsamepath-changenameandpathasnee



Setword_doc=GetObject(Application.CurrentProject.Path&"\Cu

'getdatafromAccesstable

recset.Open"Select*FromCustomersWhereState='OR'",_

conn,adOpenKeyset,adLockOptimistic

'gettherecordcount-usedtocreateWordTable

recset.MoveLast

recset.MoveFirst



Withword_doc

'navigatetoWordbookmarkandcreatetable

'thenumberoftablerowsmatchestherecordsetrowcount

'thenumberoftablecolumnsmatchesthenumberofrecordsetfi

.Bookmarks("Customers").Select

.Tables.AddRange:=Selection.Range,_

NumRows:=recset.RecordCount,NumColumns:=recset.Fields

Forrow_num=1Torecset.RecordCount

Forcol_num=1Torecset.Fields.Count

.Tables(.Tables.Count).Cell(row_num,col_num).

SelectSelection.TypeTextrecset.Fields(col_num-1)

Nextcol_num

'nextdatabaserecord

recset.MoveNext

Nextrow_num

EndWith

recset.Close

Setrecset=Nothing

Setword_doc=Nothing

MsgBox"done"

EndSub



Herearesomehighlightsofthiscode:

TheAccessdataisgatheredintoarecordset.



TheGetObjectfunctionisreferencedtotheexistingWord

document.Notethatthisexampleassumesthedatabase

andthedocumentareinthesamedirectory.Also,thename

ofthedocumentishardcoded,butyoucanchangethisas

necessary.

Thedocumenthasapreestablishedbookmarknamed

Customers.Thisisusedasaguidetowheretocreatethe

table.

AWordtableiscreated,anditsrowandcolumndimensions

matchthoseoftherecordset.ThisensuresthenewWord

tableisexactlythecorrectsizetohousethedata.

TheWordtableispopulatedcellbycellbyloopingthrough

therecordset.Anouterloopcyclesthroughtherecordset

rows,andineachrowaninnerloopcyclesthrougheach

field.



7.10.3.TheDataHasLandedIntact

Afterrunningthiscode,thedocumenthasatablewiththedata,

asshowninFigure7-43.Notethatthereisnoconnectionback

toAccess;thedataisjustessentiallypartoftheWord

document.

Notethatthissimplisticexampleassumesanumberofthings:

thebookmarkexists,thereisnoexistingtable,andtheAccess

tableisn'ttoolargeintermsofrowsandfieldstomakethe

Wordtabletoodenselypacked.

Nonetheless,thishackservesasabriefintroductiontotapping

intoWordobjects.Becausethereferencehasbeensettothe

library,youcannowusetheObjectBrowserinAccesstoreview

Word'sobjects.



Figure7-43.TheAccessdatainaWordtable



Hack68.UseAccessasaFrontEndtoMySQL



MySQLisawidelyusedopensourcedatabaseprogram

thatoftenrunsonLinuxwebservers,andAccessmakes

agreatfrontendfordataentryandreporting.

MySQLisawildlysuccessfulopensourceSQLdatabasethat

runsonmostLinux-basedwebservers.It'stheperfect

databasetostoreinformationforuseindatabase-drivenweb

sitesbecauseyoucanusethePHPprogramminglanguageto

readthedatafromMySQLanddisplayitonwebpages.Infact,

thecombinationofLinux,Apache(anopensourcewebserver

thatrunsonLinux),MySQL,andPHPissopopularthatit's

knownbyitsinitials:LAMP.

However,MySQLdoesn'tholdacandletoAccesswhenitcomes

toformsandreports.MySQLhasnobuilt-informeditoror

reportwriter.Instead,youtypecommandsatacommandline

orwriteprograms(usuallyaspartofPHP-basedwebpages)to

enter,edit,anddisplayinformation.

Butwhowantstocreateprogramsthatdisplaydataentryforms

foreachtable?ThisiswhereAccesscomesin.Accesscanadda

friendlyfacetoyourMySQLdatabase.Inthesamewayan

AccessdatabasecanlinktotablesinanotherAccessdatabase,

youcanlinktotablesinaMySQLdatabaseonyourwebserver

overaLANortheInternet.Oncethetablesarelinked,you(or

yourusers)canuseAccessformstoenteroredittheMySQL

dataandAccessreportstodisplayit.



7.11.1.InstallingtheMySQLTools



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



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

Hack 67. Manage Word Documents from Access

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

×
x