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

How to create a table for ASCII codes

Status
Not open for further replies.

Melagan

MIS
Nov 24, 2004
443
0
0
US
Greetings,

I was working on a function today and after having got it to work, thought I'd share it with tek-tips. This function will create a table that will show ASCII codes and their respective characters.
Code:
Public Function CreateASCIITable()
Dim i As Integer
Dim rst As Recordset
Dim dbs As Database
Dim strASCII As String
Dim intMsgRtn As Integer

Set dbs = CurrentDb
On Error Resume Next        'Trap error if table doesn't exist
Set rst = dbs.OpenRecordset("tblASCII")

    Select Case Err
        Case 0
            On Error GoTo 0
            intMsgRtn = MsgBox("tblASCII already exists. " _
                & "Do you want to delete and rebuild all rows?", 52)
                If intMsgRtn = 6 Then
                    dbs.Execute "DELETE * FROM tblASCII", dbFailOnError
                Else
                 rst.Close
                  Exit Function
                End If
        Case 3011, 3078         'Couldn't find table, so build it.
            Set myASCIItbl = dbs.CreateTableDef("tblASCII")
             Set fldMyASCIIField = myASCIItbl.CreateField("ASCII", dbLong)
              Set fldMyChrField = myASCIItbl.CreateField("Character", dbText)
               myASCIItbl.Fields.Append fldMyASCIIField
               myASCIItbl.Fields.Append fldMyChrField
               dbs.TableDefs.Append myASCIItbl
            Set rst = dbs.OpenRecordset("tblASCII")
        Case Else
            MsgBox "Unknown error in CreateASCIITable", vbCritical
        End Select
    
    For i = 32 To 126
     strASCII = Chr(i)
        With rst
        .AddNew
        !ASCII = i
        !Character = strASCII
        .Update
        End With
    Next
    
    DoCmd.SelectObject acTable, "tblASCII", True
    
End Function


~Melagan
______
"It's never too late to become what you might have been.
 
really??????????????????????




MichaelRed


 
Michael,

I'm not sure what you mean by your response here.. [ponder]

~Melagan
______
"It's never too late to become what you might have been.
 
You didn't like the ASCII table in online help? :)
 
Bah...the product isn't the point. It took me a while to learn how to do the coding involved. I figured if I struggled a little bit, then someone else might be as well. I've learned a lot just by browsing through threads here and like to give back.

Aparantly, I need to apologize for wasting space [3eyes]

~Melagan
______
"It's never too late to become what you might have been.
 
Melagan,
Don't apologize. Good code with some good learning points. I would also recommend this link which has a lot of good ASCII stuff in it.


There are a lot of links to other resources. Couple suggestions on the code.

1. Always use Option Explicit. I noticed that you have some variables not defined. EX:
Dim myASCIItbl As dao.TableDef
Dim fldMyASCIIField As dao.Field
Dim fldMyChrField As dao.Field

2. When working with DAO or ADODB, ALWAYS explicitly identify which. This is one of the biggest questions asked on this site, and will save a lot of headached. EX:

Dim rst As dao.Recordset
Dim dbs As dao.Database
Dim myASCIItbl As dao.TableDef
Dim fldMyASCIIField As dao.Field
Dim fldMyChrField As dao.Field

Also do the same with any named object that could have dual meaning. For example you may be working with Word from Access. "Page" and "Pages" exist in both Word and Access and mean very different objects.




3. I would modify your code to end at 255 not 136 to show the upper characters. Never know when someone wants an umlaut over their A.

4. Personally, if the procedure does not return anything then I make it a Sub and not a function. When I see function I am looking for a value to be returned. In some languages every procedure is a function. So Depending on the languages that you learn on, some people make everything a function.
 
Cool deal man, thank you for the tips!

~Melagan
______
"It's never too late to become what you might have been.
 
Of course in SQL 2005 it's much easier

SELECT number AS ASCII, char(number) AS Character
INTO tblASCII
FROM master..spt_values
WHERE (type = 'P') AND (number <= 255)

;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top