Check for a valid datasource from within the correct datasession

Here’s an interesting one. You can use hotkey’s to type in certain strings for quick data entry. I do this for my Photo collection (see Sharing Digital Pictures of your friend's )

Run the code below. It creates a form with an editbox databound to a cursor.

Try typing some text into the editbox, then hit the hotkey ctrl-S, which should add the string “foobar” to whatever you typed.

Now close the form, uncomment the DataSession line and try again. This time, the “foobar” replaces whatever you typed.

Furthermore, uncomment the first Create Cursor line, and that fixes the problem.

When a hotkey is hit, the internal form service routine looks at the currently active control to see if it contains data that needs to be flushed. The routine that flushes the data checks to see if the datasource’s workarea has a cursor. If not, it doesn’t flush anything.

To find the problem, I put a breakpoint on the code that changes the text in an editbox (which is the same as the code that inserts text into a textbox or the VFP code editor). I could see that the editbox data was being replaced. I just looked up the call stack to see where the data was coming from. When debugging, the callstack is your best friend!

The problem was the check for the datasource work area was done in the wrong datasession. That’s why creating a cursor fixes the problem; there datasource does point to a valid cursor.

Moving the check to within the right datasession fixes the problem.


ON KEY LABEL CTRL+s keyboard "foobar"

*CREATE CURSOR xx (name c(10)) && uncomment this line to fix!




DEFINE CLASS myform1 AS form


      Left = 200

      AllowOutput = .F.

* datasession=2 && Uncomment this line to break

      ADD OBJECT edit1 AS editbox WITH ;

            Left = 60, ;

            Top = 0, ;

            Width = 500, ;

            ControlSource = "foobar.notes", ;

            Name = "Edit1"

      PROCEDURE Load

            CREATE CURSOR foobar (notes c(50))

            APPEND blank

* REPLACE notes with "test"