Search code examples
vbams-access-2007

Access 2007 VBA - System.Text.Encoding.ASCII


Using Access 2007 VBA, I am trying to create an instance of the ASCII object (similar to usage in VB.NET) so I can convert string to byte array and vice-versa. I tried this by doing:

Dim ASCII As Object
Set ASCII = CreateObject("System.Text.Encoding.ASCII")

But, Access gives me an error saying:

Run-time error '429':

ActiveX component can't create object

Not sure why this is. Is this a no-no in VBA (versus VB.net)? Can someone explain how I could do this, or if I can't WHY?


Solution

  • The correct ProgId is System.Text.ASCIIEncoding :

    Dim encoding As Object
    Set encoding = CreateObject("System.Text.ASCIIEncoding")
    
    Dim bytes() As Byte
    bytes = encoding.GetBytes_4("abcde")
    

    You could also get the ASCII bytes with StrConv:

    Dim bytes() As Byte
    bytes = VBA.StrConv("abcde", vbFromUnicode)