|
When strings don't behave like you expect, use VBA code to show each character, its ASCII value, and position in the string. These results are written to the Debug/Immediate window.
Press Ctrl-G to Go to DebuG window, and show results in the Immediate window
ASCII = American Standard Code for Information Interchange
The code loops through each character of a string. Each character is displayed on one line with added spacing. Under each character is its ASCII value, and then under that it its position in the string.
First, the text to evaluate is displayed and then a blank line. Then there are sets of 3 lines.
This example has several TAB characters that are throwing the displayed spacing off. Where are they? TAB = chr(9)
This output is formatted different than easier-to-write code to show the ASCII value of each character, which usually prints vertically maybe similar to this:
s 115 t 116 r 114 i 105 n 110 g 103
Vertical ouptut is ok for short information, but what if you're looking for odd characters in longer text, you might find this output easier to read.
string with no suspicious characters. Space = chr(32)
string with carriage return and line feed, chr(13) + chr(10)
Pass a string that you want to inspect to the WriteAscii_s4p procedure.
Optionally, you can set how wide the output will be and the spacing between each column of information under each character.
Optional Parameters
'*************** Code Start ***************************************************** ' Purpose : show each character, its ASCII value, and position in the string ' in the Debug (Immediate) window ' control spacing with Tab ' Author : crystal (strive4peace) ' Code List: www.msaccessgurus.com/code.htm ' This code: https://msaccessgurus.com/VBA/WhatisAscii.htm ' LICENSE : ' You may freely use and share this code, but not sell it. ' Keep attribution. Mark your changes. Use at your own risk. '------------------------------------------------------------------------------- Sub WriteAscii_s4p(pvString As Variant _ ,Optional piLastStartPosition As Integer = 80 _ ,Optional piTabSpace As Integer = 4 _ ) 'show each character, ASCII value and position ' in the Debug (Immediate) window '171106 strive4peace, 221118 'PARAMETERS ' pvString is the string to loop each character 'Optional ' piLastStartPosition is last position ' on line to start writing character ' piTabSpace is horizontal space between ' start of each character If IsMissing(pvString) _ Or IsNull(pvString) _ Then Exit Sub Dim i As Integer _ ,iPosition As Integer _ ,sCharacter As String * 1 _ ,sAsciiValues As String _ ,sCharacterNumbers As String _ ,sSpaceAfterASCII As String sSpaceAfterASCII = Space(piTabSpace - 3) 'show string Debug.Print String(piLastStartPosition, "=") Debug.Print pvString Debug.Print iPosition = 1 sAsciiValues = "" For i = 1 To Len(pvString) sCharacter = Mid(pvString,i,1) 'make sure start position isn't past desired end If iPosition >= piLastStartPosition Then If sAsciiValues <> "" Then Debug.Print 'end line of characters Debug.Print sAsciiValues 'ASCII values Debug.Print sCharacterNumbers 'character numbers Debug.Print 'blank line sAsciiValues = "" sCharacterNumbers = "" 'reset position iPosition = 1 End If End If Debug.Print Tab(iPosition); sCharacter; 'string with ASCII codes sAsciiValues = sAsciiValues _ & Format(Asc(sCharacter), "000") _ & sSpaceAfterASCII 'string with character numbers sCharacterNumbers = sCharacterNumbers _ & Format(i, "0") _ & Space(piTabSpace - Len(CStr(i))) 'increment position for next character iPosition = iPosition + piTabSpace 'next Next i If sAsciiValues <> "" Then 'iPosition<=1? Debug.Print 'end line Debug.Print sCharacterNumbers Debug.Print sAsciiValues End If End Sub '*************** Code End *******************************************************Code was generated with colors using the free Color Code add-in for Access.
Download text file with code: vba_WriteAscii__TXT.zip
If you have trouble with the download, you may need to unblock the ZIP file, aka remove Mark of the Web, before extracting the BAS file. Here are steps to do that: https://msaccessgurus.com/MOTW_Unblock.htm
Troubleshoot output character by character
If you like this page, please let me know, thank you. Donations are always appreciated
Here's the link for this page in case you want to copy it and share it with someone:
https://msaccessgurus.com/VBA/WhatisAscii.htm
or in old browsers:
http://www.msaccessgurus.com/VBA/WhatisAscii.htm
Let's connect and team-develop your application together. I teach you how to do it yourself. My goal is to empower you.
While we build something great together, I'll pull in code and features from my vast libraries as needed, cutting out lots of development time. I'll give you lots of links to good resources.
Do you want your application to more successful?
I help you one-on-one as we make your application better together.
Email me at training@msAccessGurus
~ crystal
the simplest way is best, but usually the hardest to see