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?
James CoatsComputer Info. Sys. StudentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Mid(strInput, 2, 1)  says get a string that is one character long starting at position 2 of strinput.
0
Martin LissOlder than dirtCommented:
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

0
James CoatsComputer Info. Sys. StudentAuthor Commented:
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?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Martin LissOlder than dirtCommented:
Mid(strInput, 2, 1)  says get a string that is one character long starting at position 2 of strinput.

The Mid() function has 3 parts:
Part 1: Required - The source of the data
Part 2: Required - The starting position of characters you want to return
Part 3: Optional - The number of characters to want to return. If omitted all the characters starting at Part 2's starting position are returned.

For example if strInput is "Experts Exchange"
Msgbox mid(strinput, 3, 4) would return "pert" because the 3rd character is p and we asked for 4 characters, while Msgbox mid(strinput, 3) would return "perts Exchange" because the 3rd character is p and we asked for everything starting at positon 3.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James CoatsComputer Info. Sys. StudentAuthor Commented:
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.
0
James CoatsComputer Info. Sys. StudentAuthor Commented:
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
0
Martin LissOlder than dirtCommented:
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.
0
Dale FyeCommented:
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

0
James CoatsComputer Info. Sys. StudentAuthor Commented:
Dale,

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

Sam
0
Martin LissOlder than dirtCommented:
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.
0
Martin LissOlder than dirtCommented:
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
0
Dale FyeCommented:
@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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.