Tải bản đầy đủ - 0 (trang)
Hack 81. Inform Users of a Long Process

Hack 81. Inform Users of a Long Process

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

Themessageinthestatusbarsays,"Processing2741of8500."

Thinkinglikeaprogrammer,youprobablyrealizethattohave

thetotalnumberofrecordsbeingprocessedinthemessage

meansarecordcountpropertyisbeingused.



8.11.1.TheCode

Hereisthecodebehindthebutton:



PrivateSubcmdProcessSales_Click()

DimconnAsADODB.Connection

Setconn=CurrentProject.Connection

DimrecsetAsNewADODB.Recordset

Dimtotal_recordsAsLong

Dimrecord_numAsLong

record_num=0

recset.Open"Select*FromSalesRecords",conn,adOpenKeyset,

adLockOptimistic

total_records=recset.RecordCount

DoUntilrecset.EOF

recset.Fields("NetSales")=recset.Fields("Sales")-recset

Fields("Costs")

record_num=record_num+1

feedback_msg="Processing"&record_num&"of"&total_

SysCmdacSysCmdSetStatus,feedback_msg

recset.MoveNext



Loop

recset.Close

Setrecset=Nothing

SysCmdacSysCmdClearStatus

EndSub



Thecodecreatesarecordsetandloopsthroughit.Priortothe

looping,theRecordCountpropertypopulatesthetotal_records

variable.Duringthelooping,anothervariable,record_num,is

incremented.Thesetwovariablesareusedtocreatethe

message:





feedback_msg="Processing"&record_num&"of"&to



ThenthemessageisusedintheSysCmdmethod:

SysCmdacSysCmdSetStatus,feedback_msg



Finally,attheendoftheprocessing,thecodeclearsthestatus

barbygivingSysCmdaclearstatusflag:

SysCmdacSysCmdClearStatus



8.11.2.HackingtheHack

Providingafeedbackmessageofthetypedescribedhereis

helpfulingaugingthelengthofaprocess.Ifthefeedback

consistedofjustthenumberofrecordsprocessedwithout



indicatingthetotalnumbertobeprocessed,youstillwouldnot

knowhowlongtheprocesswilltaketocomplete.Forexample,

asimplemessageof"Processing2471"doesn'tletyouknowif

youarehalfwaydone,arenearlydone,orhavehardlyeven

begun.

Ofcourse,themessageformatof"ProcessingXofXX"works

onlyinaloop.Otherlongprocessesmightnotbebasedona

loop.Acomplexquerycantaketime,especiallywhenitneeds

toworkonmanyrecords.Itisn'tpossibletobreakintoaquery

inthesameway,sothethingtodoistoputthetimethatthe

processstartedinthestatusbar.

TheNowfunctionreturnsthetimefromthesystemclock.By

displayingthatinthestatusbar,you'reatleasttellingusers

whentheprocessstartedsothattheycancomparethestart

timetotheclocktimeinthesystemtrayattherightofthe

Windowstaskbar.



Hack82.AllowUserstoChooseaBack-End

Database



StoreODBCconnectionstringsinatablesotheyare

readytogowhenneeded.

Certainsystemapplicationsprovidemorethanonedatabase

youcaninteractwith.Aslongasthestructureofthevarious

databasesand/ortheirtablesisbasedonacommonschema,it

ispossibletoswapthebackendsinandout.Aneasywaytodo

thisistoprovidetheavailableconnectionsinalistinwhich

usersselectthedatabasetouse.

Figure8-17showsatablethatsimplycontainsODBC

connectionstrings.

Ontheforminwhichusersselectaconnection,theyjustsee

thefriendlynames,providedinthetable'ssecondcolumn(the

firstcolumnservesasawaytosortthelist).Figure8-18shows

theformandthecomboboxfromwhichaconnectionis

selected.



Figure8-17.AtablefilledwithODBCconnection

strings



Figure8-18.Selectingadatabaseconnection



Thevalueofthecomboboxisusedincodetosetthe

ConnectionpropertyforanADOconnectionobject,likethis:

DimconnAsADODB.Connection

Setconn=NewADODB.Connection

conn.ConnectionString=Me.cmbConnections

conn.Open



Inthismanner,userscaneasilychangethedatabasetheyare



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

Hack 81. Inform Users of a Long Process

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

×