Вы находитесь на странице: 1из 21

9/28/2010

Tutorial: Creating A Multi-User Network…

Tutorial: Creating A FoxPro Multi-User Application

Tutorial: Creating A FoxPro Multi-User Application Dr. Thomas E. Hicks Computer Science Department Trinity

Dr. Thomas E. Hicks Computer Science Department Trinity University

Background Information & Other Tutorials

For purposes of this tutorial, we shall start with the single user application created in the following tutorial.

Tutorial : FoxPro Create Application

1] Our RestaruantApp still resides in folder C:\Temp\RestaurantApp. (See Below!)

resides in folder C:\Temp\RestaurantApp . (See Below!) 2] The Project may be seen below:

2] The Project may be seen below:

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… Open The Database & Tables Exclusively 1] By default the

Open The Database & Tables Exclusively

1] By default the Use command opens the database in exclusive mode. A second user may not open either the database or the table. Note the exclusive on the status bar. See Below!

Open Database Rest Use Data\Rest Order Name

status bar. See Below! Open Database Rest Use Data\Rest Order Name …trinity.edu/…/FoxPro-MultiUserApplicat… 2/21

9/28/2010

Tutorial: Creating A Multi-User Network…

Open The Database & Tables Shared

1] Let us now attempt to open database Rest and table Rest simultaneously on two foxpro appplications. These applications my be on two different computers; they may also be on the same computer as illustrated below.

Open Database Rest Shared Use Data\Rest Shared Order Name

Open Database Rest Shared Use Data\Rest Shared Order Name 2] No longer does it say exclusive

2] No longer does it say exclusive at the bottom of the status bar. The active record on both applications is record 1 of 54. Any function or form that enables the user to alter a record automatically lock the record as soon as a change is initiated. Edit and Change are two such functions. In the screen capture below, The top user is going to and a 2 is added to the end of the restaurant name; you can see Record Locked in the status bar as soon as the change is initiated. Once a user locks the record while making a change, it shall stay locked until the user moves to the next record.(See Below!)

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… 3] Fortunately, locking the record does not prevent the other

3] Fortunately, locking the record does not prevent the other users from searching,running reports, running labels, examining data, or selection type queries. As you can see below, the bottom user can attempt to edit the same record and shall be successful until he/she attempts to make a change. (See Below!)

until he/she attempts to make a change. (See Below!) 4] When the bottom user attempts to

4] When the bottom user attempts to add a 2 to the end of the city Odessa, he/she gets a message Attempting To

Lock

lock and be able to make his/her change. Really "klugy" at this stage. (See Below!)

Press Esc To Cancel. As soon as the top exits or moves on to another record, the bottom user will get a

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… 5] In the screen capture below, the top user finished

5] In the screen capture below, the top user finished editing record 1 and closed the edit window; the bottom user received a lock on record 1 and is able to make his/her change to Odessa. (See Below!)

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… 6] Forms only compound these problems! Two Form Modes -

6] Forms only compound these problems!

Two Form Modes - Edit Mode & Normal Mode

1] There are two basic modes.

The E d i t M o d e shall lock the current database record and Edit Mode shall lock the current database record and permit changes to that record.

Options within this mode shall be only Cancel & Savecurrent database record and permit changes to that record. The N o r m a l

The N o r m a l M o d e shall enable the user to Normal Mode shall enable the user to

Options within this mode shall be all else ==> Next, Previous, Top, Bottom, Search, Print, Browse, Edit, Add, Delete, Exit, etc.N o r m a l M o d e shall enable the user to 2]

2] For educational purposes, let us add two temporary buttons to our Rest form. (See Below!)

The valid event for the Edit Mode button should be Do SetEditMode Do SetEditMode

The valid event for the Normal Mode button should be Do SetNormalMode Do SetNormalMode

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… Normal Mode Should Allow No Option To Change The Record

Normal Mode Should Allow No Option To Change The Record Fields - Scatter Memvar Memo

1] It is customary to work with a copy of the data in the edit mode; this greatly facilitates the ease of canceling the process with no damage to the original data. Some databases, such as FoxPro, have the option to easily create a set of local variables which contain a copy of the information in the current record; this function is called Scatter in the x-base languages.

In other database applications, such as Access, the programmer has to manually create and assign the needed local variables.

2] As you already know, Display Memory, will display all of the local variables -- followed by all of the system variables, etc. In the screen capture below, you can see that local variable NewName was assigned the value Dr.Thomas E. Hicks; Display memory shows this immediately before all of the system variable. (See Below!)

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… 3] The current record is record 1 of 54. The

3] The current record is record 1 of 54. The Customer record has 9 fields; the notes field is a memo field. Scatter Memvar Memo creates 9 new local variables whose names are the same as those in the current record and whose values match those of the current record. Scatter Memvar makes a copy of all fields except memo fields. (See Below!)

makes a copy of all fields except memo fields. (See Below!) 3] The database field Name

3] The database field Name takes precedence over the local memory variable Name; therefore, ? Name will display the contents of the field variable Name and ? M.Name will display the contents of the local memory variable Name.

In the screen capture below, both Name and M.Name contain Abstruse Steak House2 because of the scatter. Name = "Joes" will change local memory variable M.Name to "Joes". Replace Name With "Mikes" will change the field Name of the current record to "Mikes". See Below!

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… Change Control Source Of All 10 Fields To Be Local

Change Control Source Of All 10 Fields To Be Local Variable

1] A good application must enable the user to enter the edit mode, make some changes, and then discard those changes ==> thus returning to the original state. Change the control source of each of the variables on the Rest form. This means changing the control source of the Name1 field from Rest.Name to M.Name. Do likewise for all 9 fields; don't forget the Credit Card and Reservations!

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… 2] Program Main must Scatter Memvar Memo prior to launching

2] Program Main must Scatter Memvar Memo prior to launching the form! The first part of main might be:

Deactivate Windows All Set Default To CurDir( ) Set Near On Set Bell Off Set Deleted On Set Procedure To Programs\Code Open Database Rest Shared Use Data\Rest Shared Order Name Scatter Memvar Memo

Scatter Each & Every Time You Move The Read/Write Record Pointer

9/28/2010

Tutorial: Creating A Multi-User Network…

scatter should occur after the pointer is moved and before the form is refreshed.

3] Functions such as GoTo, Seek, and Skip are among the items that move the record pointer. I leave it to you to decide exactly which functions need to be changed. The scatter is included in our new and improved Next procedure below. Take time now to scatter where-ever you need in the Code.prg.

Procedure Next If EOF() Then GoTo Top

EndIf Skip 1 If EOF() Then GoTo Top

EndIf Scatter Memvar Memo _screen.activeform.refresh

Return

EndProc

4] Run the main program and make sure that your Add, Delete, Next, Previous, Top, Bottom, Delete, and Browse functions still work properly!

Normal Mode - Must Make All Fields Read Only

1] In the Normal mode, it is essential that each of the data fields must be read-only. We do not want the user moving the mouse into the data fields and making changes unless we are in the Edit mode. The following code will show you how to make the Name field read-only.

Procedure NormalMode

*--------------------------------------------------------------------

--

*--------------------------------------------------------------------

*--

All Fields Must Be Read Only

_screen.activeform.Name1.ReadOnly = .T. _screen.activeform.Address1.ReadOnly = .T.

Return

EndProc

2] Do likewise for the other 8 fields in the Restaurant form. Procedure NormalMode should have 9 such assignments.

Edit Mode - Must Make All Fields Not Read Only

1] In the Edit mode, it is essential that each of the data fields must not be read-only; it is there that we shall permit the user to edit the data. It is there that we allow the user to move the mouse into the data fields and making changes. The following code will show you how to make the Name copy of the data accessible/not read-only.

Procedure EditMode

*--------------------------------------------------------------------

--

*--------------------------------------------------------------------

*--

No Fields Should Be Read Only

_screen.activeform.Name1.ReadOnly = .F. _screen.activeform.Address1.ReadOnly = .F.

9/28/2010

Tutorial: Creating A Multi-User Network…

Return

EndProc

2] Do likewise for the other 8 fields in the Restaurant form. Procedure EditMode should have 9 such assignments.

Testing Normal Mode and Edit Mode # 1

1] Run the form from your main program. Using the mouse, select/push the Normal Mode button. (See Below!)

the mouse, select/push the Normal Mode button. (See Below!) 2] All of the fields should be

2] All of the fields should be read only. Move the cursor into the City field. Try to make a change. If you are able to add something to the end of Kailua, you do not have the field read-only. Try all nine of the data fields. (See Below!)

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… 3] Using the mouse, select/push the Edit Mode button. (See

3] Using the mouse, select/push the Edit Mode button. (See Below!)

the mouse, select/push the Edit Mode button. (See Below!) 4] All of the fields should not

4] All of the fields should not be read only. Move the cursor into the City field. Try to make a change. If you are unable to make changes something is wrong. I have added nto to the end of Kailua below. Try all nine of the data fields to

9/28/2010

Tutorial: Creating A Multi-User Network…

make sure that you can change each. (See Below!)

Network… make sure that you can change each. (See Below!) 5] At the moment, the form

5] At the moment, the form is changing a memory copy of the data record. If you hit the Next and Previous buttons, you will find that the original record has not yet been changed; we will get to that in due time.

Buttons Available In Edit Mode

1] Once a user enters the Edit Mode, the record will soon be locked; others will not have access to this record. In order to reduce the complexity and encourage the user to quickly exit the Edit Mode, thus freeing the record for others. The Edit Mode shall disable all buttons except the Save and Cancel buttons.

2] The buttons on our form are graphic; we have a disabled picture image and a regular picture image associated with each. You need not associate images with regular buttons.

with each. You need not associate images with regular buttons. …trinity.edu/…/FoxPro-MultiUserApplicat… 14/21

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… 3] The Edit Mode shall Enable the Save and Cancel

3] The Edit Mode shall Enable the Save and Cancel Buttons; it shall disable all other buttons. Do not disable the the Normal Mode and Edit Mode buttons; these are here for learning purposes and shall soon be deleted.

Enabled ==>

learning purposes and shall soon be deleted. Enabled ==> Disabled ==> Procedure EditMode . . .

Disabled ==>

and shall soon be deleted. Enabled ==> Disabled ==> Procedure EditMode . . .

Procedure EditMode

.

.

.

*--------------------------------------------------------------------

--

*--------------------------------------------------------------------

*--

Enable Save & Cancel Buttons

_screen.activeform.CancelButton.Enabled = .T. _screen.activeform.SaveButton.Enabled = .T.

*--------------------------------------------------------------------

--

*--------------------------------------------------------------------

*--

Disable All Other Buttons

_screen.activeform.TopButton1.Enabled = .F. _screen.activeform.TopButton2.Enabled = .F.

return

EndProc

4] Don't test yet!

Buttons Available In Normal Mode

1] Within the Normal Mode, the user shall have access to all buttons except the Save and Cancel.

Procedure NormalMode

.

.

.

*--------------------------------------------------------------------

--

*--------------------------------------------------------------------

*--

Disable Save & Cancel Buttons

_screen.activeform.CancelButton.Enabled = .F. _screen.activeform.SaveButton.Enabled = .F.

9/28/2010

Tutorial: Creating A Multi-User Network…

*--------------------------------------------------------------------

--

*--------------------------------------------------------------------

*--

Enable All Other Buttons

_screen.activeform.TopButton1.Enabled = .T. _screen.activeform.TopButton2.Enabled = .T.

return

EndProc

4] Don't test yet!

Testing Normal Mode and Edit Mode # 2

1] Run the form from your main program. Using the mouse, select/push the Edit Mode button Note that all of the permanent form buttons, except Save & Cancel are disabled. (See Below!)

buttons, except Save & Cancel are disabled. (See Below!) 2] Using the mouse, select/push the Normal

2] Using the mouse, select/push the Normal Mode button Note that only the permanent form buttons Save & Cancel are disabled. (See Below!)

9/28/2010

Tutorial: Creating A Multi-User Network…

9/28/2010 Tutorial: Creating A Multi-User Network… 3] The form should be initiated in the normal mode.

3] The form should be initiated in the normal mode. Once the form is launched, the main program should evoke the NormalMode procedure. (See Below!)

** Declare Public variables in which to hold the Main form

Public

RestForm

** Load form Rest and associate it with

Do Form Forms/Rest Name Do NormalMode

RestForm

linked

** Remove the FoxPro window RestForm.application.visible

= .F.

RestForm

- For Show & Hide

* The Line Below Requied For Compiled Applications Read Events

Add Procedure

1] The Add Procedure shall

1. Scatter a temporary blank copy of the variables

2. Enter the Edit Mode

3. Refresh the form

4. Move cursor to first field

5. Allow the user to complete the information for the new record

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

*-

Procedure Add

-*

 

-*

*- *- Purpose : Load a blank form and allow the user to Save or

-*

9/28/2010

Tutorial: Creating A Multi-User Network…

*-

Cancel. Save will append a new record with the

-*

*-

appropriate data.

-*

 

-*

*- Called From : Valid Event of Add Button. *-

*- Written By: Dr. Thomas E. Hicks

-* App: Visual FoxPro 6 -*

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Procedure Add

AddMode

= .T.

Scatter Memvar Memo Blank Do EditMode _screen.activeform.refresh

_screen.activeform.Name1.SetFocus

Return

EndProc

2] Remember to add global variable

Public

AddMode

AddMode = .F.

AddMode

to your main program; initialize it to false.

3] The form should look like the following once the Add button is selected/pushed.

like the following once the Add button is selected/pushed. 3] We realize that a record has

3] We realize that a record has not been added to the database yet. If the user cancels this new insertion, there is no need to add a record that we would have to delete.

Cancel The Add

9/28/2010

Tutorial: Creating A Multi-User Network…

do so.

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

*-

Procedure Cancel

-*

 

-*

*-

-*

*- Purpose : Return to the Normal Mode and refresh the form.

-*

*- Called From : Cancel Button for both Add and Edit *-

*- *- Written By: Dr. Thomas E. Hicks

functionality.

-* -* App: Visual FoxPro 6 -*

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Procedure Cancel Scatter Memvar Memo Do NormalMode

AddMode

= .F.

_screen.activeform.refresh

Return

EndProc

Save - Accept The Add

1] Gather Memvar Memo is the reciprocal operation of Scatter Memvar Memo.Gather takes all 9 of the local memory variables and reloads them into the current record.

2] The Save procedure shall

1. Add a new record if in the Add Mode

2. Gather the local information into the current record

3. Return To The Normal Mode

4. Refresh the form

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

*-

Procedure Save

-*

*-

-*

*- Purpose : If the mode is Add - Append a new blank record.

-*

*-

Use Gather command to load local memory variables

-*

*-

into the database record. Return to the Normal Mode

-*

*-

and refresh the form.

-*

*- Called From : Save Button for both Add and Edit Functionality. -* *-

*- Written By: Dr. Thomas E. Hicks

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Procedure Save

-* App: Visual FoxPro 6 -*

If (

AddMode

= .T.)

Append Blank

AddMode

= .F.

EndIf Gather Memvar Memo

AddMode

= .F.

Do NormalMode _screen.activeform.refresh

Return

EndProc

3] Check out the Add and make sure that both the cancel and save functionality work properly!

9/28/2010

Tutorial: Creating A Multi-User Network…

Procedure Edit - Must Lock The Record

1] The Edit procedure shall

1. Normally, lock will put display a message - attempting to lock in the status bar. This is not so obvious to the user. We shall Set Reprocess to 0 Seconds to prevent this loop.

2. Attempt to lock the record. LockStatus will be true if successful; otherwise false.

3. If unsuccessful, tell the user.

4. If successful, enter the EditMode and enable the user to make changes to the copy of the record.

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

*-

Procedure Edit

-*

*- Purpose : Attempt to lock the record. If the record is locked,

-*

*-

inform the user. If the recod is not locked, lock it

-*

*-

and enter the Edit Mode.

-*

 

-*

*- Called From : Edit Button. *-

*- Written By: Dr. Thomas E. Hicks

-* App: Visual FoxPro 6 -*

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Procedure Edit

Set Reprocess To 0 Seconds LockStatus = Lock() If (LockStatus = .F.) Then Wait Window "This Record Is Locked By Another User" TimeOut 5

Else

 

Do EditMode

_screen.activeform.Name1.SetFocus

EditMode = .T.

EndIf

Return

EndProc

2] The Cancel procedure will need to be altered to terminate the Edit functionality. (See Below!)

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

*-

Procedure Cancel

-*

*-

-*

*- Purpose : Return to the Normal Mode and refresh the form.

-*

-*

*- Called From : Cancel Button for both Add and Edit *-

*- *- Written By: Dr. Thomas E. Hicks

functionality.

-* -* App: Visual FoxPro 6 -*

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Procedure Cancel Scatter Memvar Memo Do NormalMode

AddMode

= .F.

EditMode

= .F.

_screen.activeform.refresh

Return

EndProc

9/28/2010

Tutorial: Creating A Multi-User Network…

3] The Save procedure will also need to be altered to terminate the Edit functionality. (See Below!)

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

*-

Procedure Save

-*

 

-*

*- *- Purpose : If the mode is Add - Append a new blank record.

-*

*-

Use Gather command to load local memory variables

-*

*-

into the database record. Return to the Normal Mode

-*

*-

and refresh the form.

-*

*- Called From : Save Button for both Add and Edit Functionality. -* -*

App: Visual FoxPro 6 -*

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Procedure Save If (

*- *- Written By: Dr. Thomas E. Hicks

AddMode

= .T.)

Append Blank

AddMode

= .F.

EndIf Gather Memvar Memo Scatter Memvar Memo Do NormalMode

EditMode

= .F.

_screen.activeform.refresh

Return

EndProc

May be accessed through URL: http://www.cs.trinity.edu/~thicks

May also be accessed through URL: http://carme.cs.trinity.edu This Document May Not Be Printed or Reproduced Without Written Permission. 2003 Copyright : Dr. Thomas E. Hicks Permission granted : Professional Educators & College Students may print one copy of this page!

& College Students may print one copy of this page! Dr. Thomas E. Hicks Computer Science

Dr. Thomas E. Hicks

Computer Science Department Trinity University

"Dr. Web"

Dr. Web's Site
Dr. Web's Site
Send Mail
Send Mail
Computer Science
Computer Science
Trinity University
Trinity University