2 Replies - 2601 Views - Last Post: 21 October 2011 - 08:55 PM Rate Topic: -----

#1 diamondw  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 09-October 11

VB6 Code Cipher input box

Posted 15 October 2011 - 06:38 PM

Hey.Whats wrong with my code.I want it to shift a word.The inputs are text and shift amount and I want them to be in a Input Box.I want the output to be in a 20 column by n row array in excel.Only lower case letters and z would shift to b if the shift amount is 2.
Function ShiftCipher(text)
        shiftamount = InputBox("Input how many numbers to shift by")
        text = InputBox("Input text")
        tempText = LCase(x)
    
        Row = 1
        Column = 1
        Worksheets("Encrypt").Cells(Row, Column) = Encrypt(shift, i, 1)
        Column = Column + 1
        If Column > 20 Then
        Row = Row + 1
        Column = 1
        End If
        Next i
        
        Select Case letter
        Case "a"
                letter = Chr((0 + shiftamount) Mod (26) + 97)
        Case "b"
                letter = Chr((1 + shiftamount) Mod (26) + 97)
        Case "c"
                letter = Chr((2 + shiftamount) Mod (26) + 97)
        Case "d"
                letter = Chr((3 + shiftamount) Mod (26) + 97)
        Case "e"
                letter = Chr((4 + shiftamount) Mod (26) + 97)
        Case "f"
                letter = Chr((5 + shiftamount) Mod (26) + 97)
        Case "g"
                letter = Chr((6 + shiftamount) Mod (26) + 97)
        Case "h"
                letter = Chr((7 + shiftamount) Mod (26) + 97)
        Case "i"
                letter = Chr((8 + shiftamount) Mod (26) + 97)
        Case "j"
                letter = Chr((9 + shiftamount) Mod (26) + 97)
        Case "k"
                letter = Chr((10 + shiftamount) Mod (26) + 97)
        Case "l"
                letter = Chr((11 + shiftamount) Mod (26) + 97)
        Case "m"
                letter = Chr((12 + shiftamount) Mod (26) + 97)
        Case "n"
                letter = Chr((13 + shiftamount) Mod (26) + 97)
        Case "o"
                letter = Chr((14 + shiftamount) Mod (26) + 97)
        Case "p"
                 letter = Chr((15 + shiftamount) Mod (26) + 97)
        Case "q"
                 letter = Chr((16 + shiftamount) Mod (26) + 97)
        Case "r"
                 letter = Chr((17 + shiftamount) Mod (26) + 97)
        Case "s"
                 letter = Chr((18 + shiftamount) Mod (26) + 97)
        Case "t"
                 letter = Chr((19 + shiftamount) Mod (26) + 97)
        Case "u"
                 letter = Chr((20 + shiftamount) Mod (26) + 97)
        Case "v"
                 letter = Chr((21 + shiftamount) Mod (26) + 97)
        Case "w"
                 letter = Chr((22 + shiftamount) Mod (26) + 97)
        Case "x"
                 letter = Chr((23 + shiftamount) Mod (26) + 97)
        Case "y"
                 letter = Chr((24 + shiftamount) Mod (26) + 97)
        Case "z"
                letter = Chr((25 + shiftamount) Mod (26) + 97)
     End Function
        End Select


Is This A Good Question/Topic? 0
  • +

Replies To: VB6 Code Cipher input box

#2 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 836
  • Joined: 16-September 09

Re: VB6 Code Cipher input box

Posted 17 October 2011 - 03:15 PM

The code looks good....I would start by changing the names "Row" & "Column" to something that wouldn't be considered a built in function. Try "MyRow" & "MyColumn" or something the like...

If that doesn't help, post the error that your getting and the line you're getting it on and we can try to help further.

Also btw, you have next i on line 14 with out a for. Then you have text defined in the function name as a "byval" but aren't using it that way since you're assigning from an inputbox and finally you have LCase(x) but I don't see x used anywhere or temptext for that matter.

:)

This post has been edited by guyfromri: 17 October 2011 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: VB6 Code Cipher input box

Posted 21 October 2011 - 08:55 PM

If you're using text to store the InputBox input, then you shouldn't pass it in as an argument. In your code, whatever gets passed to your function gets overwritten when you call the InputBox function.

Second, you should use Sub rather than Function. Functions return a value to whatever code calls it (Subs don't), and you don't appear to be doing that. Here's an example of a function:
Function AddTwo(x as Integer) As Integer
AddTwo = x + 2
End Function

[somewhere else in your code]Dim q as Integer
q = AddTwo(7)
Debug.Print q 'will print 9 in the debug window

If you work through that so you understand it, you'll see why you don't need that functionality there. Unless I'm misunderstanding what you are up to, of course.

Finally, your entire select statement can be written with just one line of code, thus:
letter = Chr((Asc(letter) - 61 + shiftamount) Mod (26) + 97)

This is because the ASCII value for "a" is 61, and all the others add 1 to that value. (Google "ASCII table" and have a look; what I'm saying should be obvious.) You might want to handle upper case input, too, by forcing letter to lowercase: letter = LCase(letter).

Happy tinkering! :)

This post has been edited by BobRodes: 21 October 2011 - 08:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1