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

How to check file's entry

Status
Not open for further replies.

Crusader2

Technical User
Dec 29, 2002
11
EE
I have a file, there're some numbers separated with "," example: 1, 5, 6, 8, 10. I have an integer it's equals 8 (int = 8). How to check if the int equals at least any number in that file?

P.S.
Sorry about my english
 
its lengthy but it works...

Private Sub Command1_Click()

Dim Mystring As String
Dim Stringlength As Integer
Dim Mynumbers() As Integer

j = 0

'load the file
Open App.Path & "\temp.txt" For Input As #1
'assuming your only using 1 line
Line Input #1, Mystring
Close #1

Stringlength = Len(Mystring)

'find your punctuation
For i = 1 To Stringlength
FoundValue = InStr(i, Mystring, ",", vbTextCompare)
If FoundValue = 0 Then
'not found
Exit For
Else
ReDim Preserve Mynumbers(j)
Mynumbers(j) = Mid(Mystring, i, FoundValue - i)
j = j + 1
i = FoundValue
End If
Next i

MyInt = 8

'validate what youve found
For i = 0 To j - 1
If Val(Mynumbers(i)) = MyInt Then
MsgBox "Found Value.", vbOKOnly, "Message"
End If
Next i

End Sub

hope it helps. I like doing it the hard way! OK!
 
well...
compiller says Expected Array in this part:
"ReDim Preserve Mynumbers(j)"

here's a part of code, so you navigate better:
Else
ReDim Preserve Mynumbers(j)
Mynumbers(j) = Mid(Mystring, i, FoundValue - i)
j = j + 1
i = FoundValue
End If

HELP please
 
hmmm... the code there was straight from a lil program i wrote (slightly modified) but i tested it and it worked... i pressume there may be something in your code that bounces of this code if it doesnt work. (to test you could start a new project and dump a command button on the form and see if it works.)

in the meantime ill take another look.

when you single step the code does the MyString value contain the numbers you want to search.

i honestly cant think why its not working (hopefully a tech will see this and give me a hand) I like doing it the hard way! OK!
 
this is straight from msdn (apologise if youve already read it)

Expected array


A variable name with a subscript indicates the variable is an array. This error has the following cause and solution:

The syntax you specified is appropriate for an array, but no array with this name is in scope.
Check to make sure the name of the variable is spelled correctly. Unless the module contains Option Explicit, a variable is created on first use. If you misspell the name of an array variable, the variable may be created, but not as an array.

For additional information, select the item in question and press F1.
I like doing it the hard way! OK!
 
pressed the button to early.... what i was going to add was that maybe you need to put the
Dim Mynumbers() As Integer
at the top of your code?!?!? I like doing it the hard way! OK!
 
hmmm...little problem actually I'm doing it not in VB, i'm using VBA (Visual BASIC for Applications) built-in MS Exel, could that be a reason i have errors?
 
ive just stuck the code code in excel (office 2000) and it works. (had to change the app.path bit to the actual location of the file)

any chance you could post the code?!? I like doing it the hard way! OK!
 
Sorry, my internet didn't work for a while :(
Yes, it's works just fine now!!! Thank you very much!!!
 
This one builds a string giving the numbers element position in the string.

Option Explicit
Public CapString As String

Public Sub FindNumber()
Dim hfile As Long
Dim buff As String
Dim buffarray() As String
Dim sPathname As String
Dim intMyNbr As Integer: intMyNbr = 8
Dim intLo As Integer
Dim intHi As Integer
Dim intKnt As Integer

sPathname = App.Path + "\filein.txt"
hfile = FreeFile
Open sPathname For Input As #hfile
buff = Input$(LOF(hfile), hfile)
Close

buffarray = Split(buff, ",")
intLo = LBound(buffarray)
intHi = UBound(buffarray)
For intKnt = intLo To intHi
If intMyNbr = CInt(buffarray(intKnt)) Then
CapString = "The number is in the " _
& Str(intKnt + 1) _
& " Element of the String"
Exit For
End If

Next intKnt
End Sub
Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
hmm...I have one more question...
and if i want to make action when there's no 8 in that file, for example if there's no 8 in the file app shows msgbox ("There's no 8 in the file")
 
erm

For i = 0 To j - 1
If Val(Mynumbers(i)) = MyInt Then
MsgBox "Found Value.", vbOKOnly, "Message"
End If
Next i

change to

dim MyFlag as boolean
MyFlag=false
For i = 0 To j - 1
If Val(Mynumbers(i)) = MyInt Then
MsgBox "Found Value.", vbOKOnly, "Message"
Myflag=true
End If
Next i

if MyFlag= false then
msgbox "Value not found.",vbokonly,"message"
end if

again its crude but it works....

enjoy! I like doing it the hard way! OK!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top