Problem in using the Mid function

specsol

Registered User.
Local time
Today, 20:13
Joined
Feb 11, 2008
Messages
18
In a routine I wrote to encrypt a login password for a database I have come across an oddity. For each character in the entered password I compare it in turn to each of the values of the chr function to obtain the index of that character in the Ascii table so it can be used to construct the encrypted value. If the character is lower case Mid(password, i,1) is seen as equal to both the upper and lower case versions of that character. Since I loop through the Ascii table from 0 to 127, the character is always encoded as if it was upper case. Does anyone know why this is and how to bypass it? The relevant code section in my routine is

Do While counta > 0
enc_idx = enc_idx + 1
For enc_idy = 0 To 127
If Mid(passwd, enc_idx, 1) = Chr(enc_idy) Then
Exit For
End If
Next

regardless of whether the character is lower case the value of enc_idy is that of the corresponding upper case character.

Any help appreciated
 
i think there is a string option to compare using case, rather than the default - not sure exactly what without checking, though.

i don;t quite understand what you are doing. given a char you can directly retrive the ascii value

asciivalue = asc(somestring)

or assign a string to an ascii char

somestring = chr(asciivalue)
 
You are of course correct in that the Asc function does return the index corresponding to the case of the character. I could alter the code to recognise the passwords as different if the case is different by using it. It just struck me as strange that the Mid function does correctly recognise the case of the character returned but the direct comparison with the Chr function does not return the correct True/False value. To my simple mind my code should have worked. Both the Mid function and the Chr function return the same string when I put the correct value of the index into Chr so I expected the IF condition to be true only when I get to the correct index

Thank you for the information!
 
gemma-the-husky is referring to Option Compare Binary, in your module you have it as Option Compare Database as default. The Binary option will perform a case sensitive comparison but note that this case sensitive comparison will affect your entire form module when you perform normal comparisons.

If you don't want to affect your entire form module you can use the Instr() function and use vbBinaryCompare in the fourth parameter.

So as you can see, there's nothing odd happening. Just the default setting.
 
Understood. As I said, simple minded

Many thanks
 

Users who are viewing this thread

Back
Top Bottom