Code I have:
cell_val = CStr(Nz(fld.value, ""))
Dim iter As Long
For iter = 0 To Len(cell_val) - 1 Step 1
If Asc(Mid(cell_val, iter, 1)) > 127 Then
addlog "Export contains ascii character > 127"
End If
Next iter
This code doesn't work. Anyone know how to do this? I've simply got no idea with VB or VBA.
Program to loop on every character in string in C++ To loop on each character, we can use loops starting from 0 to (string length – 1). For accessing the character we can either use subscript operator "[ ]" or at() function of string object.
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
I believe your problem is that in VBA string indexes start at 1 and not at 0. Try the following:
For iter = 1 To Len(cell_val)
If Asc(Mid(cell_val, iter, 1)) > 127 Then
addlog "Export contains ascii character > 127"
End If
Next
With VBA, VB6 you can just declare a byte array and assign a string value to it and it will be converted for you. Then you can just iterate through it like a regular array.
e.g.
Dim b() as byte
Dim iter As Long
b = CStr(Nz(fld.value, ""))
For iter = 0 To UBound(b)
if b(iter) > 127 then
addlog "Export contains ascii character > 127"
end if
next
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With