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

Clear Grid record

Status
Not open for further replies.

ninjaman01

Programmer
Aug 11, 2021
14
PH
Hi,
I have a textbox in my form wherein a user can choose a zip folder on the directory and click the extract button to extract the file and display the record to the grid of the form. however when a user once again extract a different zipped folder it only add more records to the grid. what i want is when a user extract a new/different zipped folder it will overwrite the existing record displaying in my grid with a new record, thank you.
Code:
 //this my code// 
mdir = "C:\Downloads\"
if .not. directory(mdir)
   md &mdir 
endif
mfile = thisform.txtEmprpath.value
if empty(mfile)
   m = messageb("No selected ZIP File. Please select folder to be extract",64,"Select ZIP File")
return
endif
mfname = juststem(mfile)
mzip = mfname +".zip"
mfile1 = mfname +".dbf"

mtofile = mfile + " " + "-o" + mdir +" "+ mfile1 + " " + "-aoa" 
run 7z x &mtofile
mfile1 = mdir + mfile1
mymess = ""
if .not. file(mfile1)
   m = messagebox("Operation Cancelled. " + mfile1 + " does not exist",64,"Extract File")
else
   use &mfile1 alias mfile1 in 0 shared

   select mfile1
   go top
   do while .not. eof()
      memprole = emprole
      mrlvl = rollevel
      scatter memvar
     
      &&Display the records of extracted dbf file
      select tmpex
      delete for emprole = memprole .and. rollevel = mrlvl
       append blank
      gather memvar
      select mfile1
      skip
   enddo
   m = messagebox("Extraction Completed",64,"Extract File")
endif
m = closedbf("mfile1")
thisform.refresh
 
Are you saying that each file contains the complete set of records you want, so all records should be removed when a new file is chosen? If that's it, then ZAP the table/cursor behind the grid and add the new records.

If you're saying that you want to replace each record that's matched in the new file, no reason to delete. Just find the record you want (with SEEK or LOCATE) and modify it. You can do that with SCATTER/GATHER as you've done, or with REPLACE or SQL-UPDATE.

A few things I'd absolutely change:

1) Use ForceExt rather than just tacking on an extension:

[pre]mzip = FORCEEXT(mfname, "zip")[/pre]

That way, if there's already an extension, you don't add a second.

2) Never use a macro in the USE command. If the path or filename contains spaces, it'll break. Use parens instead:

[pre] use (mfile1) alias mfile1 in 0 shared[/pre]

3) Use SCAN rather than DO WHILE to loop through a table. Note that it starts at the first record (so you don't need GO TOP), automatically skips to the next record at ENDSCAN, and reselects the right workarea then. So (without making the change I discussed to fix your problem):

[pre] SCAN
memprole = emprole
mrlvl = rollevel
scatter memvar

&&Display the records of extracted dbf file
select tmpex
delete for emprole = memprole .and. rollevel = mrlvl
append blank
gather memvar
ENDSCAN[/pre]


Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top