Using InStr to find a range of ASCII characters

A

Albert S.

Hello,

I need to create a query that finds all the ascii characters from 192 to 255
and finds some others like 140, 158 & 159. I looked for a similar question in
the forums and didn't find anything.

Here is what I tried:

Public Function FindChars(textIn As String) As String
Dim strNameIn As String

strNameIn = Nz(textIn, "")

If InStr(1, strNameIn, Chr(192)-Chr(255)) > 0 Then
Debug.Print strNameIn
End If

End Function

Of course, this doesn't work. I tried a number of other variations also.

Thanks for any assistance.
 
J

John W. Vinson

Hello,

I need to create a query that finds all the ascii characters from 192 to 255
and finds some others like 140, 158 & 159. I looked for a similar question in
the forums and didn't find anything.

Here is what I tried:

Public Function FindChars(textIn As String) As String
Dim strNameIn As String

strNameIn = Nz(textIn, "")

If InStr(1, strNameIn, Chr(192)-Chr(255)) > 0 Then
Debug.Print strNameIn
End If

End Function

Of course, this doesn't work. I tried a number of other variations also.

Thanks for any assistance.

InStr won't work, that way anyhow, but if textIn is in a table field you can
use the LIKE operator in a query:

SELECT whatever
FROM table
WHERE fieldname LIKE "*[" & Chr(192) & "-" & Chr(255) & "]*";

Alternatively I would guess you'll need to do a loop through the possible
character values.
 
A

Albert S.

I tried the SQL query with

WHERE fieldname LIKE "*[" & Chr(192) & "-" & Chr(255) & "]*";

but it returns all the rows.
--
Albert S.


John W. Vinson said:
Hello,

I need to create a query that finds all the ascii characters from 192 to 255
and finds some others like 140, 158 & 159. I looked for a similar question in
the forums and didn't find anything.

Here is what I tried:

Public Function FindChars(textIn As String) As String
Dim strNameIn As String

strNameIn = Nz(textIn, "")

If InStr(1, strNameIn, Chr(192)-Chr(255)) > 0 Then
Debug.Print strNameIn
End If

End Function

Of course, this doesn't work. I tried a number of other variations also.

Thanks for any assistance.

InStr won't work, that way anyhow, but if textIn is in a table field you can
use the LIKE operator in a query:

SELECT whatever
FROM table
WHERE fieldname LIKE "*[" & Chr(192) & "-" & Chr(255) & "]*";

Alternatively I would guess you'll need to do a loop through the possible
character values.
--

John W. Vinson [MVP]

.
 
D

david

"Like" may also be used in Access VBA

Public Function liketest(s) As Boolean
liketest = (s Like "*b*")
End Function

FWIW, you can also use IN, using EVAL:

Public Function InTest(s) As Boolean
InTest = Eval("(""" & s & """in (""a"",""b"",""c""))")
End Function

(david)


John W. Vinson said:
Hello,

I need to create a query that finds all the ascii characters from 192 to
255
and finds some others like 140, 158 & 159. I looked for a similar question
in
the forums and didn't find anything.

Here is what I tried:

Public Function FindChars(textIn As String) As String
Dim strNameIn As String

strNameIn = Nz(textIn, "")

If InStr(1, strNameIn, Chr(192)-Chr(255)) > 0 Then
Debug.Print strNameIn
End If

End Function

Of course, this doesn't work. I tried a number of other variations also.

Thanks for any assistance.

InStr won't work, that way anyhow, but if textIn is in a table field you
can
use the LIKE operator in a query:

SELECT whatever
FROM table
WHERE fieldname LIKE "*[" & Chr(192) & "-" & Chr(255) & "]*";

Alternatively I would guess you'll need to do a loop through the possible
character values.
 
A

Albert S.

Thanks for the info, but I wanted to find the range of characters. I think I
will have to try to loop through each character to find them.
--
Albert S.


david said:
"Like" may also be used in Access VBA

Public Function liketest(s) As Boolean
liketest = (s Like "*b*")
End Function

FWIW, you can also use IN, using EVAL:

Public Function InTest(s) As Boolean
InTest = Eval("(""" & s & """in (""a"",""b"",""c""))")
End Function

(david)


John W. Vinson said:
Hello,

I need to create a query that finds all the ascii characters from 192 to
255
and finds some others like 140, 158 & 159. I looked for a similar question
in
the forums and didn't find anything.

Here is what I tried:

Public Function FindChars(textIn As String) As String
Dim strNameIn As String

strNameIn = Nz(textIn, "")

If InStr(1, strNameIn, Chr(192)-Chr(255)) > 0 Then
Debug.Print strNameIn
End If

End Function

Of course, this doesn't work. I tried a number of other variations also.

Thanks for any assistance.

InStr won't work, that way anyhow, but if textIn is in a table field you
can
use the LIKE operator in a query:

SELECT whatever
FROM table
WHERE fieldname LIKE "*[" & Chr(192) & "-" & Chr(255) & "]*";

Alternatively I would guess you'll need to do a loop through the possible
character values.


.
 
A

Albert S.

Ok, here is what I tried, but it doesn't work. It seems that the function is
not seeing the strChar as a string. Any suggestions? Thanks!

Public Function FindChars(s As String) As String
Dim intPos As Integer
Dim intLen As Integer
Dim intChar As Integer
Dim strChar As String

intChar = 250
strChar = "Chr(" & intChar & ")"

Do Until intChar > 255
intLen = Len(s)
intPos = 1
Do Until intPos > intLen
If InStr(intPos, s, strChar, vbTextCompare) > 0 Then
Debug.Print s
End If
intPos = intPos + 1
Loop
intChar = intChar + 1
strChar = "Chr(" & intChar & ")"
Loop
End Function
--
Albert S.


Albert S. said:
Thanks for the info, but I wanted to find the range of characters. I think I
will have to try to loop through each character to find them.
--
Albert S.


david said:
"Like" may also be used in Access VBA

Public Function liketest(s) As Boolean
liketest = (s Like "*b*")
End Function

FWIW, you can also use IN, using EVAL:

Public Function InTest(s) As Boolean
InTest = Eval("(""" & s & """in (""a"",""b"",""c""))")
End Function

(david)


John W. Vinson said:
On Wed, 12 May 2010 14:37:01 -0700, Albert S.
<[email protected]>
wrote:

Hello,

I need to create a query that finds all the ascii characters from 192 to
255
and finds some others like 140, 158 & 159. I looked for a similar question
in
the forums and didn't find anything.

Here is what I tried:

Public Function FindChars(textIn As String) As String
Dim strNameIn As String

strNameIn = Nz(textIn, "")

If InStr(1, strNameIn, Chr(192)-Chr(255)) > 0 Then
Debug.Print strNameIn
End If

End Function

Of course, this doesn't work. I tried a number of other variations also.

Thanks for any assistance.
--
Albert S.

InStr won't work, that way anyhow, but if textIn is in a table field you
can
use the LIKE operator in a query:

SELECT whatever
FROM table
WHERE fieldname LIKE "*[" & Chr(192) & "-" & Chr(255) & "]*";

Alternatively I would guess you'll need to do a loop through the possible
character values.


.
 
J

John W. Vinson

Ok, here is what I tried, but it doesn't work. It seems that the function is
not seeing the strChar as a string. Any suggestions? Thanks!

You're mixing levels, and perhaps trying too hard. Chr() is a function which
accepts an integer argument and returns a string. Try

Public Function FindChars(s As String) As String
Dim intPos As Integer
Dim intChar As Integer
Dim strChar As String

For intChar = 250 To 255
strChar = Chr(intChar)
intPos = InStr(s, strChar)
If intPos > 0 Then
Debug.Print s, intPos, Mid(s, intPos, 1)
End If
Next intChar
End Function

Sample output:

?findchars("AbcDefóÿú")
AbcDefóÿú 9 ú
AbcDefóÿú 8 ÿ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top