Link to home
Start Free TrialLog in
Avatar of daverichardson
daverichardson

asked on

Convert ASCII to String

I have a table containing a lot of text, contained within the text is ASCII coding, I am looking for a way to update the ASCII code to its string value
E.g. change [ to [
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Well, Chr(91) will return [

mx
you will need an update query

update NameOftable
set [fieldName]=replace([fieldName],"#91",chr(91))

Chr(Replace("&#91","&#",""))  

returns

[
Avatar of daverichardson
daverichardson

ASKER

Thanks for the replies, I should have made myself a bit clearer [ isnt the only ASCII coding that was just an example there are lots of different characters. I was looking for a way to convert all ASCII coding to string.
well, you have to give more info regarding your table.

you  will need a function to do that.
*open the table as recordset
*locate and replace all occurence of the ASCII characters


if you can zip your db and upload (Now, i'll be busy later on) here www.ee-stuff.com 
i can write you a function to do that..


Create a Function as below.  Use the Function in your query.

--Jimfive
Function htmlUnFormat(s As String) As String
Dim done As Boolean
Dim LocStart, LocEnd As Integer
Dim char As String
 
done = False
While Not done
    LocStart = InStr(1, s, "&#")
    If LocStart = 0 Then
        done = True
    Else
        LocEnd = InStr(LocStart, s, ";")
        If LocEnd > LocStart Then
            char = Mid(s, LocStart + 2, LocEnd - LocStart - 2)
            If IsNumeric(char) Then
                s = Replace(s, Mid$(s, LocStart, LocEnd - LocStart + 1), Chr(CInt(char)))
            End If
        Else
            done = True
        End If
    End If
Wend
htmlUnFormat = s
End Function

Open in new window

JimFive i get invalid procedure call or argument on this line
s = Replace(s, Mid$(s, LocStart, LocEnd - LocStart + 1), Chr(CInt(char)))
Hmm...Try Mid instead of Mid$
--
JimFive
Your problem is very simple and can be solved by the following code.

Follow the following instructions:
1. Create a New Module in your Database.
2. Copy Paste the following code into that newly created Module that I have written just for your custom need
3. Press F5 and then Press Enter.
4. Enter Table Name to FIX, i.e. to replace ALL &#code; patterns with their character equivalents

I have tested it on your Uploaded Table and found it fully functionaly and bugs free, so Enjoy!

Option Compare Database
 
Function ConvertASCII(s As String) As String
    Dim str As String, st As Integer, prevst As Integer, i As Integer
    str = ""
    st = 1
    i = 0
    Do
        i = InStr(st, s, "&#")
        If i <> 0 Then
            If i <> 1 Then
                str = str + Mid(s, st, i - st)
            End If
            st = InStr(i, s, ";") + 1
            str = str + ChrW(Int(Mid(s, i + 2, st - 1 - i - 2)))
        Else
            str = str + Mid(s, st)
        End If
    Loop While i <> 0
    
    ConvertASCII = str
End Function
 
Sub FixTable()
    Dim r As Recordset
    Dim TableName As String
    
    TableName = InputBox("Enter Table Name to Fix ASCIIs", "Table Name", "tmp")
    'On Error GoTo Err
    Set rs = CurrentDb.OpenRecordset("Select * From " & TableName)
    Do While Not rs.EOF
        rs.Edit
        
        For i = 0 To rs.Fields.Count - 1
            rs.Fields(i) = ConvertASCII(rs.Fields(i))
        Next
        
        rs.Update
        rs.MoveNext
    Loop
    rs.Close
    
    MsgBox "Done!"
    
    Exit Sub
Err:
    If Err.Number = 3078 Then
        MsgBox "The table name is invalid. No such table in the current database.", vbCritical, "Table not exist"
    Else
        MsgBox "Error: " & Err.Description
    End If
End Sub

Open in new window

That didnt work either, Its throwing the error on this value &#8482;
I believe 8482 is in the extended ASCII set ...


"The ChrW function returns a String containing the Unicode character except on platforms where Unicode is not supported, in which case, the behavior is identical to the Chr function"


mx
TheGD that works great, except if it comes across a null field then I get invalid use of null error
As DatabaseMX suggested.  Change the Chr to ChrW
--
JimFive
ASKER CERTIFIED SOLUTION
Avatar of JimFive
JimFive
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Guys