Swatto 10-24-2004, 01:33 PM hi again :D
ive had an idea:
i want to make a simple program that allows users to register a username and password {which is saved in a text file somewhere on the HD}, and then once registered they can login using their username\password into a new form which enables them to use the program.
would this be difficult to do?
and would the coding have to be complex?
thanks in advance :)
Ricardo Manuel 10-24-2004, 01:38 PM Its very simple to do what you want
You can use the Visual Basic file I/O functions to store the username and passwords and read the file.
When the user trys to enter the program, just search the text file line by line to see if there is a user with that name and password.
hope this helps. :)
Swatto 10-24-2004, 04:54 PM thanks ricardo ;)
ive made all of the forms, now i got to see if i can code the program :rolleyes:
MikeJ 10-24-2004, 04:56 PM VB includes a Login Form (go to Add New Form, and then select login form. Modify the code found here to open a file - .. - and then do the checking.)
Swatto 10-24-2004, 05:31 PM thanks MikeJ, ive tried, but failed:
i would like my program when it is first executed to create the text file in the system directory of the computer my program is run on, how do i do this :-\
ive read through the file I/O tutorial, and i cant understand most of it, e.g. about the mode and the filenumber :confused: also when i tried to open a text file, the program said it had opened the file but it wasnt displayed on the screen :confused:
MikeJ 10-24-2004, 06:15 PM First off, you will want to use the Dir function, like so:
Dim FF As Integer, tmpStr As String
FF = FreeFile()
If Dir("C:\Path\To\File.txt") = "" Then
'File does not exist. Create it.
MsgBox "The file does not exist. Creating now...", vbExclamation + vbOKOnly, "File Creation"
Open "C:\Path\To\File.txt" For Append As #FF
Print #FF, "my file's contents"
Close #FF
Else
'It does, let's read it
Open "C:\Path\To\File.txt" For Input As #FF
tmpStr = Input$(LOF(FF), FF)
Close #FF
End If
MsgBox tmpStr
See how that works? (It would help to run it twice for you to see the full effect.)
The file number is basically how VB refers to your file - it is how VB knows which file to work with (you assign it when you open the file) - just don't worry about it too much. The mode is how VB will open it. For Input means you are going to be reading data out of it; For Output means you will overwrite it; and For Append means you will be adding on to it. Don't worry about For Binary at this point.
Because the Windows system folder can be in different places on different machines (i.e. WinNT\System32 or Windows\System etc...) then you will need a routine to determine the Windows system folder....here it is:
Private Const MAX_PATH = 260
Private Const S_OK = 0
Private Const CSIDL_SYSTEM As Long = &H25
Private Declare Function SHGetFolderPath Lib "shfolder" _
Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, _
ByVal nFolder As Long, ByVal hToken As Long, _
ByVal dwFlags As Long, ByVal pszPath As String) As Long
Private Function GetSystemFolder() As String
Dim strPath As String
Dim lngReturn As Long
strPath = Space(MAX_PATH)
lngReturn = SHGetFolderPath(0, CSIDL_SYSTEM, 0, 0, strPath)
Select Case lngReturn
Case S_OK
strPath = Left$(strPath, InStr(1, strPath, Chr(0)) - 1)
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
End Select
GetSystemFolder = strPath
End Function
Couple of notes: if the returned value is an empty string then there was an error (this routine doesn't raise an error which would be a better thing to do). It also adds a trailing backslash ("\") character to the end of the returned folder, so your returned string will look something like "C:\Windows\System\"
tidy trax 10-24-2004, 07:45 PM You could also use the WScript.Shell object:
Private Function GetSystemFolder() As String
Dim WshShell As Object, Path As String
Set WshShell = CreateObject("WScript.Shell")
Path = WshShell.ExpandEnvironmentStrings("%windir%")
Set WshShell = Nothing
GetSystemFolder = Path & "\system"
End Function
Swatto 10-25-2004, 03:58 AM Dim FF As Integer, tmpStr As String
FF = FreeFile()
If Dir("C:\Path\To\File.txt") = "" Then
'File does not exist. Create it.
MsgBox "The file does not exist. Creating now...", vbExclamation + vbOKOnly, "File Creation"
Open "C:\Path\To\File.txt" For Append As #FF
Print #FF, "my file's contents"
Close #FF
Else
'It does, let's read it
Open "C:\Path\To\File.txt" For Input As #FF
tmpStr = Input$(LOF(FF), FF)
Close #FF
End If
MsgBox tmpStr
thanks all :D
MikeJ, the file does not create, it says file does not exist, im guessing you need to add some code after the message box in order to create it before it can be opened :confused:
another thing, what does the #FF and Input$ all mean? and the LOF?
thanks for your help so far MikeJ, and thanks for going to the effort to write the code out for me to use ;)
----------------------------------------------------------------------
The Things I Know How To Do in VB:
I understand my way around the code window and form window.
Create Forms ~ with different controls on etc.
Use Code ~ to change properties of controls at run-time, perform simple calculations, make decisions, or for looping.
maybe this is a bit too advanced for me, what do you suggest i learn next in VB? :-\
Swatto 10-25-2004, 07:51 AM anyone?
avinash 10-25-2004, 07:59 AM Does the path "C:\Path\To" exist in your system? The line
Open "C:\Path\To\File.txt" For Append As #FF
will create a file.
Swatto 10-25-2004, 08:04 AM i guess the Open command doesnt create the directories, only the file, i will see if it works now, thanks :)
Swatto 10-25-2004, 08:18 AM yep works now, i changed the line to: c:\file.txt
ive gotten the code to write into the file what the user puts in the text boxes, but how do i make it so that whatever is typed in is stored in blocks so that the program reads the text file line by line, like this:
User: Swatto
Password: 123
-----------------------
User: VB
Password: 321
etc, so that when a new entry is made it stores it like that in the text file and reads it block by block
thanks in advance for help ;)
avinash 10-25-2004, 08:20 AM Hi,
Print writes line by line into a file. So if you put two Print statements, they will write two lines.
To read a line from a file, use LineInput
Swatto 10-25-2004, 09:07 AM Hi,
Print writes line by line into a file. So if you put two Print statements, they will write two lines.
To read a line from a file, use LineInput
thanks avinash, ive got the program to write the text line by line, when the person logs in, i want the program to check the text file line by line to see if the username matches with the password.
Ive attached the project so you can see how it is all set up so far.
Im a newb at VB, i want to learn it properly but all i know how to do really is:
I understand my way around the code window and form window.
Create Forms ~ with different controls on etc.
Use Code ~ to change properties of controls at run-time, perform simple calculations, make decisions, or for looping.
==============================================
Ricardo Manuel 10-25-2004, 12:20 PM Later on the project when you are more confortable with vb , you can start look at file security to protect the password list, you must reconize that storing passwords in files using plain and readable text is very unsafe.
You can use encryption for example.
Swatto 10-25-2004, 05:03 PM I understand what you mean ricardo :)
Do you think im ready to take on this project based on what ive said in my above post?
or do you think i should take on something slightly easier?
avinash 10-25-2004, 10:19 PM Take up the project. But do it in stages. For example, if you are not very much familiar with how to provide file security, then whatever knowledge you have to provide security, have only that even if it is not that secure. Write that in a separate function. Latter, when you gain more knowledge, then change that function. Instead, you can just write a separate function to check for username and password. As of now, let the function always return True. Latter, write code in that function to check for security in a proper way.
But always keep modularity in mind. Make sure that all your functions do well-defined tasks.
ashutosh9910 10-25-2004, 10:58 PM You coud also consider using FileSystemObject for that purpose. It might be easier to create and maintain the file and store it to a location, access it and read or write anything to and from it.
Ashutosh
MikeJ 10-26-2004, 03:24 PM Let me state that using the FSO, when not absolutely not necessary, is a bad idea. It increases the number of resources to do the same thing as native VB functions. While it's easier, it's not good programming practice to take the easy way over the best way.
Swatto 10-26-2004, 05:20 PM The program now writes to the file and also reads from the file and displays the contents in a message box {thanks to MikeJ} but how do i get the program to match up wether the information entered into the text boxes matches up with the information in the file.
and how do i make sure the program reads the text file like this {reads the data in sections}:
line 1 is: Username which should be equal to txtuser.text
line 2 is: Password which should be equal to txtpassword.text
line 3 is: Username which should be equal to txtuser.text
line 4 is: Password which should be equal to txtpassword.text
ETC ETC
but i want it to only check each of the two lines until it finds the right match for both inputs.
i hope someone understands what i mean ;)
thanks for any help on this :D
MikeJ 10-26-2004, 06:03 PM Look at the Split statement. If you use the vbCrLf (line feed) as your delimiter, all the even number parts of the array will be usernames, and the odd ones will be passwords.
Swatto 10-27-2004, 05:58 PM sorry if this sounds really newbish, MikeJ
what is a split statement?
what is vbCrLf ~ line feed?
and what is a delimiter?
lol, im sorry but i dont have a clue, do most programmers look things like this up in books if they dont know how to code something?
Toveling 10-27-2004, 06:08 PM most of us use the search function, the code library, and the tutors corner to find answers, along with google.
the split statement does exactly what it sounds like, it splits a string into smaller parts (the parts are put into an array). the way it works is that every time is sees the character to split by it adds that to the array. example:
dim str as string, array() as string
str = "text1|text2|text3"
if you were to use
array = split$(str, "|")
if would return an array with the following items:
array(0) = "text1"
array(1) = "text2"
array(2) = "text3"
the delimeter, in this case, is the | character (pipe).
vbCrLf = new line in text
so if you were to read the entire file, then split it by its line feeds (vbCrLf), you would get
name
pass
name
pass
in an array and you could compare the string to that.
MikeJ 10-27-2004, 07:35 PM lol, im sorry but i dont have a clue, do most programmers look things like this up in books if they dont know how to code something?
Type it into your code window, and hit F1. (Or hit F1 and search for it...)
|