Tải bản đầy đủ - 0 (trang)
Hack 95. Use Access as an XML Database

Hack 95. Use Access as an XML Database

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

Thedataiscompletelyexternal,butitdoesn'tcomefroma

table.Thisapplicationcontainsnotables,whetherlinkedor

connectedwithADOorODBC.Infact,thisapplicationcontains

nothingexceptthisoneform.



9.5.1.TheCode

Thefollowingcodebehindtheformtakescareofalldata

management:

OptionCompareDatabase

PublicxmlobjAsDOMDocument

Publicxml_listAsIXMLDOMNodeList

Publicrecord_numAsInteger

Publicfile_nameAsString



PrivateSubcmdDelete_Click()

Dimxml_nodeAsIXMLDOMElement

Setxml_node=xmlobj.documentElement.childNodes(record_num



xmlobj.documentElement.removeChildxml_node

xmlobj.Savefile_name

reload_file

EndSub

PrivateSubcmdNew_Click()

Me.txtEmployeeID=""

Me.txtEmployeeName=""

Me.txtHireDate=""

Me.txtRecordNum=""

EndSub

PrivateSubcmdNext_Click()

Ifrecord_num
record_num=record_num+1

Else



record_num=0

EndIf

load_record

EndSub

PrivateSubcmdPrevious_Click()

Ifrecord_num>0Then

record_num=record_num-1

Else

record_num=xml_list.length-1

EndIf

load_record

EndSub

PrivateSubcmdSave_Click()

Dimxml_nodeAsIXMLDOMElement

IfMe.txtEmployeeID=""OrMe.txtEmployeeName=""Or_

Me.txtHireDate=""Then

MsgBox"Mustfillinallthreefields"

ExitSub

EndIf



Setxml_node=xmlobj.createElement("Employee")

xml_node.setAttribute"EmployeeID",Me.txtEmployeeID

xml_node.setAttribute"EmployeeName",Me.txtEmployeeName

xml_node.setAttribute"HireDate",Me.txtHireDate

xmlobj.documentElement.appendChildxml_node

xmlobj.Savefile_name

reload_file

EndSub

PrivateSubcmdUpdate_Click()

xmlobj.documentElement.childNodes(record_num)_

.Attributes(0).nodeValue=Me.txtEmployeeID

xmlobj.documentElement.childNodes(record_num)_

.Attributes(1).nodeValue=Me.txtEmployeeName

xmlobj.documentElement.childNodes(record_num)_

.Attributes(2).nodeValue=Me.txtHireDate

xmlobj.Savefile_name

reload_file

EndSub

PrivateSubForm_Open(CancelAsInteger)

file_name="C:\EmployeeData.xml"

Setxmlobj=NewDOMDocument

xmlobj.async=False

xmlobj.Loadfile_name

Setxml_list=xmlobj.selectNodes_

("Employees/Employee")

'loadfirstrecord

record_num=0

load_record

EndSub

Subload_record()

Me.txtEmployeeID=_

xml_list.Item(record_num).Attributes(0).nodeValue



Me.txtEmployeeName=_

xml_list.Item(record_num).Attributes(1).nodeValue

Me.txtHireDate=_

xml_list.Item(record_num).Attributes(2).nodeValue

Me.txtRecordNum=record_num+1

EndSub

Subreload_file()

xmlobj.Loadfile_name

Setxml_list=xmlobj.selectNodes_

("Employees/Employee")

'loadfirstrecord

record_num=0

load_record

EndSub



9.5.2.LoadingtheXMLFile

Whentheformopens,apublicXMLvariable(xmlobj)issetto

theloadedXMLfile,whichresidesinmemory.Alistofnodes

(xml_list)holdstheEmployeerecords,andthefirstrecordis

displayedintheform:

PrivateSubForm_Open(CancelAsInteger)

file_name="C:\EmployeeData.xml"

Setxmlobj=NewDOMDocument

xmlobj.async=False

xmlobj.Loadfile_name

Setxml_list=xmlobj.selectNodes_

("Employees/Employee")

'loadfirstrecord

record_num=0



load_record

EndSub



9.5.3.BrowsingRecords

InXMLlingo,thelengthpropertyisthesameasthecount

propertyinVB.WhentheNextorPreviousbuttonsareclicked,

apublicvariable,record_num,iscomparedwiththenumberof

XMLrecords.Iftherecord_numvariablehitsthetotalcountasa

resultofclickingNext,itresetsto0.Iftherecord_numvariable

hits0asaresultofclickingPrevious,itresetstothenumberof

records.ClickingNextorPreviouscompleteswithacalltothe

load_recordroutine:

PrivateSubcmdNext_Click()

Ifrecord_num
record_num=record_num+1

Else

record_num=0

EndIf

load_record

EndSub

PrivateSubcmdPrevious_Click()

Ifrecord_num>0Then

record_num=record_num-1

Else

record_num=xml_list.length-1

EndIf

load_record

EndSub



Theload_recordroutinesimplyfillsthecontrolsontheformwith

thedatafromtheXMLrecordthatispositionedattherecord_num

number:

Subload_record()

Me.txtEmployeeID=_

xml_list.Item(record_num).Attributes(0).nodeValue

Me.txtEmployeeName=_

xml_list.Item(record_num).Attributes(1).nodeValue

Me.txtHireDate=_

xml_list.Item(record_num).Attributes(2).nodeValue

Me.txtRecordNum=record_num+1

EndSub



9.5.4.UpdatingaRecord

Whendataischangedwhileontheform,theUpdatebutton

mustbeclickedtosavethechangesbacktotheoriginalfile.

Theprocesshereistoupdatethenode(theemployeerecord)

inthefilewiththeformvalues.TheEmployeenodeisachildof

documentElementEmployees.Thevaluesaren'tsaveduntiltheSave

methodrunsonxmlobj.Afterthat,thefileisreloaded,andthis

laststepresetstheformbacktothefirstrecord(analternative

istoleavetheformdisplayingtheupdatedrecord):

PrivateSubcmdUpdate_Click()

xmlobj.documentElement.childNodes(record_num)_

.Attributes(0).nodeValue=Me.txtEmployeeID

xmlobj.documentElement.childNodes(record_num)_

.Attributes(1).nodeValue=Me.txtEmployeeName

xmlobj.documentElement.childNodes(record_num)_

.Attributes(2).nodeValue=Me.txtHireDate

xmlobj.Savefile_name

reload_file



EndSub



9.5.5.DeletingaRecord

Todeletearecordsetanodevariable(xml_node)totheemployee

record.Then,theremoveChildmethodofitsparentdeletesit:



PrivateSubcmdDelete_Click()

Dimxml_nodeAsIXMLDOMElement

Setxml_node=xmlobj.documentElement.childNodes(record_num

xmlobj.documentElement.removeChildxml_node

xmlobj.Savefile_name

reload_file

EndSub



Aswithotherfilechanges,theSavemethodisnecessary.



9.5.6.AddingaNewRecord

TheNewandSavebuttonsworktogethertoaddarecordtothe

XMLfile.TheNewbuttonsimplyclearstheform,andnew

employeeinformationcanbeentered.TheSavebuttonrunsthe

codethatsavesanewrecord.

Aftervalidatingthatalltextboxescontaindata,anewelement

iscreated.Attributesaresettotheformvalues,andthe

element,alongwithitsattributes,aresavedusingthe

appendChildmethod.TheSavemethodfollows,andthefileis

reloaded(nowitcontainsthenewrecord):



PrivateSubcmdSave_Click()

Dimxml_nodeAsIXMLDOMElement

IfMe.txtEmployeeID=""OrMe.txtEmployeeName=""Or_

Me.txtHireDate=""Then

MsgBox"Mustfillinallthreefields"

ExitSub

EndIf

Setxml_node=xmlobj.createElement("Employee")

xml_node.setAttribute"EmployeeID",Me.txtEmployeeID

xml_node.setAttribute"EmployeeName",Me.txtEmployeeName

xml_node.setAttribute"HireDate",Me.txtHireDate

xmlobj.documentElement.appendChildxml_node

xmlobj.Savefile_name

reload_file

EndSub



9.5.7.SeeAlso

"ImportVariedXMLDataintoAccess"[Hack#63]

"ExportXMLDataSanely"[Hack#64]

"BreakThroughVBA'sTransformationBarrier"[Hack#65]

"ProvideCompleteXMLControltoAnyVersionofAccess"

[Hack#87]



10.TheInternet

Section10.1.Hacks96100

Hack96.ExportaReportasHTML

Hack97.UseaBrowserInsideAccess

Hack98.PulltheHTMLSourceCodefromaWebSite

Hack99.DownloadFilesUsingtheWebBrowserControl

Hack100.UseaSmartTagtoOpenaWebPage



10.1.Hacks96100

WebtechnologiesandtheInternetareingrainedinsomuchof

whatwedothatextendingtheWebtoworkwithourdatabase

applicationsmakesperfectsense.AlthoughatfirstAccessmight

notseemlikeaweb-savvyproduct,withafewhacksAccess

canworkwellwiththeWeb.

ThischapterbeginsbyshowinghowtosaveareportasHTML

[Hack#96].Thisletsyoupostyourdatatoawebsite.Better

yet,howaboutbringingtheInternetintoyourdatabase?"Usea

BrowserInsideAccess"[Hack#97]showsyouhowtousea

webbrowserrightwithinyourAccessforms.Needtogetdata

fromawebsite?JustFTPitover[Hack#99].



Hack96.ExportaReportasHTML



Previewyourreportsonwebpagestoreachalarger

audience.

Previewingsummarizeddataisakeybusinessneed,andAccess

handlesitquitewell.Toreachalargeaudience,youcanprint,

email,savetoWord,andsoon.Anotheroptionistosavea

reporttoawebpage.

WhenyouopenanAccessreport,oneoftheselectionsonthe

FilemenuisExport.Ahandfulofoutputtypesareavailable,and

oneofthemisHTML.Figure10-1showsareportbeing

exportedasHTML.

SavingthereportasanHTMLfileopensupanumberof

possibilities.Thelikelyactionistoviewthereportinaweb

browser.Figure10-2showsthereportpreviewedinInternet

Explorer.TheHTMLfileisnolongerapartofthedatabase,so

youmustopenitinthebrowserusingtheFile Openmenu.

YoucanuploadtheHTMLfiletoawebserverandmakeit

availablepublicly,oryoucanuploadittoacontrolledintranet

group.OpeningthefileinNotepadoranotherplain-texteditor

revealstheactualHTMLcode,asshowninFigure10-3.



Figure10-1.SelectingtoexportareportasHTML



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

Hack 95. Use Access as an XML Database

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

×