none
[Access 2k3 + VBA] How to reverse values in a textbox RRS feed

  • Question

  • Hi,

    I use this code to generate a password for my word document:

    ' set password
    strPassW = [Form_Form1]!PKID

    and to password the word document:

         ' Saving the Document with password set as PKID
             objApp.ActiveDocument.SaveAs FileName:=strNewFile, Password:=strPassW

    what I want to do is:

    IF PKID: 1234

    I want the password to be 4321

    How can I do that, please?

    Regards

    Monday, February 13, 2012 6:30 PM

Answers

  • Try to use the strReverse Function, which does exactly what you are after.

    So in your case it will be like this:

    ' set password
    strPassW = strReverse([Form_Form1]!PKID)

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    • Marked as answer by Admin-Dev Monday, February 13, 2012 7:50 PM
    Monday, February 13, 2012 7:16 PM
    Moderator

All replies

  • Try to use the strReverse Function, which does exactly what you are after.

    So in your case it will be like this:

    ' set password
    strPassW = strReverse([Form_Form1]!PKID)

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    • Marked as answer by Admin-Dev Monday, February 13, 2012 7:50 PM
    Monday, February 13, 2012 7:16 PM
    Moderator
  • That sounds like a VERY BAD (easy to guess / crack) password...but that said here's how you could reverse as string in VBA:

    Public Function ReverseString(ByVal value As String) As String
        Dim i As Long, j As Long, chars() As String
        ReDim chars(Len(value) - 1)
        j = Len(value) - 1
        For i = 1 To Len(value)
            chars(j) = Mid(value, i, 1)
            j = j - 1
        Next
        ReverseString = Join(chars, "")
    End Function

    A simpler but slower version (string concatenation by the VBA engine is very slow, but you won't notice a difference for short strings):

    Public Function ReverseString2(ByVal value As String) As String
        Dim i As Long
        For i = 1 To Len(value)
            ReverseString2 = Mid(value, i, 1) & ReverseString2
        Next
    End Function


    jmh

    Monday, February 13, 2012 7:23 PM
  • Mister Daniel as always^^
    Monday, February 13, 2012 7:51 PM
  • Hi jmh,

    you're right, but the distribution channel is safe and the password reversed method is just to have everyone working just his files

    risk level to breach is very low though

    Monday, February 13, 2012 7:55 PM