Jamie Smith
Jamie Smith

Reputation: 33

MS- Access VBA Converting multiple characters to Asc

For a homework project I am trying to enter characters in a single textbox as (eg:"AbC" no spaces) and have the output in a captioned label as the corresponding ASCII value written out with commas and spaces. (eg: 65, 98, 67)

Private Sub cmdCode_Click()
Dim codeInt As Integer
strInput = txtInput.value
codeInt = Asc(strInput)
lblAnswer.Caption = codeInt & ", "
End Sub

I would like the result to look like: 65, 98, 67

I'm getting no errors but only receiving "65," as my output.

Upvotes: 0

Views: 663

Answers (2)

Gustav
Gustav

Reputation: 55831

This can be done for generic usage - and a little smarter:

Public Function StrToAscList( _
    ByVal Text As String) _
    As String

    Dim Chars() As Byte
    Dim Item    As Integer
    Dim List    As String

    Chars() = StrConv(Text, vbFromUnicode)
    For Item = LBound(Chars) To UBound(Chars)
        If Item > 0 Then List = List & ", "
        List = List & CStr(Chars(Item))
    Next

    StrToAscList = List

End Function

Then:

Me!lblAnswer.Caption = StrToAscList(strInput)

Upvotes: 1

Jamie Smith
Jamie Smith

Reputation: 33

Here is my solution. It assumes that the input is always going to be three (3) characters long:

Private Sub cmdCode_Click()
Dim x As String
Dim y As String
Dim z As String
strInput = txtInput.value
x = Asc(Left(strInput, 1))
y = Asc(Mid(strInput, 2, 1))
z = Asc(Right(strInput, 1))
lblAnswer.Caption = x & ", " & y & ", " & z
End Sub

Upvotes: 2

Related Questions