Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Premature loading 2

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
I am using a grid to display a cursor which is created within a form. How do I prevent the grid from trying to loading data before the cursor is created?

Create an empty cursor at the start perhaps?

Keith
 
Create you cursor in the load event of the form, that way when the grid gets initialized, the cursor already exists.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Just realised why I struggle with VFP, my name isn't Mike!

Tried Mike Lewis's suggestion but still get 'YORPICTURES' not found.

Mike Gagnon's suggestion sounds more promising and putting
Code:
SELECT PICNAM, MANUF, VPW, VPH, PICTOT FROM FRAMES WHERE PICTOT = 5.23 INTO CURSOR YORPICTURES
into the load event works ok but when this code, in a text box runs.
Code:
COUNT ALL FOR FRAMES.CNUM = LOOKCUS AND FRAMES.PICTOT > 0 TO MATCHING

IF MATCHING > 0
SELECT PICNAM, MANUF, VPW, VPH, PICTOT FROM FRAMES WHERE CNUM = LOOKCUS AND PICTOT > 0 INTO CURSOR YORPICTURES

ThisForm.Grid1.recordsource=YORPICTURES
ENDIF
I just get a white box where the grid was.
Matching has a value of 9, in my test set.

Keith
 
Keith,

Tried Mike Lewis's suggestion but still get 'YORPICTURES' not found.

Where does 'YORPICTURES' come from? You didn't mention that before.

I'm sure my suggestion will work, but obviously there might be other factors involved.

I just get a white box where the grid was

One possibility would be to make the grid invisible until the cursor is ready. That might be better from a cosmetic point of view.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Another way would be to intentionally create the cursor in your form's INIT method, but leave it with only a single blank row. You might also create an index such as:
INDEX ON RECNO() TAG Display FOR !DELETED()

Then when you need it populated, append those records into the already existing cursor.

Should you need to change the cursor contents, you could always DELETE the existing records and add new ones. The Index should prevent the old records from appearing.

Good Luck,
JRB-Bldr
 
Another way would be to intentionally create the cursor in your form's INIT method, but leave it with only a single blank row.

Sounds like a good idea, but you'd need to do in the Load rather than the form's Init, as the grid would already have been instantiated by the time the Init fires.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
try this

Create you grid in the load event and remove the line
Code:
ThisForm.Grid1.recordsource=YORPICTURESENDIF

And put it in the init.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
I have been re working this app a bit but the problem remains the same.

YORPICTURES is declared in the form's load event to stop it moaning about the lack of data. The data is now taken from the 'valuation' table which is created from the original frames table but will store the latest values for future reference.

Code:
SELECT REC, CNUM, INVNO, PICNAM, ARTIST, VPW, VPH, PICTOT FROM VALUATION WHERE PICTOT =5 INTO CURSOR YORPICTURES

This part works and populates the grid with all the records where pictot=5

Code:
IF LASTKEY() = 13
	SELECT CUSTOMER
	SET ORDER TO C_SNAME
	THISNAME=UPPER(ALLTRIM(THIS.VALUE))
	COUNT ALL FOR CUSTOMER.C_SNAME=THISNAME TO NUMBR
	IF NUMBR > 0
		SEEK THISNAME
		IF FOUND()
			WAIT WINDOW "found" NOWAIT
			IF NUMBR>1
				BROW FOR CUSTOMER.C_SNAME=THISNAME TITLE "Select Customer"
			ENDIF
			LOOKCUS=CUSTOMER.CNUM
			SELECT VALUATION
			COUNT ALL FOR VALUATION.CNUM = LOOKCUS TO MATCHING
			IF MATCHING > 0
				SELECT PICNAM, CNUM, INVNO, ARTIST, VPW, VPH, PICTOT FROM VALUATION WHERE CNUM = LOOKCUS INTO CURSOR YORPICTURES
SELECT YORPICTURES
BROW
			ENDIF
		ENDIF
	ELSE
		WAIT WINDOW " notfound" NOWAIT
		M=MESSAGEBOX("Name not found",48,"System Message")
		LOOKINV=0
		LOOKCUS=""
	ENDIF
	THISFORM.REFRESH
	THIS.VALUE=''
ENDIF
[code]

This section is in a text box within the form. A surname is entered, all matching surnames are displayed in a browse window. The required one is selected, hit ESC, all the items purchased by that customer are displayed in another browse window (this browse is just to verify that there is matching data).
Clearing this browse window should display the same data within the grid, who's recordsource property is set to YORPICTURES but the grid just appears as an empty white box.

The grid's recordsource is set correctly as it displays first time round but why does it not display the second time? 
This really is frustrating.

Keith
[URL unfurl="true"]www.studiosoft.co.uk[/URL]
 
Keith,

The grid's recordsource is set correctly as it displays first time round but why does it not display the second time?

Because every time you execut SELECT .... INTO CURSOR YORPICTURES, the cursor is destroyed immediately before being recreated. When you destroy a cursor that is bound to a grid, the grid loses its columns, and therefore displays as an empty rectangle.

The easiest solution is to do what I originally suggested: set the grid's RecordSource to an empty string. Do that before you do the SELECT, and then set it back to "YORPICTURES" afterwards.

(And don't forget the double-quotes when setting the RecordSource. If you leave them out - as you did in your earlier code - VFP will look for a variable called YORPICTURES and complain when it can't find it).

Another solution would be do the SELECT into an intermediate cursor; then ZAP YORPICTURES, then append the contents of the intermediate cursor into it. That way, the RecordSource will remain in existence the whole time, so the problem won't occur.

Similarly, you could use a local view instead of a cursor. That would work in a similar way.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Thanks Mike, I knew there had to be a simple solution, there usually is.
The business with the speech marks was the reason for my earlier attempts failing. I assumed that as it was a var, the speech marks wouldn't be required but I realise that it is not a var but an instruction - oops.

Adding this line, after the select statement above, solved the problem.
Code:
ThisForm.Grid1.recordsource="YORPICTURES"
Thankyou very much.


Keith
 
My client has now decided that he wants to be able to edit the resulting data and also add now records to the VALUATION table. Adding records is easy enough but how do I make the data editable. As far as I know, the cursor I created can only be read only.
The data is in free tables in this app.

Keith
 
Keith,

Glad to hear it's now working.

As for the grid being editable: the grid is editable by default - unless you expicitly set either the entire grid or its columns to be read-only.

The problem is that the cursor that populates the grid is read-only. That's because you created it with a SELECT statement. It's easy to make the cursor editable: you just add READWRITE to the SELECT. But the real problem is what you do with the edits.

You need a way for the edits in the cursor to get back to the underlying table. There are several ways of doing that. My preferred approach is to table-buffer the cursor; then, when the user is ready to save their edits, you issue a TABLEUPDATE() on it. You would also need a way for the user to cancel the edits, at which point you do a TABLEREVERT().

Another possibility is to use a local view instead of a cursor. A view can be made updateable, with the updates automatically going back to the base table.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
I thought views were only possible on databases, not free tables.

No. A local view can be based on both database tables and free tables. You might be confused by the fact that the view itself has to be in a database, but that doesn't stop you basing it on a free table.

Is this the best way of selecting and editing a subset of data in VFP6?

It's not my preferred method. I would favour creating a cursor (as you have done), but it's a matter of taste. A local view would work as well.

By the way, I think my previous post might be misleading. I said that you should buffer the cursor, and then issue a TABLEUPDATE() on it to commit the changes. That won't achieve the goal of writing the changes to the underlying table. You would need to write additional code to do that explicitly.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Thanks Mike
This is getting worse now.
My original idea was to display the relevant info in a grid of dynamically created text boxes which could be edited and then the whole set could be saved or discarded all at once.

This method was, to me simple but I thought I would have a go at using some of VFP's in built tools, the grid in particular. I have since gone back to my original method, figuring that I can have it written in the time it will take me to manipulate the pesky VFP controls and I know for sure that my method will work.

Keith
 
"My original idea was to display the relevant info in a grid of dynamically created text boxes which could be edited and then the whole set could be saved or discarded all at once.

I thought I would have a go at using some of VFP's in built tools, the grid in particular. "


This is a very common and do-able approach.

"This method was, to me simple"

While it may not be SIMPLE, it is not too difficult.

It will give you an opportunity to learn that "the pesky VFP controls" aren't noticeably any more challenging (a.k.a. 'pesky') than those in other languages.

What makes you think that it is too difficult for you to use?

Good Luck,
JRB-Bldr
 
It will give you an opportunity to learn that "the pesky VFP controls" aren't noticeably any more challenging (a.k.a. 'pesky') than those in other languages.

Do you have any suggestions regarding how to edit the data in a read only cursor view?


Keith
 
Keith,

Have you definitely decided to use a view?

If so, you should be able to bind the view to the grid (that is, point the grid's RecordSource to the view). Make sure the view is updatable.

If I've understood it right, that's all you need to do. Provided the grid is not read-only, the user will be able to edit it. As they move the highlight off each row, the updates will automatically get back to the underlying table.

If you prefer to let the user review the edits before committing them, and to have a chance to cancel them, then you'll need to buffer the view. But let's take one step at a time.

Does the above answer your question?

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
First, if you want the users to possibly edit the data in the Grid, why are you using a "read only cursor view ?

Personally I would have used a R/W cursor or table in the Grid.
You can always make the non-editable Grid columns Read-only - either manually when configuring the Form's Grid or programatically.

Also following Mike's suggestions above would be a good idea for controlling updates to the back-end.

Good Luck,
JRB-Bldr




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top