• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Seek a record in file and updating it

I have written a program to write to a file and retrieve the records. But currently i'm stuck at this where i need to search a certain record and update it.

Dim idFile As Integer
.
.
For counter=1 To Eof(idFile)
    Get idFile, counter, recStud
    If recStud.Name Like "Jessie" Then
        recStud.Name=200
        Put idFile, counter, recStud
    End If          
Next counter  

I have a problem 'putting' the updated record back or maybe i'm using a wrong way to update records. So, experts out there, pls help. Tq!
0
LotusBlur
Asked:
LotusBlur
  • 6
  • 2
1 Solution
 
rfharrisCommented:
Hello -

You may want to run this code with the Lotus debugger so you can watch this code and its results line by line during execution.

Places where I have concern are:
1) your preparation and opening of the text file: was it successful?

2) is your code handling the 'idfile' variable correctly?  (I always use the nomanclature shown in the help files when using external files.  My 'idfile' is declared as:
fileNum% = FreeFile()
and later referenced as:
Put #fileNum%, counter, rec

3) how well did it handle your swap of the recstud.name value from text to numeric?  I'm guessing this line may fail -- try putting the 200 in quotes and treating it as a text value?

I think the nomanclature of the idfile (failing to put the # in front) and  the sudden swap from text to numeric for the recStud.name are causing troubles.  The debugger should be able to help you monitor this code.

Good luck!
0
 
rfharrisCommented:
More ...

The following snippet is from Notes Designer help on use of the Get and Put statements.  I have pasted the syntax description of the 'fileNumber':

FileNumber
The number assigned to the file when it was opened with the Open statement. **Note that the pound sign (#),  fileNumber, and variableName are all required.**

(Emphasis using the "**" characters added.)
0
 
PaebdbCommented:
also, as what type of file are you opening it and do you also close it ?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
LotusBlurAuthor Commented:
Hi rfharris,

I'm pretty sure I managed to write and read from the file. That was the first part I worked on. When I added

   If recStud.Name Like "Jessie" Then
           recStud.Name="John"  (sorry, typo error earlier)
           Put idFile, counter, recStud
   End If          

That's when error occurs. From what u said, do u mean i have to write

Put #idFile%, counter,recStud

instead of idFile, counter, recStud?

I'll try this for now, let's hope it works.

p.s : for Paebdb, i used a random access file. and yes, i did close it.

Thanks

0
 
rfharrisCommented:
I suggest that both your Get and Put statements use the # prefix when referring to the idfile variable.  

The %, as an explicit declaration of type integer, is optional depending on how you had first declared/used the variable.  If you use the % in your earliest declaration/use of idfile - then continue to use it in every reference -- but do *not* add it only on the Get/Put lines.
  -R
0
 
rfharrisCommented:
By the way -- what kind of error are you getting?  When does it occur (at save/compile or during code execution)?  Message from the error?
0
 
rfharrisCommented:
The following code is tested as compiled and working.  Note that I was unable to make the "like" comparison work unless padding my search criteria with spaces to equal the length declared for the recStud.name (in my example I set it to length 20).

In the Declarations:
Type PersonRecord
      Number As Integer
      Name As String * 20
End Type

In the Initialize event:
Sub Initialize
      Dim idFile As Integer
      Dim fileName As String
      Dim recStud As PersonRecord
      idFile = Freefile()
      fileName$ = "data.txt"
' First, open a random file with a record length equal to
' the size of the records to be stored.
      Open fileName$ For Random As idFile Len = Len(recStud)
%REM
      For counter=1 To Eof(idFile)
            Get idFile, counter, recStud
            If recStud.Name Like "Jack Jones" Then
                  recStud.Name="Jack Replaced"
                  Put idFile, counter, recStud
            End If          
      Next counter          
%END REM      
      counter = 1
      NameForSearch$ = "Jack Jones"
      SearchString$ = NameForSearch$ + Space(20-Len(NameForSearch$))
      Do Until Eof(idfile)
            Get #idFile, counter, recStud             
            If recStud.Name Like SearchString$ Then
                  recStud.Name="Jack Replaced"
                  Put #idFile, counter, recStud
            End If    
            counter = counter + 1
      Loop            
      Close idFile              ' Close the file.
End Sub

0
 
rfharrisCommented:
Sorry - please ignore the code between the %REM and %END REM lines.  I was playing with two methods of advancing through the file and I forgot to remove the method closest to your own before pasting the code.
0
 
LotusBlurAuthor Commented:
thanks rfharris... i'll just take it that it works. but now i realised the thing i'm working on might not need to use writing to external file, instead, i may need to have an external database(any .dbf) and connect to Notes using ODBC. so, pls lookout for my next question as i've never done odbc before. thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now