Click to See Complete Forum and Search --> : Ahhh... creating a text file...


I am Fubar
May 28th, 2001, 04:24 AM
<newbieness mode>




Hey everyone...
How do I create a text file for a username and password to be stored in (its for a Maths program... doesn't need to be secure)

Thanks,

-Fubar


</newbieness mode>




I wanted to be a procrastinator, but never got round to it.

<i> Remove NOSPAM_ to email me </i>

cksiow
May 28th, 2001, 04:42 AM
refer http://vblib.virtualave.net, there is a function that write text string array into text file called WriteFileText in vbFileIO as the activeX DLL.

so, in order to store username and password, do this

dim data(0) as string
dim vbFileIO as new vbFileIO

data(0) = "username" & " " & "password

vbFileIO.WriteFileText(filename,data)...

HTH

I am Fubar
May 28th, 2001, 04:54 AM
Ok, before hacking me into tiny pieces (usually what happens to newbies) where do I put this zip file???

Thanks again,

-Fubar

I wanted to be a procrastinator, but never got round to it.

<i> Remove NOSPAM_ to email me </i>

shree
May 28th, 2001, 05:05 AM
You can use this quick solution for storing your names and passwords in a textfile.

Open App.Path & "pass.txt" for append as #1
print #1, txtUser.Text
print #1, txtPass.text
Close #1

Cimperiali
May 28th, 2001, 05:08 AM
Here another way to do it. Put twotextboxes (first for name, second for password) and a commandbutton on a form. Copy and paste. (except the contents of mypass.txt: it is there only to provide an example).
You may find .ini file or random file a better solution

contents of mypass.txt file:

gigi
a
bruno
b
pino
c

Option Explicit

Private Sub Command1_Click()
Dim freef As Integer
freef = FreeFile
Dim vntInput As Variant
Dim blnFound As Boolean



blnFound = False 'should be its default value
'but initialize to it for clarity and security
'serach for file of password
If Dir("c:\mypass.txt") = "" Then
'file does not exist. Create it
Open "c:\mypass.txt" For Output As #freef
Close #freef
End If
'in any case, now you have the file.
'open it to search for username and password
'do you want it case sensitive or not?
'
Open "c:\mypass.txt" For Input As #freef
Do While EOF(freef) = False
'first line the name
Line Input #freef, vntInput
'see if it matches with text1.text
If Text1.Text = vntInput Then
'look if password is all right
Line Input #freef, vntInput
If Text2.Text = vntInput Then
'password ok
MsgBox "You are a registered user"
blnFound = True
Exit Do
Else
MsgBox "You entered wrong password"
blnFound = True
Exit Do
End If
Else
'name do not match, skip one line
'(no need to lok for password)
Line Input #freef, vntInput
End If
Loop
Close #freef
If blnFound = False Then
MsgBox "User name not found"
End If
End Sub

Private Sub Form_Load()
Text2.PasswordChar = "*"
End Sub


Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.

Cimperiali
May 28th, 2001, 05:10 AM
...as always, I was in a hurry, and wrote down code to... read from file...
It will work nicely with shree solution.

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.

I am Fubar
May 28th, 2001, 05:31 AM
ok, that kinda works but what i need is a seperate text file for each student... and it creates an "MathsTutitionbob" (when Username = bob)

plus, it stores it in a directory below the one i want... grrr... :)

But thanks alot!

I wanted to be a procrastinator, but never got round to it.

<i> Remove NOSPAM_ to email me </i>

shree
May 28th, 2001, 05:38 AM
dim sFileName as string
dim sFilePath as string
sFileName = "MathsTuition" & txtUser.Text
sFilePath = App.Path & "\yourDir\"
Open sFilePath & sFileName for Output as #1
print #1, txtUser.Text
print #1, txtPass.text
Close #1

shree
May 28th, 2001, 06:03 AM
I think this is closer to what you wanted. Missed the \ earlier.

Open App.Path & "\" & txtUser.Text & ".txt" for Output as #1
print #1, txtUser.Text
print #1, txtPass.text
Close #1

I am Fubar
May 28th, 2001, 06:12 AM
Thanks Shree, Thanks a lot, it works!!!

I wanted to be a procrastinator, but never got round to it.

<i> Remove NOSPAM_ to email me </i>

Cimperiali
May 28th, 2001, 06:34 AM
'you can hard code the path
dim yourPath as string
yourpath = "c:\yourdir\yoursubdir\"
'you should chek for its existance:
if dir(yourpath,vbdirectory) <> "" then
Open yourpath.Path & "pass.txt" for append as #1
print #1, txtUser.Text
print #1, txtPass.text
Close #1
end if
'and if you want a different txt file for each student
'(you sure you want?)
you can do as follow:
Option Explicit
Dim yourpath As String

Private Sub Command1_Click()
'use username to write a file
yourpath = "c:"
Open yourpath.Path & "\" & Text1.Text For Output As #1
'if it does not exist= created.
'if it exists= replaced
'store only password
Print #1, txtPass.Text
Close #1

End Sub

Private Sub Command2_Click()
Dim vntInput As String
'to retrieve:
yourpath = "c:"

If Dir(yourpath & "\" & Text1.Text) <> "" Then
Open yourpath.Path & "pass.txt" For Input As #1
Line Input #1, vntInput
Close #1
'in vbinput you have passowrd
If Text2.Text <> vbinput Then
MsgBox "Wrong Password"
End If
Else
MsgBox "user not registered. Create the file, before."
End If

End Sub





Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.

cksiow
May 28th, 2001, 08:48 AM
download the source code and unzip to anyway. look at the function that you are interested in.