msw


Hello, I'm new to VFP 9, but I'm working at it..

I have a simple form that displays a Grid with my customer records. I would like to double click and or press the enter key on a given record on the grid and open a form to make editing earier and then return to the grid when I'm done.

Can anyone give me some help with this...

Thanks...Scott




Re: How to select a record from a Grid and open a form

Naomi Nosonovsky


Yes, you may create a special grid class for this and use it. Here is what I have in my grdSorting class Initialize method (called from grid's Init): (Note, that you can also delegate KeyPress event).

Code Snippet

LOCAL loColumn, loControl

* Delegate header's click to grid's HeaderClick custom method
* and column's textbox DblClick to grid's DblClick, RightClick to grid's RightClick

FOR EACH loColumn IN THIS.COLUMNS
loColumn.ADDPROPERTY("cOriginalControlSource", m.loColumn.CONTROLSOURCE)
IF this.lShowToolTipInfo
BINDEVENT(m.loColumn,"MouseMove",THIS,"ShowToolTipInfo")
endif
FOR EACH loControl IN m.loColumn.CONTROLS
IF UPPER(m.loControl.BASECLASS) = "HEADER"
loControl.FONTSIZE = 8
BINDEVENT(m.loControl,"Click",THIS,"HeaderClick")
loControl.ADDPROPERTY("CurrentTag","") && Adds CurrentTag property
IF THIS.lCreateIndexes && Adds indexes on the fly
THIS.CreateTag (m.loControl)
ENDIF
ELSE
* Changed by Nadya Nosonovsky on 01/17/2007 16:10:45

IF PEMSTATUS(m.loControl, "DblClick",5)
BINDEVENT(m.loControl,"DblClick",THIS,"DblClick")
ENDIF
IF PEMSTATUS(m.loControl, "RightClick",5)
BINDEVENT(m.loControl,"RightClick",THIS,"RightClick")
ENDIF
ENDIF
ENDFOR
ENDFOR

IF THIS.lManualHighlight
LOCAL lcDynamicBackColor, lcDynamicForeColor

lcDynamicBackColor = "iif(recno(this.RecordSource)=this.nRecno,this.HighlightBackColor,this.BackColor)"
lcDynamicForeColor = "iif(recno(this.RecordSource)=this.nRecno,this.HighlightForeColor,this.ForeColor)"

THIS.SETALL("DynamicBackColor", m.lcDynamicBackColor, "Column")
THIS.SETALL("DynamicForeColor", m.lcDynamicForeColor, "Column")
THIS.HIGHLIGHTSTYLE = 0
ENDIF

IF THIS.lSaveGridProperties
THIS.SaveGridProperties()
ENDIF

IF THIS.READONLY
THIS.SETALL('ReadOnly',.T.)
ENDIF

And here is a code in HeaderClick -

Code Snippet

*---------------------- Location Section ------------------------
* Library: Acustomcontrols.vcx
* Class: GrdSorting
* Method: HeaderClick()
*----------------------- Usage Section --------------------------
*) Description: Custom method which fires when Header is clicked (idea from Barbara Peisch)
*)
* Scope: Public
* Parameters:
*$ Usage:
*$
* Returns:
*--------------------- Maintenance Section ----------------------
* Change Log:
* CREATED 01/06/2005 - NN
* MODIFIED
*----------------------------------------------------------------
local loCalledBy && as Object

aevents[aCurEvent,0]
loCalledBy = aCurEvent[1] && should be a Header object

if vartype(m.loCalledBy)= "O" and not empty(m.loCalledBy.parent.cOriginalControlSource)
local lcOrder, lcRecSource, llDirection
lcRecSource = this.recordsource
llDirection = this.lAscending

lcOrder = order(m.lcRecSource) && Saves current tag
this.SetOrder(m.loCalledBy.parent.cOriginalControlSource, m.loCalledBy.CurrentTag)
if this.lShowSortingArrows
* Check, if we changed the order
if not order(m.lcRecSource)== m.lcOrder or m.llDirection <> this.lAscending
** Clear the picture of the previously sorted column
loCalledBy.CurrentTag = this.cTagName
this.ClearHeaderPictures()
this.SetHeaderPicture(m.loCalledBy)
endif
endif
endif
this.mousepointer = 0

Hopefully this gives you some direction.






Re: How to select a record from a Grid and open a form

msw

Thanks for the reply, but your way over my head..

Can I cut and past the above code into the INIT command on my grid

Thanks






Re: How to select a record from a Grid and open a form

Naomi Nosonovsky

Yes, cut and paste the code with BINDEVENT (and remove index creation, etc.)

Code Snippet

LOCAL loColumn, loControl

* Delegate column's textbox DblClick to grid's DblClick, RightClick to grid's RightClick

FOR EACH loColumn IN THIS.COLUMNS

for each loControl in loColumn.Controls
IF PEMSTATUS(m.loControl, "DblClick",5)
BINDEVENT(m.loControl,"DblClick",THIS,"DblClick")
ENDIF
IF PEMSTATUS(m.loControl, "RightClick",5)
BINDEVENT(m.loControl,"RightClick",THIS,"RightClick")
ENDIF
ENDIF
ENDFOR
ENDFOR

This should be enough for you. And then just call your form from grid's DblClick method.





Re: How to select a record from a Grid and open a form

dni

You may put in double click method of the grid:

IF "given record " condition

Do form YpurForm

ENDIF

For pressed key you may put .T. on form property KeyPreview and in Keypress method pu something like:

IF LASTKEY() = 13 && for enter

IF "given record " condition

Do form YpurForm

ENDIF

ENDIF






Re: How to select a record from a Grid and open a form

msw

I'm sorry to be really dumb here but I'm still not getting this...

You have posted (3) code snippets above and I'm not sure where or if I need to past them all into my grid code window. You also mentioned in your last post that I need cut and past with BINDEVENT, I don't see that listed in the grid code area.

Thank you again for your help...





Re: How to select a record from a Grid and open a form

Naomi Nosonovsky

In my second message I posted all the code you need to make DblClick of the grid do the work for you. I didn't post code for KeyPress.

You just place the code as is from my second message into grid's Init method.

Then in the DblClick method of the grid you would put:

do form MySecondForm





Re: How to select a record from a Grid and open a form

msw

Thanks for your reply..

I've copied your code above into the doublclick code area in my grid but when I attempt to save and close I get an error message "command contains unrecognized phrase/keyword" on the "IF "given record" condition.

Thanks...Scott





Re: How to select a record from a Grid and open a form

msw

***

In my second message I posted all the code you need to make DblClick of the grid do the work for you. I didn't post code for KeyPress.

You just place the code as is from my second message into grid's Init method.

Then in the DblClick method of the grid you would put:

do form MySecondForm

***

Okay, I got it and it works great......

Thanks so much for sticking with me on this...

Now, that it's opening my form how do I get it to stay on the record that I've selected.. The form that I'm calling was created with the wizard but I didn't assign any sorts or indexes etc, just the data environment it's self.

Thanks...Scott





Re: How to select a record from a Grid and open a form

dni

"given condition" it is not code it is your condition, if you don't have one you may write:

- in double click method

Code Snippet
do form <YourFormName>

-in keypress method

Code Snippet

IF lastkey() = 13

do form <YourFormName>

ENDIF

-KeyPreview=.T. in form property windows






Re: How to select a record from a Grid and open a form

Naomi Nosonovsky

One of the possibility would be to pass the Primary Key of the table to the Child form as a parameter.

In other words, your second form would have in the Init method

lparameters tcPK && assuming that your primary keys are of character type, otherwise the parameter name would be tiID

select myTable

=seek(m.tcPK,'MyTable','MyPKTag') && Go to the record with this PK





Re: How to select a record from a Grid and open a form

dni

What is the record source of the grid

If is a cursor, table ...you should be on the same position when run the second form...






Re: How to select a record from a Grid and open a form

msw

Okay, I've verified that when I double click on the desired record the record pointer is moved to correct record, but my form displays a different record and I can't figure out why. I've tried putting in commands to refresh the form on load, init etc both nothing seems to move the forms pointer.

Any idea as to what I'm missing here

Thanks...Scott





Re: How to select a record from a Grid and open a form

Naomi Nosonovsky

Perhaps your form has some code which moves the record pointer, say, go top.

Do both forms share the same Data Session In the form's Init last line put

=messagebox(recno('myTable')) (and the same in double click) to see, if the record pointer is different.





Re: How to select a record from a Grid and open a form

msw

Yes, I believe both forms share the same data session but I'm not exactly sure what is meant by that

I've remarked out all set order commands and indexes and checked for go top messages and do not see any at this point.. I have one database called custmaster and I've created a form with a grid in it and currently this grid allows me to double click the desired record. Upon the double click it loads a form called "form2". The only thing that form2 has that I'm not sure about is the "data environment" which shows my custmaster and a payhist table that are linked together thru a field custnum(type=character).

I did put in your messagebox above and yes the record pointer jumps around. Hope this helps clear up what I'm attempting to do.

Thanks again..