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

Reading in Login info from outside

P: 16
I am currently writing a very simple encryption program for my friends and myself. I'm still learning VB, but am very familiar with programming itself. This is all irrelevant though, because this technique could be applied to any program which requires logging in.

I would like to be able to make a file (just a basic text file or spreadsheet to start would be fine) with Usernames and Passwords listed which will be accepted into my program. I currently just use If statements for two or three users, but will eventually probably distribute this to more of my friends, in which it would be much easier to host some sort of file for the program to connect to and read from.

I understand how to use the Login information, I am just not familiar with reading in information with VB. Once that is working I'd also like a suggestion as to how I could make it so only the program (or some sort of password protection) could access the login information file.

If we get this far, I would also like to be able to host it (which I can do) and have my program read from it on the web, so I could just change the file from my computer and it would work with whoever is using the software.

Thanks alot!
Frog
Jul 1 '07 #1
Share this Question
Share on Google+
9 Replies


Expert 5K+
P: 8,434
If you have a look at the "Index of articles" at the top of the VB forum, there are links to a couple of samples showing how to read a text file in VB.

I don't know how you can restrict access to the file, but one simple precaution would be to store the passwords in their encrypted form. That way, if someone edits the file, unless they know your encryption method, the most they can do is to stop it working by messing up the stored details. If you encrypt the entire file contents, or at least include some sort of checksum, then your program could detect that it has been modified and refuse to run.
Jul 2 '07 #2

P: 16
I did see that but I don't understand how to use it. It essentially hands you a chunk of code and doesn't tell you how to use it

I have also been looking online and many are saying to use ADO, where you would use excel as opposed to a text file. I have been screwing around with this with no success =/.
Jul 2 '07 #3

danp129
Expert 100+
P: 321
I did see that but I don't understand how to use it. It essentially hands you a chunk of code and doesn't tell you how to use it

I have also been looking online and many are saying to use ADO, where you would use excel as opposed to a text file. I have been screwing around with this with no success =/.
Here's the article's code with the sub renamed (read makes more sense than dump) and an example to call it, and also a comment in the code. If you actually test the code with a file, it will be more apparent what's going on.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     ReadFile_V01 ("c:\test.txt")
  3. End Sub
  4.  
  5.  
  6. Public Sub ReadFile_V01(ByVal FileName As String)
  7.   Dim FileNo As Long
  8.   Dim LineNo As Long
  9.   Dim LineText As String
  10.  
  11.   FileNo = FreeFile ' Get next available file number.
  12.  
  13.   Open FileName For Input Access Read Shared As #FileNo
  14.   Do Until EOF(FileNo) ' Repeat until end of file...
  15.     Line Input #FileNo, LineText ' Read a line from the file.
  16.  
  17.  
  18.     'put your code here to deal with the current line of text
  19.     LineNo = LineNo + 1
  20.     Debug.Print Format(LineNo, "00000"); ": "; LineText
  21.  
  22.  
  23.  
  24.     DoEvents ' Allow Windows to handle other tasks.
  25.   Loop
  26.   Close #FileNo
  27. End Sub
  28.  
If you want the password file hosted on a website you can use something like this to download the file:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private Sub Command1_Click()
  3.     Dim sHTML
  4.     sHTML = RequestText("http://news.google.com/")
  5.  
  6.     msgbox sHTML
  7.  
  8. End Sub
  9.  
  10. Private Function RequestText(sURL, Optional sMethod = "POST")
  11.     'You may have caching issues using GET
  12.     Dim XMLHTTP
  13.     Set XMLHTTP = CreateObject("microsoft.XMLHTTP")
  14.     sMethod = UCase(sMethod)
  15.     XMLHTTP.Open sMethod, sURL, False
  16.     XMLHTTP.send (Null) '"x=x"
  17.     RequestText = XMLHTTP.responseText
  18.     Set XMLHTTP = Nothing
  19. End Function
Jul 2 '07 #4

Expert 5K+
P: 8,434
I did see that but I don't understand how to use it. It essentially hands you a chunk of code and doesn't tell you how to use it

I have also been looking online and many are saying to use ADO, where you would use excel as opposed to a text file. I have been screwing around with this with no success =/.
Hm... ADO and Excel... interesting. I haven't seen it done, but that doesn't prove anything I suppose. I guess both Excel and Access have the advantage of supporting some security measures.

As for the samples, yes they really are just code snippets for people to play with. We don't have anything much in the way of tutorials as yet. There are heaps of VB tutorials available on the web, though. However, let's assume you had a text file with a bunch of user IDs, each one followed on the next line by an encrypted password. Here's a sample I threw together which will prompt you for the name of the file then invoke a little "black box" function to return an array of user IDs and encrypted passwords from it.

This is the complete code module for the function...
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Type UserStuff
  4.   ID As String
  5.   Psw As String
  6. End Type
  7.  
  8. Public Function GetUserDetailsFromFile(ByVal FileName As String) As UserStuff()
  9.   Dim FileNo As Long
  10.   'Dim LineNo As Long
  11.   Dim LineText As String
  12.   Dim TempUser() As UserStuff
  13.   Dim UserNo As Long
  14.   'Dim TempPsw() As String
  15.  
  16.   FileNo = FreeFile ' Get next available file number.
  17.  
  18.   Open FileName For Input Access Read Shared As #FileNo
  19.   Do Until EOF(FileNo) ' Repeat until end of file...
  20.     UserNo = UserNo + 1
  21.     ReDim Preserve TempUser(1 To UserNo)
  22.     Line Input #FileNo, TempUser(UserNo).ID
  23.     Line Input #FileNo, TempUser(UserNo).Psw
  24.     DoEvents ' Allow Windows to handle other tasks.
  25.   Loop
  26.   Close #FileNo
  27.   GetUserDetailsFromFile = TempUser
  28. End Function
And here's the code in my sample form to test it out...
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim MyUserDetails() As UserStuff
  3.  
  4. Private Sub Form_DblClick()
  5.   MyUserDetails = GetUserDetailsFromFile(InputBox("Enter name (including path) of file with users"))
  6.   Dim I As Long
  7.   For I = LBound(MyUserDetails) To UBound(MyUserDetails)
  8.     With MyUserDetails(I)
  9.       Debug.Print "UserID:"; .ID, "Psw:"; .Psw
  10.     End With
  11.   Next
  12. End Sub
Just run it, double-click the form and away you go. It just prints the results to the immediate window.

My sample file was just a text file I created in Notepad, containing these stupid values...
User01
%JFK ASEF:F$$W
User02
45v-98n3pytp4
User03
$%Y$%$K%JFK$$W
Jul 2 '07 #5

P: 16
Okay, thank you all alot for your help. This was the code that I came up with. I Created an array UInfo(999, 2). The X portion of this array is the maximum amount of usernames and passwords I could set, I could increase the amount but I doubt I'll have over 1000 users using this program. The Y portion is set up so if Y=1 then you are looking at a username, and if Y=2 then it is the password for the corresponding username.

In order for this to work, just create a multiline Textbox and a Command button, with the default VB6 names. I would be happy to explain to anybody who needs it because this is a very simple solution to having multiple usernames and passwords to your program without editing the actual code.


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
  2.  
  3. Private Sub Command1_Click()
  4.  
  5. nFileNum = FreeFile
  6.  
  7.  
  8. Open "C:\Documents and Settings\Matt\My Documents\testing.txt" For Input As nFileNum
  9. lLineCount = 1
  10.  
  11. NumLine = 0
  12. LCount = 1
  13. UnPw = 0
  14. Do While Not EOF(nFileNum)
  15.    Line Input #nFileNum, sText
  16.    LCount = LCount + 1
  17.    If LCount Mod 2 <> 0 Then UnPw = 2
  18.    If LCount Mod 2 = 0 Then
  19.    NumLine = NumLine + 1
  20.    UnPw = 1
  21.    End If
  22.   UInfo(NumLine, UnPw) = sText
  23.  
  24. Loop
  25. UInfo(0, 1) = "UN"
  26. UInfo(0, 2) = "PW"
  27. For x = 0 To LCount
  28. For y = 1 To 2
  29. Text1.Text = Text1 & UInfo(x, y) + " "
  30. Next y
  31. Text1.Text = Text1 & vbCrLf
  32. Next x
  33.  
  34. Close nFileNum
  35.  
  36. End Sub
Jul 8 '07 #6

Expert 5K+
P: 8,434
Glad to see you got it working.

Just one piece of general advice I would like to pass on. When you have two mutually exclusive possbilities (such as in lines 17-18 of your code) you should normally code it with an Else clause, as it makes the intention much more obvious. For instance...
Expand|Select|Wrap|Line Numbers
  1. If LCount Mod 2 <> 0 Then
  2.   UnPw = 2
  3. Else
  4.   NumLine = NumLine + 1
  5.   UnPw = 1
  6. End If
Jul 9 '07 #7

P: 16
I know but I learned programming in TrueBasic and am not used to using 0 in arrays so I was afraid it would screw things up (which it did anyway). But thank you!
Jul 9 '07 #8

Expert 5K+
P: 8,434
I know but I learned programming in TrueBasic and am not used to using 0 in arrays so I was afraid it would screw things up (which it did anyway). But thank you!
I never use entry 0 in an array either. I hate zero-based arrays - they go completely against the way humans think, and try to force them to think like computers. They should be banned.

All I was talking about in this case is the structure of the IF tests. You are doing the equivalent of...
Expand|Select|Wrap|Line Numbers
  1. IF Condition Then
  2.   ' Blah blah
  3. End If
  4. If Not Condition Then
  5.   ' Blah blah
  6. End If
In such a case, it's usually simpler to read if you code it as
Expand|Select|Wrap|Line Numbers
  1. IF condition Then
  2.   ' Blah blah
  3. Else
  4.   ' Blah blah
  5. End If
This is largely a personal choice, of course.
Jul 9 '07 #9

P: 16
Understood, thank you! Check out my other post about reading the file from the internet?
Jul 9 '07 #10

Post your reply

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