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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding data

Status
Not open for further replies.

FoxEgg

Programmer
Mar 24, 2002
749
0
16
AU
Sorry... this is probably a simple one for you guys..


I created a form and I want to add cheque data to the table via the form... Amount, Chq details, date, comment etc

I had a series of text boxes for each field and, underneath, two buttons ... One 'ACCEPT' the other 'CANCEL'

The 'accept' would append the data.. the 'cancel' would clear and close the form

I assume that I should make the data source for the text boxes some temporary memory variable... and then do an 'APPEND BLANK' and REPLACE the respective fields... when I click the 'ACCEPT' button etc etc

But.. is there an easier or better way ? I looked up tableupdate... mmm tricky thought he ...

I would appreciate any advice


JF



 
FoxEgg,

I assume that I should make the data source for the text boxes some temporary memory variable... and then do an 'APPEND BLANK' and REPLACE the respective fields... when I click the 'ACCEPT' button etc etc

That would work fine. But it's not ideal. There is a small risk of things going wrong, for example, if the program fails between the APPEND BLANK and the REPLACE, you will end up with empty records. Worse, if the table has a Candidate key, the empty records will cause a unique-key violation error. Also, there's a (very small) risk of multi-user violations if two users happen to hit Accept at the same time (unlikely, but possible).

The preferred way is to put the table in buffer mode. Specify a mode that uses pessimistic locking, using either row buffering or table buffering (table buffering is generally preferred).

Next, bind the data-entry controls on the form directly to the fields in the table (using the ControlSource property).

When the user first opens the form, do the Append Blank. Let them do their edits. Then, in the Save button, issue TableUpdate(). That will commit the changes. In the Cancel button, issue TableRevert() to cancel. You don't need to do any REPLACEs, as the fields will be updated by virtue of their control sources.

In this particular case, you may feel it's not worth the trouble of doing what I've just described, and your simpler solution will work OK. That's true. However, it's worth learning the techniques of using buffering, because it will be very valuable when you develop more sophisticated forms.

Hope this helps.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
You'll get several different answers on this one.
The way I do it is:
in the INIT method
when the dbf is opened
Scatter memo name oRecord additive
or a new record it is
Scatter memo blank name oRecord additive.

this makes a memvar for every field in the DBF by FieldName so it is a property of the form.

Then in the Textbox Control source
Thisform.Fieldname

Every time I move to another record I do the scatter.

when I need to place the info into the record I:
Gather Name oRecord memo
or
Append blank
Gather name oRecord memo

Hope this helps



David W. Grewe Dave
 
Thanks Mike and Dave...

Very much appreciated

JF
 
Hello JF!

both are simple solutions. In a multiuser environment I had too many problems with network stability, so I prefer not to bind to the table directly. Dave's solution is better in that way but if you want to be independent from the backend and one day be able to move data to sql server you better create a cursoradapter. Not an easy thing for a FoxEgg, but by now you should hatch...

The big advantage of the CA is, it creates a cursor and you can act on that as if it was your foxpro table. So you can make use of Mike's or Dave's solution including tableupdate(), tablerevert() and or scatter/gather.

Bernard Bout has written a nice tutorial on CursorAdapters in foxite:
Bye, Olaf.
 
JF,

If your not incrementing sequential id's for the new records I recommend assigning the appropriate table field names to each text field, except you name them m.fieldname[\i].
Then you use a vfp insert command in the <ACCEPT> click method that will look like this.

INSERT INTO YOURTABLENAME[\i] FROM MEMVAR.

This is a simple way to do what you want without having to worry record locking and tableupdate/revert. If the user clicks cancel you simply close the program.



 
Another extension of Dave's thought(several different answers)

Code:
* init() of the form

* Group 1
* For example, List all Char data fields here
t_FieldList = "Field1,Field2,Field3" 

* Now transfer to an array
t_Tot = alist(t_a_FieldList,t_FieldList,',')

for i = 1 to t_Tot
    * setup all properties for your fileds here
    t_fName = t_a_FieldList[i]
    t_fLen  = len(tablename.&t_fName)
    i_txt&t_fName..Value = space(t_fLen)
    i_txt&t_a_FieldList[i]..inputmask = replicate('!',t_fLen)
    * set any other property as you need it left,top,etc.
endfor

* Group 2
* Copy the above Group for Numeric data, etc fields
* and continue
The advantage is, when you add/remove fields in the future you have to change only one place(maybe). And not need to know the code. Repeat the same code for replace(ing) the fields.

Name input TextBox as
i_txtFIELDNAME, where i_ for input, txt for TextBox and FIELDNAME your real FieldName from the table, or the way you want as long as Suffix is field name. It looks long and hairy but it will grow very slowly even you have 100 fields.






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top