By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,118 Members | 1,134 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Simple way to read Login information from text file

P: 16
This is my first tutorial, so if there are any mistakes please forgive me =).

This will show you a very simple way to read your Login information from an outside text file.

What you need:
1. Add a new "Login Dialog" form to your project.
2. Completely erase the code inside of it, but keep the interface.
3. Create a text file listing your usernames and passwords one line after the other.

Your text file should look like this (without the numbers in front of the names):

Expand|Select|Wrap|Line Numbers
  1. Username1
  2. Password1
  3. Username2
  4. Password2
  5. Username3
  6. Password3
Etc...

Now, paste this code into your Login form.
Expand|Select|Wrap|Line Numbers
  1. Dim nFileNum As Integer, UInfo(999, 2) As String, sNextLine As String, lLineCount As Long, NumLine As Integer, LCount As Integer, UnPw As Integer, UnCheck As Boolean, PwCheck As Boolean, UserNum As Integer
  2.  
  3. Private Sub Form_Load()
  4. UnCheck = False
  5. PwCheck = False
  6.  
  7. nFileNum = FreeFile
  8.  
  9. Open "C:\Documents and Settings\Matt\My Documents\testing.txt" For Input As nFileNum
  10. lLineCount = 1
  11.  
  12. NumLine = 0
  13. LCount = 1
  14. UnPw = 0
  15.  
  16. Do While Not EOF(nFileNum)
  17.    Line Input #nFileNum, sText
  18.    LCount = LCount + 1
  19.    If LCount Mod 2 <> 0 Then UnPw = 2
  20.    If LCount Mod 2 = 0 Then
  21.    NumLine = NumLine + 1
  22.    UnPw = 1
  23.    End If
  24.   UInfo(NumLine, UnPw) = sText
  25. Loop
  26.  
  27. End Sub
  28. Private Sub cmdCancel_Click()
  29.     Me.Hide
  30.     End
  31. End Sub
  32.  
  33. Private Sub cmdOK_Click()
  34.     'check for correct password
  35.     For x = 1 To LCount
  36.         If txtUserName = UInfo(x, 1) Then
  37.         UnCheck = True
  38.         UserNum = x
  39.         End If
  40.     Next x
  41.  
  42.     If txtPassword = UInfo(UserNum, 2) Then PwCheck = True
  43.  
  44.     If txtUserName = "" Then
  45.       MsgBox "Invalid Username or Password, try again!", , "Login"
  46.         txtPassword = ""
  47.         txtPassword.SetFocus
  48.         SendKeys "{Home}+{End}"
  49.     ElseIf txtPassword = "" Then
  50.        MsgBox "Invalid Username or Password, try again!", , "Login"
  51.         txtPassword = ""
  52.         txtPassword.SetFocus
  53.         SendKeys "{Home}+{End}"
  54.     ElseIf UnCheck And PwCheck = True Then
  55.         MAINFORM.Visible = True
  56.         Me.Enabled = False
  57.         Me.Hide
  58.     Else
  59.         MsgBox "Invalid Username or Password, try again!", , "Login"
  60.         txtPassword = ""
  61.         txtPassword.SetFocus
  62.         SendKeys "{Home}+{End}"
  63.     End If
  64.  
  65. End Sub
Now, you are almost done.

1. Go to line 9 and change the filepath to where your text file resides.
2. Go to line 55 and change "MAINFORM" to your form that you would like to show.

Now you SHOULD be done! The way the program is written, you may have up to 999 sets of usernames and passwords.

The only problem with this is that if somebody knows where your text file is, they have all your passwords! Luckily the program I am using this for just HAPPENS to be an encryption program, so I can simply encrypt the text file. Once I have finished this I will repost on how it is done.
Jul 9 '07 #1
Share this Article
Share on Google+
4 Comments


Expert 5K+
P: 8,434
Thanks for that, ldpfrog

When I have the time one of these weekends, I may edit the article, just for consistency with various others here - one of the benefits of being a moderator. :)

I'd like to point out though, in any such case, the most basic security measure would be to store the encrypted version of each password in your file, not the "clear" version. That way, reading the file will not tell people your passwords, unless they figure out how to decrypt them. And if they wanted to edit the file to allow themselves access, they would have to encrypt their password, your way, for it to work. Certainly not impregnable, but definitely tougher than just reading the file.
Jul 9 '07 #2

hariharanmca
100+
P: 1,977
can you able to explain this code then it'll be good to get the things clear?
Aug 4 '07 #3

P: 15
How to make it read the text file is the data is printed like this

"username1","password1"
"username2","password2"

etc.
Apr 2 '08 #4

Expert 5K+
P: 8,434
How to make it read the text file is the data is printed like this
"username1","password1"
"username2","password2"
I'd look into using the Input # statement, instead of Line Input #.
Apr 3 '08 #5