Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


String Characters

Posted on 2005-03-06
Medium Priority
Last Modified: 2010-05-02

I am trying to devise some code that will enable me to do some simple encoding and decoding.

I have a text box and when the user enters Characters, words or number's I would like to allow the user to click an encode button so that 'each' character is encoded (ASCII Code - 1) and is displayed in the text box as characters.

Then the user is able to hit another command button and Decode the characters back to there original state (Encoded ASCII + 1).

I am finidng it difficult to get each individual character and applying the Encode method on it, any help wold be greatly appreciated.

Thanks in advance,

Question by:Akkas
  • 2
LVL 52

Expert Comment

by:Carl Tawn
ID: 13471537
Try this:

   Create a form with one textbox (txtValue) and two command buttons (cmdEncode and cmdDecode), then add the following code to it:

Private Sub cmdEncode_Click()

    txtValue.Text = Encode(txtValue.Text, True)

End Sub

Private Sub cmdDecode_Click()

    txtValue.Text = Encode(txtValue.Text, False)

End Sub

Private Function Encode(ByVal pString As String, ByVal pEncodeFlag As Boolean) As String

    Dim sEncoded As String
    Dim iDir As Integer
    Dim i As Integer
    If pEncodeFlag = True Then
        iDir = 1
        iDir = -1
    End If
    For i = 1 To Len(pString)
        sEncoded = sEncoded & Chr(Asc(Mid(pString, i, 1)) + iDir)
    Next i
    Encode = sEncoded

End Function

Hope this helps.
LVL 52

Accepted Solution

Carl Tawn earned 500 total points
ID: 13471542
Ooops, just reread the question, change:

        If pEncodeFlag = True Then
            iDir = 1
            iDir = -1
        End If


        If pEncodeFlag = True Then
            iDir = -1
            iDir = 1
        End If

Author Comment

ID: 13471708
Quality!!!! You've done this before, no doubt!!!!

I came up with the code below whilst I was waiting for a repsonse,

Private Sub cmdEncode_Click()
Dim Encoded As Variant
 Word = txtValue.Text
'Add Characters to variable
 For Ascii = 1 To Len(Word)
    Encode = Asc(Mid(Word, Ascii, 1)) - 1
    Encoded = Encoded & Chr(Encode)
  Next Ascii
   txtValue.Text = Encoded
End If
End Sub

You have used a function which requires minimal code for each command button, Im new to VB6.

Thanks and its a great help!


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question