PDA

View Full Version : vba form check against text file



puppy123
09-18-2011, 01:11 AM
I have 2 forms.
The first one have the button "create user" and "login"
the create user button will allow user to create a new user and export the username and password info into a txt file (I have done this).
The login button will open form2 which have 2 textboxes for entering username and password. The login info must be checked against the info I have saved in the txt file, but I'm not sure how to do it. Can someone help? This is what I have for form2:
Private Sub CommandButton1_Click()
Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
sFileName = "C:\Users\accounts.txt"
If Len(Dir$(sFileName)) = 0 Then
Exit Sub
End If

iFileNum = FreeFile()
Open sFileName For Input As iFileNum

Do While Not EOF(iFileNum)
Line Input #iFileNum, sBuf
If TextBox1.Text = sBuf AND TextBox2.Text = sBuf Then
MsgBox ("Logging in")
Form2.Hide
Form1.Show
Else: MsgBox ("Username or password incorrect")
Unload Me
Form2.Show
End If
Loop
Close iFileNum
End Sub

But when I enter the correct password and username, it always display "Username or password incorrect". So I wonder how can u compare the first textbox with the first line in txt file and second textbox with the second line in txt file

My txtfile have the following info I have saved from Form1:
1
test

1 is username
and test is password.

mikerickson
09-18-2011, 01:15 AM
Cross posted.
http://www.excelforum.com/excel-programming/792739-vba-form-textbox-to-compare-with-a-textfile.html#post2601570

Puppy123, welcome.
Please Read this link (http://www.excelguru.ca/node/7) and post a link whenever you cross-post to a different forum.

Thank you.