Solved

Seek a record in file and updating it

Posted on 2000-02-16
9
210 Views
Last Modified: 2013-12-18
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
Comment
Question by:LotusBlur
  • 6
  • 2
9 Comments
 
LVL 1

Expert Comment

by:rfharris
Comment Utility
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
 
LVL 1

Expert Comment

by:rfharris
Comment Utility
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
 
LVL 2

Expert Comment

by:Paebdb
Comment Utility
also, as what type of file are you opening it and do you also close it ?
0
 

Author Comment

by:LotusBlur
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:rfharris
Comment Utility
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
 
LVL 1

Expert Comment

by:rfharris
Comment Utility
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
 
LVL 1

Accepted Solution

by:
rfharris earned 70 total points
Comment Utility
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
 
LVL 1

Expert Comment

by:rfharris
Comment Utility
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
 

Author Comment

by:LotusBlur
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For Desktop Techs: How to retain a user's Notes configuration data when swapping out the end user's computer. (Assuming that you are not upgrading to a completely different version of Notes client) All you need to do is: 1) install Notes o…
For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now