Link to home
Start Free TrialLog in
Avatar of James Coats
James CoatsFlag for United States of America

asked on

Access VBA Code problem

AccessDB.accdbI have some code that works but there is a section of it that I don't quite understand. I have commented what I don't understand. In this string example you are only to put three characters in the text box.


Private Sub cmdCode_click()

Dim strInput As String, strA As String, strB As String, strC As String, strD As String, int1 As Integer, int2 As Integer

    strInput = txtInput.Value
    strA = Left(strInput, 1) 'Get first character of the string to the left
        int1 = Len(strInput) 'measure the length of the string
        int2 = int1 - 1 'subtract 1 from int1 which leaves you with 2 characters? Is this correct?
    strB = Mid(strInput, 2, 1) 'I don't know what is going on here. Is this saying select first character from the left?
    strC = Right(strInput, 1) 'get last character on string to the right
    strD = Asc(strA) & "," & Asc(strB) & "," & Asc(strC) 'assign ASCII values to characters
lblOutPut.Caption = strD 'display output

End Sub


Is there a better way / shorter way to write this code?
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Mid(strInput, 2, 1)  says get a string that is one character long starting at position 2 of strinput.
Another way to do it would be the following, but it's probably harder to understand.

Private Sub cmdCode_click()
lblOutPut.Caption = Asc(Left(txtInput.Value, 1)) & "," & Asc(Mid(txtInput.Value, 2, 1)) & "," & Asc(Right(txtInput.Value, 1))
End Sub

Open in new window

Avatar of James Coats

ASKER

In your rewrite if I understand the first section:

Asc(Left(txtInput.Value, 1))  'this says get the first left most character

Asc(Mid(txtInput.Value, 2, 1))  'this say get the middle character??

Asc(Right(txtInput.Value, 1)) ' this says get the last character to the right.

I really like this rewrite it's cleaner and actually easier to understand except that the "2, 1" thing is a little confusing can you walk me through that? Is it similar to what you explained in the first code?
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I used to having to "Dim" items as String, Integer, date or some other value. It's odd that in your rewrite you don't do this and it still works.

I guess you are just taking the value of what was put into the text box and in your output you assign the Asc to convert what was input into the text box into the ASCII value therefor you don't need to Dim anything.
Martin,

Thank that really cleared up things. I have a book but it doesn't explain things a well as you just did. I wish you wrote the book. Do you have any suggestions for a book for someone who has no coding experience in VBA
That's correct. My revision acts directly on what's in the textbox, so strInput isn't needed, and since we aren't creating any intermediate values the other variables aren't needed.
Only problem with this technique is that you have no guarantee that the user will enter 3 or only 3 characters.  you might try:

Dim intLoop as integer, strAsc as string
'Loops through the first 3 characters (or less if less characters were entered)
'Appends the ascii value of the 1st-3rd characters to strAsc
For intLoop = 1 to IIF(Len(strInput) < 3, len(strInput), 3)
    strAsc = strAsc & ", " & ASC(mid(strInput, intLoop, 1))
Next
'strips off the leading comma from strAsc and assigns result to the label
lblOutPut.Caption = Mid(strAsc, 2)

Open in new window

Dale,

Thank you for your input that was very helpful having another example for me to learn from.

Sam
Only problem with this technique is that you have no guarantee that the user will enter 3 or only 3 characters.
It actually works with any number of characters greater than or equal to 3.
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
@Marty,

My point with the For/Next loop was that your code (and that of the OP) assumes the correct number of letters were entered.  I try not to assume anything given my experience with users.