You are here:

FoxPro/overwrite records

Advertisement


Question
QUESTION: Good day sir!
I am using VFP 9.0
Can you please give me an idea on how to overwrite records?
For example I entered dates in the date from and date to textboxes.
Date From: 02/04/2013 and Date To: 02/08/2013

It will be displayed in grid like:
DATE         DAY
02/04/2013   MON
02/05/2013   TUE
02/06/2013   WED
02/07/2013   THU
02/08/2013   FRI

I used FOR command to get the dates from 02/04/2013 to 02/08/2013.

I tried editing the dates "from and to", say from 02/02/2013 to 02/10/2013, but only the last record is being overwrite with the date from 02/02/2013, the result:

DATE         DAY
02/04/2013   MON
02/05/2013   TUE
02/06/2013   WED
02/07/2013   THU
02/02/2013   SAT

Instead of:

DATE         DAY
02/02/2013   SAT
02/03/2013   SUN
02/04/2013   MON
02/05/2013   TUE
02/06/2013   WED
02/07/2013   THU
02/08/2013   FRI
02/09/2013   SAT
02/10/2013   SUN

Can you please help me again with this?
Thank you very much!

ANSWER: AJ,

  I can't tell what's causing the problem without more information.  I remember what the form looked like.  If you could send the code that generated the data, I think I could quickly find the issue.  You can copy the code from the event which runs it, then paste it into a follow-up question.  (If you took my earlier advice, the click event of the ADD button.)

  It will also help to include any code used prior to running the form.

AND:
  What is the source of the grid? (table?, array?, etc.)
  

************** GUESSING TIME
  If the source of the grid is a table, and you try the second group of numbers right away after the first set, you may have left out the need to reposition the record pointer to the top of the file.  GO TOP before you start the second set of dates.  However, since the second group is a longer group than the first group, you will eventually need more records.  I can show you how to do that also, when you send me your code.

Fred

---------- FOLLOW-UP ----------

QUESTION: *Source: tbl_dates (fields: num,dates,datefrom,dateto)

cNo = thisform.txtNo.Value
dFrom = thisform.txtFrom.Value
dTo = thisform.txtTo.Value

FOR x = 0 TO thisform.txtTo.Value - thisform.txtFrom.Value
IF thisform.Tag = '0'   && add mode
 SELECT tmp_dates
 APPEND BLANK
 REPLACE tmp_dates.num WITH cNo,;
    tmp_dates.dates WITH dFrom + x,;
    tmp_dates.datefrom WITH dFrom,;
    tmp_dates.dateto WITH dTo
     
ELSE         && edit mode
 SELECT tmp_dates
 *GO TOP
 REPLACE tmp_dates.num WITH cNo,;
      tmp_dates.dates WITH dFrom + x,;
    tmp_dates.datefrom WITH dFrom,;
    tmp_dates.dateto WITH dTo
ENDIF
ENDFOR

*I already tried using GO TOP but the result was quiet similar, only the first record is being overwrite.

I'm still confused of this "FOR commands". I haven't use this commands yet in my other forms..I just tried this based on my readings in the VFP Help Menu.

Thank you very much sir for your patience!

Answer
AJ,

 You're only getting one record because there is nothing to move the record pointer.  In ADD mode, the APPEND BLANK moves the pointer when it adds the new record.  Try this:

GO TOP
FOR x = 0 TO thisform.txtTo.Value - thisform.txtFrom.Value
 SELECT tmp_dates
 IF thisform.Tag = '0' && add mode
    APPEND BLANK
  ELSE   && edit mode
    IF x > 0
       * don't move the pointer the first time
       SKIP
       IF EOF()
         * if date range is longer than original range
         APPEND BLANK
       ENDIF
    ENDIF
 ENDIF
 REPLACE tmp_dates.num WITH cNo,;
 tmp_dates.dates WITH dFrom + x,;
 tmp_dates.datefrom WITH dFrom,;
 tmp_dates.dateto WITH dTo
ENDFOR


JUST FYI:
The FOR...ENDFOR set works similar to the DO WHILE...ENDDO.  The difference is: FOR...ENDFOR repeats commands for a specific number of events.  DO WHILE...ENDDO is used to repeat commands until a specific condition is met.

Good Luck,

Fred

P.S. Please enter a rating just so you can use the COMMENTS to let me know if it worked out.  
I'm far more interested in knowing if it helped than in the rating system.

FoxPro

All Answers


Answers by Expert:


Ask Experts

Volunteer


Fred Frase

Expertise

Questions about development only. No installation or hardware specific questions, please. Your first step in requesting assistance should be to identify the version of FoxPro you are using. I can write short functions but, PLEASE do not ask me to write programs for you.

Experience

Nearly 30 years professional programming experience using FoxPro (FoxBase through VFP 9) or other xbase language, primarily in Windows environments.

Education/Credentials
Completed a 1,000 hour diploma course in programming at International Academy of Ohio (subsequently merged with Southern Ohio College).

©2016 About.com. All rights reserved.