By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,154 Members | 2,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,154 IT Pros & Developers. It's quick & easy.

How Do I run this code by dbl clicking a field in a form.

P: 8
I am trying to run this code in a form when a user double clicks in a particular field. I am unsure how I should do it since it uses 2 text files, one to generate and the other to store to eleminate redundancy. If you can help this newbie, thanks.
Expand|Select|Wrap|Line Numbers
  1. '==========================================================================
  2. '
  3. ' NAME: RandomPasswordGenerator.vbs
  4. '
  5. ' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
  6. ' URL: http://www.thespidersparlor.com
  7. ' DATE  : 7/29/2004
  8. ' Modified by Homer Simpson, 9/05/2007 for Wire Room Pin generation
  9. '
  10. ' COMMENT: Generates Random Passwords meeting "Complex" Requirements
  11. '          By default will generate a 4 digit password.
  12. '          Edit line passLen = 4 to change length
  13. '==========================================================================
  14. Option Explicit
  15. 'On Error Resume Next
  16.  
  17. Dim BankCode, newpass, passList, inFlag, pgLength, x, fso, ts, passLen, WshShell, passin 
  18. Const ForWriting = 2, ForAppending = 8, ForReading = 1
  19. passLen = 4
  20.  
  21. 'Give inFlag (input Flag) an initial value to ensure we run once
  22. inFlag = "invalid"
  23.  
  24. Do While inFlag = "invalid"
  25.     BankCode = InputBox("Enter a valid Bank Code")
  26.  
  27.     'Quit if no entry
  28.     If BankCode = "" Then WScript.Quit
  29.     BankCode=UCase(BankCode)    
  30.     'copy this line per bank. Put codes in upper case
  31.     If (BankCode = "BW") or (BankCode = "BWE") then inFlag = "valid"
  32.      If (BankCode = "CB") or (BankCode = "CBE") then inFlag = "valid"
  33.       If (BankCode = "FB") or (BankCode = "FBE") then inFlag = "valid"
  34.        If (BankCode = "MB") or (BankCode = "MBE") then inFlag = "valid"
  35.         If (BankCode = "MM") or (BankCode = "MME") then inFlag = "valid"
  36.          If (BankCode = "ON") or (BankCode = "ONE") then inFlag = "valid"
  37. Loop
  38.  
  39.  
  40. 'Generate the PIN
  41. inFlag= "invalid"
  42. Do While inFlag = "invalid"
  43.     set ts = nothing
  44.     set fso = nothing
  45.     Set fso = CreateObject("Scripting.FileSystemObject")
  46.     Set ts = fso.OpenTextFile ("PasswordList.txt", ForReading)
  47.     newpass = generatePassword(passLen)
  48.     newpass = BankCode + newpass
  49.  
  50.     ' validate password uniqueness
  51.  
  52.     inflag="valid"
  53.     Do While ts.AtEndOfStream <> true
  54.         passin=ts.ReadLine
  55.         if passin = newpass then inFlag="invalid"
  56.     Loop
  57. Loop
  58. set ts = nothing
  59. set fso = nothing
  60.  
  61. 'Now save it all to a text file.
  62. passList = newpass & vbCrLf
  63. Set fso = CreateObject("Scripting.FileSystemObject")
  64. Set ts = fso.CreateTextFile ("NewPin.txt", ForWriting)
  65. ts.write passList
  66.  
  67. set ts = nothing
  68. set fso = nothing
  69.  
  70. 'Open PIN in notepad
  71. set WshShell = WScript.CreateObject("WScript.Shell")
  72.  
  73. 'Launch Notepad from the command line 
  74. WshShell.Run "Notepad.exe NewPin.txt"
  75.  
  76. 'Write Password to permanent list
  77. Set fso = CreateObject("Scripting.FileSystemObject")
  78. Set ts = fso.OpenTextFile ("PasswordList.txt", ForAppending)
  79. ts.write passList
  80.  
  81. set ts = nothing
  82. set fso = nothing
  83.  
  84. 'end of program
  85.  
  86. Function generatePassword(PASSWORD_LENGTH)
  87.  
  88. Dim NUMLOWER, NUMUPPER 
  89. Dim newPassword, count, pwd
  90.  
  91.  NUMLOWER    = 48  ' 48 = 0
  92.  NUMUPPER    = 57  ' 57 = 9
  93.  
  94.  ' initialize the random number generator
  95.  Randomize()
  96.  
  97.  newPassword = ""
  98.  count = 0
  99.  DO UNTIL count = PASSWORD_LENGTH
  100.  
  101.        pwd = Int( ( NUMUPPER - NUMLOWER + 1 ) * Rnd + NUMLOWER )
  102.  
  103.   newPassword = newPassword + Chr( pwd )
  104.  
  105.   count = count + 1
  106.  
  107.  Loop
  108.  
  109. 'The password is good so return it
  110.  generatePassword = newPassword
  111. End Function
Sep 6 '07 #1
Share this Question
Share on Google+
1 Reply


Scott Price
Expert 100+
P: 1,384
It looks to me like you have two separate and independent functions doing the same thing here. Did you write these?

If you are only trying to generate a random password by double-clicking, you should be able to use the second function (starting with line 86 of your code), you'll need to pass it the length, but in the double click event of your text box, write this kind of code:

Expand|Select|Wrap|Line Numbers
  1. Call generatePassword(6)
This code will pass the password length of 6 characters.

The generatePassword function will, of course, go into a standard code module as a Public Function.

From what I can tell just looking at this, the first function you have is a highly specific password generator possibly for a banking institution?

Regards,
Scott
Sep 9 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.