searching a text file
|
|
Thread rating:  |
Bhavik Dhokia - 28 Nov 2004 13:22 GMT In my program, a user has to enter his name, ID and location n then when he clicks on Submit Interest, I would like the program to search in a text file to see if his ID is already submitted and if he has then dont write it again to the text file and if his ID is not in the text file then add it to the text file.
So far I can add the name, ID and location to the text file but i cant get it to search first. Please could someone help.
bhavik
Norm Cook - 28 Nov 2004 13:45 GMT For something like this you might try code along these lines (air code)
Private Type RecInfo UName as String UID as Long ULoc as String End Type Dim Records() as RecInfo Dim RecCount as Long Dim FF as Long Dim i as Long
'populate the array Records(1).UName = "Joe Blow" Records(1).UID = 1 Records(1).ULoc = "somewhere" etc, use a loop to get your saved data into the array
'Save the data Open "c:\somepath\Records.Txt" for Binary as FF Put#ff, , RecCount 'start with number of records For i = 1 to RecCount 'the write them out Put#FF, , Records(i) Close FF
'Retrieve the data Open "c:\somepath\Records.Txt" for Binary as FF Get#ff, , RecCount 'get the count For i = 1 to RecCount 'and read them in Get#FF, , Records(i) Close FF
'Search for ID Private Function IDExists(byval SearchID as long) as Boolean Dim i as Long For i = 1 to RecCount If Records(i).UID = SearchID then IDExists=True Exit Function end if Next End Function
> In my program, a user has to enter his name, ID and location n then when he > clicks on Submit Interest, I would like the program to search in a text file [quoted text clipped - 6 lines] > > bhavik Bhavik Dhokia - 28 Nov 2004 14:00 GMT sorry but i dont get you.
this is the code so far:
Option Explicit
Dim Flag As Boolean Dim Filename As String
Private Sub validation1()
If (opt18.Value) Or (opt31.Value) Or (opt41.Value) Or (opt55.Value) Then Flag = True Else Flag = False MsgBox ("You must select your age.") End If
If (opt0.Value) Or (optOver2.Value) Or (optOver10.Value) Then Flag = True Else Flag = False MsgBox ("You must select the number of years of your service.") End If
If (txtAnnualSalary.Text = "") Then Flag = False MsgBox ("You must enter your annual salary.") Else Flag = True End If
End Sub
Private Sub cmdCalculate_Click()
Dim AnnualSalary, TotalPay As Currency
Call validation1 If Flag = True Then
AnnualSalary = txtAnnualSalary.Text
If (opt18.Value) And (opt0.Value) Then TotalPay = (Val(AnnualSalary) * 0.025) + ((Val(AnnualSalary) / 4)) ElseIf (opt31.Value) And (opt0.Value) Then TotalPay = (Val(AnnualSalary) * 0.05) + ((Val(AnnualSalary) / 4)) ElseIf (opt41.Value) And (opt0.Value) Then TotalPay = (Val(AnnualSalary) * 0.075) + ((Val(AnnualSalary) / 4)) ElseIf (opt55.Value) And (opt0.Value) Then TotalPay = (Val(AnnualSalary) * 0.1) + ((Val(AnnualSalary) / 4)) ElseIf (opt18.Value) And (optOver2.Value) Then TotalPay = (Val(AnnualSalary) * 0.025) + ((Val(AnnualSalary) / 2)) ElseIf (opt31.Value) And (optOver2.Value) Then TotalPay = (Val(AnnualSalary) * 0.05) + ((Val(AnnualSalary) / 2)) ElseIf (opt41.Value) And (optOver2.Value) Then TotalPay = (Val(AnnualSalary) * 0.075) + ((Val(AnnualSalary) / 2)) ElseIf (opt55.Value) And (optOver2.Value) Then TotalPay = (Val(AnnualSalary) * 0.1) + ((Val(AnnualSalary) / 2)) ElseIf (opt18.Value) And (optOver10.Value) Then TotalPay = (Val(AnnualSalary) * 0.025) + (Val(AnnualSalary)) ElseIf (opt31.Value) And (optOver10.Value) Then TotalPay = (Val(AnnualSalary) * 0.05) + (Val(AnnualSalary)) ElseIf (opt41.Value) And (optOver10.Value) Then TotalPay = (Val(AnnualSalary) * 0.075) + (Val(AnnualSalary)) ElseIf (opt55.Value) And (optOver10.Value) Then TotalPay = (Val(AnnualSalary) * 0.1) + (Val(AnnualSalary)) End If
txtTotal.Text = Format(TotalPay, "#,##0.00") txtTotal.Visible = True cmdReset.Enabled = True
lin1.Visible = True lblID.Enabled = True lblName.Enabled = True lblRequired.Enabled = True lblSiteLocation.Enabled = True txtID.Enabled = True txtName.Enabled = True cboLocations.Enabled = True cmdSubmitInterest.Enabled = True
End If
End Sub
Private Sub cmdReset_Click()
opt18.Value = False opt31.Value = False opt41.Value = False opt55.Value = False opt0.Value = False optOver2.Value = False optOver10.Value = False txtAnnualSalary.Text = "" txtTotal.Text = "" txtTotal.Visible = False cmdReset.Enabled = False
lin1.Visible = False lblID.Enabled = False lblName.Enabled = False lblRequired.Enabled = False lblSiteLocation.Enabled = False txtID.Enabled = False txtName.Enabled = False cboLocations.Enabled = False cmdSubmitInterest.Enabled = False
End Sub
Private Sub cmdSubmitInterest_Click()
Dim Name As String Dim ID As String Dim Location As String
Name = txtName.Text ID = txtID.Text Location = cboLocations.Text Open Filename For Append As #1 Write #1, Name, ID, Location Close #1 txtName.Text = "" txtID.Text = "" cboLocations.Text = "Birmingham"
End Sub
Private Sub Form_Load() Filename = App.Path & "\Interested.txt" End Sub
But in Private Sub cmdSubmitInterest_Click() i dont know how to search first to see if the ID is already in the Interested.txt. If it is then i dont want to write the details into the file and if isnt in the file already then i want to write them into the file as shown above.
cheers
bhavik
"Norm Cook" <normcookNOSPAM@cableone.net> wrote in message news:10qjli2b49mm 1a3@corp.supernews.com...
> For something like this you might try code along these lines (air code) > [quoted text clipped - 52 lines] > > > > bhavik Randy Birch - 28 Nov 2004 17:13 GMT So many problems <sigh>. :-(
1) why potentially 3 message boxes for the validation routine? Assuming your user has even minimal intelligence, give them one message indicating what the three requirements for successfully entering data are, eg (air code).
Private Sub validation1()
Flag = (opt18.Value Or _ opt31.Value Or _ opt41.Value Or _ opt55.Value Or _ opt0.Value Or _ optOver2.Value Or _ optOver10.Value) And Len(Text1.Text) > 0 End Sub
That said, personally I don't believe a validation routine should raise an error (msgbox), but simply return a value the subsequent code can use to determine whether to proceed:
Private Function IsValidInput() as boolean
IsValidInput = (opt18.Value Or _ opt31.Value Or _ opt41.Value Or _ opt55.Value Or _ opt0.Value Or _ optOver2.Value Or _ optOver10.Value) And Len(Text1.Text) > 0 End Sub
This removes the requirement to have a public variable to store the status of the validation routine:
Private Sub cmdCalculate_Click()
Dim AnnualSalary, TotalPay As Currency
If IsValidInput() Then
AnnualSalary = txtAnnualSalary.Text ... and so on
2) As has been mentioned umpteen times before:
Dim AnnualSalary, TotalPay As Currency
... creates a variant for the AnnualSalary variable. If you want a variant, define it as such, otherwise define it to a specific data type. Since all your calculation muck with the value assigned to AnnualSalary, make it a numeric data type Single ...
Dim AnnualSalary As Single, TotalPay As Currency
3) Look carefully at the cmdCalculate_Click routine. Where can you see improvement? I see two places right off the bat. First, dump the multiple Val calls and do it once only ...
AnnualSalary = Val(txtAnnualSalary.Text)
Naturally this has no checks for non-numeric entry, but we'll skip over that for now.
Next, you are essentially doing the same calculations repeatedly, with a different divisor based on opt0, optOver2, and optOver10 (or so it seems). Why not perform one check to set a variable to indicate the divisor?
Dim divisor as long
Select case True 'some don't like this; I do. case opt0.value : divisor = 4 : case optOver2.value: divisor = 2 case optOver10.value: divisor = 1 end select
Now you only need to test one set of option buttons, with one block of code:
If (opt18.Value) Or (opt18.Value) Then TotalPay = (AnnualSalary * 0.025) + (AnnualSalary / divisor)
ElseIf (opt31.Value) Or (optOver2.Value) Or (optOver10.Value) Then TotalPay = (AnnualSalary * 0.05) + (AnnualSalary / divisor)
ElseIf (opt41.Value) Or (optOver2.Value) Or (optOver10.Value) Then TotalPay = (AnnualSalary * 0.075) + (AnnualSalary / divisor)
ElseIf (opt55.Value) Or (optOver2.Value) Or (optOver10.Value) Then TotalPay = (AnnualSalary * 0.1) + (AnnualSalary / divisor) End If
In fact, you could nuke half the code since it's the same and again, perform the calculations only once ...
Dim divisor as long Dim adjustment as Single
Select case True 'some don't like this; I do. case opt0.value : divisor = 4 case optOver2.value: divisor = 2 case optOver10.value: divisor = 1 end select
adjustment = Val(AnnualSalary) / divisor
If (opt18.Value) Or (opt18.Value) Then TotalPay = (AnnualSalary * 0.025) + adjustment
ElseIf (opt31.Value) Or (optOver2.Value) Or (optOver10.Value) Then TotalPay = (AnnualSalary * 0.05) + adjustment
ElseIf (opt41.Value) Or (optOver2.Value) Or (optOver10.Value) Then TotalPay = (AnnualSalary * 0.075) + adjustment
ElseIf (opt55.Value) Or (optOver2.Value) Or (optOver10.Value) Then TotalPay = (AnnualSalary * 0.1) + adjustment End If end if
You get the drift. Further optimization (and more readable code) can be attained by doing the same thing for the rates the annualsalary is increased based on option 18,31,41, and 55. With this done you should be able to get to a one-liner with no If statements that might look like:
totalpay = increasedAnnualSalary + adjustment
4) NEVER use a hard-coded file number such as #1. VB provides a function for this -- FreeFile. Use it.
dim hfile as long
hfile = freefile open whatever for input as #hfile .. etc
5) If the ID value you're interested in is unique, you can load the file into a single string and use Instr() to determine if there is a matching string in the file ...
dim hfile as long dim buff as string
hfile = freefile open whatever for input as #hfile buff = input$(LOF(hfile), hfile)
if instr(1, buff, "yourIDhere", vbTextCompare) then debug.print "Yep, it's in here already"
...etc
 Signature Randy Birch MS MVP Visual Basic http://vbnet.mvps.org/
: sorry but i dont get you. : [quoted text clipped - 200 lines] : > > : > > bhavik Hans Eekels - 28 Nov 2004 17:33 GMT Great Randy ! Why not write a book: Visual Basic for all those wonderfull ambitious but "calculating" guys and girls of this generation who: never read a book (oops, contradictionary) never read a manual (RTFM you know) never heard of Google (can't find it) never found the MSDN (illegal version of course) never search within the NG ( uuh ??) have bad teachers (just one chapter ahead of the group) etc Then we make a FAQ and close this NG :)
HE
> So many problems <sigh>. :-( > [quoted text clipped - 373 lines] > : > > > : > > bhavik Bhavik Dhokia - 28 Nov 2004 18:09 GMT Ccheers mate
Call validation1 If Flag = True Then
AnnualSalary = Val(txtAnnualSalary.Text)
Select Case True Case opt0.Value: Divisor = 4 Case optOver2.Value: Divisor = 2 Case optOver10.Value: Divisor = 1 Case opt18.Value: Multiplier = 0.025 Case opt31.Value: Multiplier = 0.05 Case opt41.Value: Multiplier = 0.075 Case opt55.Value: Multiplier = 0.1 End Select
PayoutAmount = AnnualSalary / Divisor Percentage = AnnualSalary * Multiplier
TotalPay = Percentage + PayoutAmount
Just wondering now how come this dont work? any ideas??
bhavik
> So many problems <sigh>. :-( > [quoted text clipped - 203 lines] > : ElseIf (opt18.Value) And (optOver2.Value) Then > : TotalPay = (Val(AnnualSalary) * 0.025) + ((Val(AnnualSalary) / 2))
> : ElseIf (opt31.Value) And (optOver2.Value) Then > : TotalPay = (Val(AnnualSalary) * 0.05) + ((Val(AnnualSalary) / 2)) > : ElseIf (opt41.Value) And (optOver2.Value) Then > : TotalPay = (Val(AnnualSalary) * 0.075) + ((Val(AnnualSalary) / 2))
> : ElseIf (opt55.Value) And (optOver2.Value) Then > : TotalPay = (Val(AnnualSalary) * 0.1) + ((Val(AnnualSalary) / 2)) [quoted text clipped - 148 lines] > : > > > : > > bhavik Rick Rothstein - 28 Nov 2004 18:23 GMT > Call validation1 > If Flag = True Then [quoted text clipped - 17 lines] > > Just wondering now how come this dont work? any ideas?? The Select Case statement block is not a sequentially executing set of statements; rather, it is an alternate method of doing this
If .... Then ..... ElseIf .... Then .... ElseIf ..... Then etc.
So, the **first** Case statement that yields a True result (what you set for the Select Case argument) gets executed and all the rest are skipped.
Rick - MVP
Randy Birch - 29 Nov 2004 04:13 GMT The select case will execute the first item you that meets the True condition. For situations where more than one needs to be determined, separate select case statements would be required. Looking at your code I would suspect two ... one for the divisor group, and one for the multiplier group.
 Signature Randy Birch MS MVP Visual Basic http://vbnet.mvps.org/
: Ccheers mate : [quoted text clipped - 240 lines] : > : ElseIf (opt31.Value) And (optOver2.Value) Then : > : TotalPay = (Val(AnnualSalary) * 0.05) + ((Val(AnnualSalary) / 2))
: > : ElseIf (opt41.Value) And (optOver2.Value) Then : > : TotalPay = (Val(AnnualSalary) * 0.075) + ((Val(AnnualSalary) / : 2)) : > : ElseIf (opt55.Value) And (optOver2.Value) Then : > : TotalPay = (Val(AnnualSalary) * 0.1) + ((Val(AnnualSalary) / 2))
: > : ElseIf (opt18.Value) And (optOver10.Value) Then : > : TotalPay = (Val(AnnualSalary) * 0.025) + (Val(AnnualSalary)) [quoted text clipped - 150 lines] : > : > > : > : > > bhavik Rick Rothstein - 28 Nov 2004 15:46 GMT > In my program, a user has to enter his name, ID and location n then when he > clicks on Submit Interest, I would like the program to search in a text file [quoted text clipped - 4 lines] > So far I can add the name, ID and location to the text file but i cant get > it to search first. Please could someone help. What does the ID look like? Is it a number of combination of digits and characters? If the latter, is there a "structure" to the combination (such as, first character a letter, second through sixth characters digits, last character another letter or some other fixed combination)? Is the ID delimited in your file in any way (that is, it surrounded by parentheses, have a dash in front of it and a newline character sequence after it, on a line all by itself, or some other unique delimiter combination highlighting it)? What I'm looking for here is... what makes it uniquely findable in your file (so that an accidental finding of the same combination of characters making up the ID somewhere else in your file). For future reference in asking questions on newsgroups... the more information you give us to work with, the better able someone here can figure out what you have, what you want to do with it and how to accomplish that.
Rick - MVP
Bhavik Dhokia - 28 Nov 2004 18:08 GMT it is just sum numbers like, 123 or 001 or 102
> > In my program, a user has to enter his name, ID and location n then > when he [quoted text clipped - 26 lines] > > Rick - MVP Rick Rothstein - 28 Nov 2004 18:25 GMT > it is just sum numbers like, 123 or 001 or 102 But is it on a line by itself in the file? Or is it surrounded by some delimiters in the file (like this <123> maybe)? Or is it just a number embedded on a line along with (possibly) other numbers?
Rick - MVP
Bhavik Dhokia - 28 Nov 2004 18:31 GMT e.g.
"bhavik dhokia","003","Manchester"
> > it is just sum numbers like, 123 or 001 or 102 > [quoted text clipped - 3 lines] > > Rick - MVP Rick Rothstein - 28 Nov 2004 18:57 GMT > > > it is just sum numbers like, 123 or 001 or 102 > > [quoted text clipped - 5 lines] > > "bhavik dhokia","003","Manchester" Okay. try this function out where it reads the entire file into a String variable and then uses the InStr function to see if the ID number is in there. Since you didn't ask for any more than that, the function returns True if the ID is in the file and False if it isn't.
Function IsFileID(PathAndFileNameOfDataFile As String, _ IDnumber As Variant) As Boolean Dim FileNum As Integer Dim TotalFile As String Dim ID As String ' Just in case you passed in the ID number ' surrounded by parentheses, I'll remove them. ' This way, later on, when I search for it, I'll ' know the "shape" of the ID is without quotes. ID = Replace(IDnumber, """", "") ' Get a file channel number that isn't being used FileNum = FreeFile ' Reads the entire file into memory all at once Open PathAndFileNameOfDataFile For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' At this point, the entire file is contained in the ' TotalFile variable, so use InStr to see if the ID ' is in it (here, I add quotes around the ID since ' all ID numbers are surrounded by quotes in the file IsFileID = (InStr(TotalFile, ",""" & ID & """,")) End Function
Rick - MVP
Bhavik Dhokia - 28 Nov 2004 19:08 GMT Sorry but i dont understand where to put this piece of code. Should i replace it with the code below?
Private Sub cmdSubmitInterest_Click()
Dim Name As String Dim ID As String Dim Location As String
Name = txtName.Text ID = txtID.Text Location = cboLocations.Text Open Filename For Append As #1 Write #1, Name, ID, Location Close #1 txtName.Text = "" txtID.Text = "" cboLocations.Text = "Birmingham"
End Sub
Private Sub Form_Load() Filename = App.Path & "\Interested.txt" End Sub
> > > > it is just sum numbers like, 123 or 001 or 102 > > > [quoted text clipped - 38 lines] > > Rick - MVP Steve Gerrard - 28 Nov 2004 18:32 GMT | > it is just sum numbers like, 123 or 001 or 102 | [quoted text clipped - 3 lines] | | Rick - MVP To save us the agony of watching you extract the required information from OP (<g>), here is a snip of code he posted earlier in this thread:
Name = txtName.Text ID = txtID.Text Location = cboLocations.Text Open Filename For Append As #1 Write #1, Name, ID, Location Close #1
Bhavik Dhokia - 28 Nov 2004 18:37 GMT So i want to check when the cmdSubmitInterest is clicked, if txtID.Text is in Interested.text and if it isnt then write it in:
> Name = txtName.Text > ID = txtID.Text > Location = cboLocations.Text > Open Filename For Append As #1 > Write #1, Name, ID, Location > Close #1 Else: msgbox("You have already submitted your interest!")
> | > it is just sum numbers like, 123 or 001 or 102 > | [quoted text clipped - 13 lines] > Write #1, Name, ID, Location > Close #1 Rick Rothstein - 28 Nov 2004 18:42 GMT > | > it is just sum numbers like, 123 or 001 or 102 > | [quoted text clipped - 13 lines] > Write #1, Name, ID, Location > Close #1 LOL... Thanks, I missed that when I skimmed the other posting.
Rick
Dan Tex1 - 28 Nov 2004 19:04 GMT >In my program, a user has to enter his name, ID and location n then when he >clicks on Submit Interest, I would like the program to search in a text file [quoted text clipped - 4 lines] >So far I can add the name, ID and location to the text file but i cant get >it to search first. Please could someone help. cough.. ???HOMEWORK??? cough, cough, cough..
Dan :-)
|
|
|