Shoutbox

VB Help [Another problem] - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: VB Help [Another problem] (/showthread.php?tid=50449)

VB Help [Another problem] by Reaper on 09-13-2005 at 06:01 PM

im working on my A2 computing project, and i want to put a login dialog on my system.
so far ive used the template they have given me for the pag and the design.
i want to know how to search the file for the user name and password and where in the code i do all this

code:
Option Explicit

Public LoginSucceeded As Boolean

Private Sub cmdCancel_Click()
    'set the global var to false
    'to denote a failed login
    LoginSucceeded = False
    Me.Hide
End Sub

Private Sub cmdOK_Click()
    'check for correct password
    If txtPassword = "password" Then
        'place code to here to pass the
        'success to the calling sub
        'setting a global var is the easiest
        LoginSucceeded = True
        Me.Hide
    Else
        MsgBox "Invalid Password, try again!", , "Login"
        txtPassword.SetFocus
        SendKeys "{Home}+{End}"
    End If
End Sub


this is the code given by the VB app
RE: VB Help by matty on 09-13-2005 at 06:48 PM

code:
Dim strUsername, strPassword as string

Open "text1.text" For Input As #1
Do Until EOF(1)
     Input#1, strUsername, strPassword
     If strUsername = txtUsername And strPassword = txtPassword Then
          'Do stuff if password and username match
          Exit Do
     End If
Loop
Close#1

RE: VB Help by Reaper on 09-13-2005 at 06:52 PM

text1.text is the name of the file. do i put the whole path name or just the name of the file?

also what do i wrrite in the text file so it identifies the username and password. so bascially what do i wirte in the text file if the username is Guest and the password is qwerty


RE: VB Help by RaceProUK on 09-13-2005 at 09:14 PM

Most of the time you need the full name to the file. As for writing the username and password, use comma-separated values (or maybe the pipe | character). I also recommend hashing the password before storing it - you won't need MD5, a simple hash like the one MSN uses to encode e-mail addresses will suffice.


RE: VB Help by Reaper on 09-13-2005 at 10:00 PM

ok i got a few problems, when i type guest, qwerty in the file it doesnt login in.
when i put just guest in the file, i get the error: Run-time error "62"
Input past end of file

when i click debug, the line Input #1, strUsername, strPassword is highlighted. when i put my mouse over strUsername, it ses strUsername = "guest" but when i do it over strPassword it says strPassword""

also the pipe character doesnt work


RE: VB Help by brian on 09-13-2005 at 10:25 PM

How do you mean doesn't work, what you are supposed to do is:

make a file with "guest|qwerty" in it
open the file, loop through it, split each LINE with |.
each line will have a user/pass, so you choose the line/user you need and get its pass, then check.


RE: VB Help by Reaper on 09-13-2005 at 10:29 PM

i just wanna test it with the one login info, so i dont need to do the loop and split do i?

what i got in the file is:

guest|qwerty

nothing else

btw if u want the code i have got, it is:

code:
Option Explicit

Public LoginSucceeded As Boolean

Private Sub cmdCancel_Click()
    'set the global var to false
    'to denote a failed login
    LoginSucceeded = False
    Me.Hide
End Sub

Private Sub cmdOK_Click()
    'check for correct password
    If txtPassword = "password" Then
        'place code to here to pass the
        'success to the calling sub
        'setting a global var is the easiest
        LoginSucceeded = True
        frmMainMenu.Show
        Me.Hide
            Else
        MsgBox "Invalid Password, try again!", , "Login"
        txtPassword.SetFocus
        SendKeys "{Home}+{End}"
    End If
End Sub

Private Sub Form_Load()
Dim strUsername, strPassword As String

Open "C:\Documents and Settings\Kevish\My Documents\A2 Computing Coursework\login.txt" For Input As #1
Do Until EOF(1)
     Input #1, strUsername, strPassword
     If strUsername = txtUserName And strPassword = txtPassword Then
          'Do stuff if password and username match
          Exit Do
     End If
Loop
Close #1
End Sub


RE: VB Help by matty on 09-13-2005 at 11:08 PM

Heres an example for you.


RE: RE: VB Help by CookieRevised on 09-14-2005 at 06:33 AM

quote:
Originally posted by raceprouk
Most of the time you need the full name to the file.
Unless you execute the program from within the same folder as where the file is, you always need the give the full path.

quote:
Originally posted by raceprouk
As for writing the username and password, use comma-separated values (or maybe the pipe | character).
It depends on how you're going to read the file. If you use the "Input" command, then you must seperate the fields with a comma. If you use the "Line Input" command (which reads a whole line instead until the next delimeter), it doesn't matter what you use to seperate it as long as you use that same seperator in your code when you extract the parts you need from it (Matty's code) or use it to build up your comparisson string (my alternative code).

quote:
Originally posted by Reaper66613
ok i got a few problems, when i type guest, qwerty in the file it doesnt login in.
Not because you entered something wrong in the file or read it out wrongly (aka the reading routine). It is because you didn't handle it correctly higher-up in your code. See my notes below about your code...

quote:
Originally posted by Reaper66613
when i put just guest in the file, i get the error: Run-time error "62"
Input past end of file.
when i click debug, the line Input #1, strUsername, strPassword is highlighted. when i put my mouse over strUsername, it ses strUsername = "guest" but when i do it over strPassword it says strPassword""
Because there was 1 field missing in the file.

quote:
Originally posted by Reaper66613
also the pipe character doesnt work
That doesn't work when you use "Input #1, Variable1, Variable2". If you use that then you need 2 fields in the file. A pipe symbol doesn't qualify as a delimeter, only a comma does (when reading field by field).

If you wanna use a pipe symbol you need to read the whole line at once with "Line Input #1, Variable". In fact instead of a pipe symbol you can use whatever character you like (or none at all for that matter) as the line is interpreted as a whole, not as seperate fields...

quote:
Originally posted by brian
How do you mean doesn't work, what you are supposed to do is:
It didn't work because he is using "Input", and not "Line Input".

quote:
Originally posted by brian
make a file with "guest|qwerty" in it
open the file, loop through it, split each LINE with |.
each line will have a user/pass, so you choose the line/user you need and get its pass, then check.
Much to complicated (not to mention slow string and/or array manipulations). You don't need to split anything and then check each fields individually. See my alternative code.

quote:
Originally posted by Matty
Heres an example for you.
It contains a big bad wolvebug :P You forgot to close your file when a match is found ;)


-----------------------------------------


Reaper66613, about your posted Form code:

1) Since you used "Input #1, strUsername, strPassword", login.txt needs to contain:
code:
guest,qwerty
2) You need to adapt many other parts of the example code to let it work eventually. What people have provided for you in this thread here is the base for checking a file with some login info: the basic open file and checking for some matches. You need to adapt the example source from VB to incooperate that. Aka adapt the cmdOK_Click() routine because at the moment the file login.txt is simply read out when you load the form and checked against empty strings as you don't have anything entered yet in the textboxes (because the form isn't shown yet).

The code provided in this thread does not go into the Form_Load procedure but must be incooperated into cmdOK_Click()...

Also:
code:
Dim strUsername, strPassword As String
is the same as
code:
Dim strUsername As Variant
Dim strPassword As String
and not, as intended, as
code:
Dim strUsername As String
Dim strPassword As String
Never declare variables on one line, unless you state for each individual one the proper data type. strUsername is declared "As Variant" because there is no explicit data type given.


-----------------------------------------


Alternative on Matty's code (use/copy this Command1_Click() procedure over Matty's example code. It is not meant to be put in your Form, Reaper66613)
Note that this doesn't use any heavy string manipulations, nor splits or whatever (and it does close the file when a match is found :p)
Also note that the login.txt file is expected to be in the following format:
code:
guest|qwerty
login2|password2
login3|password3
login4|password4
code:
Private Sub Command1_Click()

    Dim strPath        As String
    Dim strText        As String
    Dim strMatchSearch As String
    Dim blnMatchFound  As Boolean

    strMatchSearch = txtUsername.Text & "|" & txtPassword.Text
    blnMatchFound = False

    strPath = App.Path
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"

    Open strPath & "text.txt" For Input As #1
        Do Until EOF(1) Or blnMatchFound
            Line Input #1, strText
            If strMatchSearch = strText Then blnMatchFound = True
        Loop
    Close #1

    If blnMatchFound Then
        MsgBox "Username/Pass match"
    Else
        MsgBox "Username/Pass not found"
    End If

End Sub


Help on MSDN Library (=official help files, which explain in full detail each command of VB) about:
Input #1
Line Input #1



For the 2 examples (one based on Matty's approach (project2), as listed above. The other the full working source of the VB example (project3)) see attached zip -------vv
RE: VB Help by Reaper on 09-14-2005 at 02:58 PM

omg cookie :D

thanks, it finally works :D

cheers to everyone else who posted (Y)


RE: VB Help [Another problem] by Reaper on 09-15-2005 at 05:17 PM

i was just sorting out my systme, and i thought about adding an option to create a user. so how would i go about doing this?
would i have to write the information to a file? if so how :P?

i have written a code i think i got the basics, but i want to know how to put the information in the file in the format userame|password
the code doesnt work, as in i type in the info and click ok but it just disappears and nothing appears in the txt file. also how do i mask the password characters?

code:
Dim Filename As String

Private Sub cmdCancel_Click()
Unload Me
End Sub

Private Sub cmdOK_Click()
    Dim Username As String
    Dim Password As String
    Username = txtUsername.Text
    Password = txtPassword.Text
    Open Filename For Append As #1
    Write #1, Username, Password
    Close #1
    txtUsername.Text = " "
    txtPassword.Text = " "
    txtUsername.SetFocus
End Sub

Private Sub Form_Load()
    Filename = App.Path & "\login.txt"
End Sub


RE: VB Help [Another problem] by RaceProUK on 09-15-2005 at 08:25 PM

Surely it should be 'Open Filename For Append As #1'?


RE: VB Help [Another problem] by Reaper on 09-15-2005 at 09:17 PM

lol yup that works but i get a " at the beginning of the username and end of password for example:

"123456|blah"

how do i remove it, and how do i mask the password?

[Edit] changed the code in prevous post


RE: RE: VB Help [Another problem] by CookieRevised on 09-16-2005 at 06:55 AM

quote:
Originally posted by Reaper66613
i was just sorting out my systme, and i thought about adding an option to create a user. so how would i go about doing this?
would i have to write the information to a file? if so how :P?

i have written a code i think i got the basics, but i want to know how to put the information in the file in the format userame|password
the code doesnt work, as in i type in the info and click ok but it just disappears and nothing appears in the txt file. also how do i mask the password characters?

code:
Dim Filename As String

Private Sub cmdCancel_Click()
Unload Me
End Sub

Private Sub cmdOK_Click()
    Dim Username As String
    Dim Password As String
    Username = txtUsername.Text
    Password = txtPassword.Text
    Open Filename For Append As #1
    Write #1, Username, Password
    Close #1
    txtUsername.Text = " "
    txtPassword.Text = " "
    txtUsername.SetFocus
End Sub

Private Sub Form_Load()
    Filename = App.Path & "\login.txt"
End Sub


Issue 1

Why do you assign txtUsername.Text and txtPassword.Text to variables and then immediatly write those variables? That's rather useless... Just write out txtUsername.Text and txtPassword.Text directly...
code:
Dim Username As String
Dim Password As String
Username = txtUsername.Text
Password = txtPassword.Text
......
Write #1, Username, Password


===>

Dim Username As String
Dim Password As String
Username = txtUsername.Text
Password = txtPassword.Text

......
Write #1, txtUsername.Text, txtPassword.Text





Issue 2 (main issue of your question)

As I said several times before (if not in all my replies): LOOK UP WHAT EACH FUNCTION DOES BEFORE USING IT!!! I provided links to the online MSDN Library in my previous replies, USE THEM!

If you would have read the online help of "Input #1" and read the pages like you should have and thus also clicked on "See Also" and the various "Examples" you would have seen that there are two possible commands to write to files: "Write" and "Print"... The difference is explained in great detail on the helppages (or in the help files of VB6). Also the difference can be seen quite easly if you just try it out and see the result by opening login.txt in notepad.





Issue 3

Study the complete source I gave to you. Ask yourself questions like why did he do that? Why did he used that command and not that one? And above all: what does that line exactly do?

I say this (again) because you missed the big point about delimiting (or in this case not delimiting) the data you write. Line Input, which you used to read out the file, doesn't care about delimiters; Line Input reads a complete line. This includes any characters you used for delimiting the two fields as that delimiter is nothing more then a character itself:

Reading data:

login.txt:
username, password

Reading out:
=> Line Input #1, MyString1
MyString1 will contain: username, password
(there is no MyString2 because 1) the login.txt only contains one line and 2) because Line Input reads whole lines literally, not fields)

=> Input #1, MyString1, MyString2
MyString1 will contain: username
MyString2 will contain: password
(this is because the comma is a delimiter used in VB6 to split up seperate fields when _reading_ data out of a file)

login.txt:
username@password

Reading out:
=> Line Input #1, MyString1
MyString1 will contain: username@password

=> Input #1, MyString1, MyString2
MyString1 will contain: username@password
MyString2 will contain nothing, in fact this input statement will produce an error since there is no second MyString2 field (the @ character is not interpreted as a delimiter of fields).

login.txt:
"username", "password"

Reading out:
=> Line Input #1, MyString1
MyString1 will contain: "username", "password"
(Thus, as you can see, the Line Input statement reads an entire line literally. It doesn't care about delimiters, quotes or whatever)

=> Input #1, MyString1, MyString2
MyString1 will contain: username
MyString2 will contain: password
(the comma is interpreted as delimiter AND note that the quotes around the fields aren't returned; they are also interpreted)

login.txt:
"username, password", "beep"

=> Input #1, MyString1, MyString2
MyString1 will contain: username, password
MyString2 will contain: beep
(Because the first comma in the file is part of the string here and thus not a delimiter. The second comma is a delimiter as it is no part of any field.)


Writing data:

Write #1, "Hello"
the file will contain: "Hello"
(note the quotes)

Write #1, "Hello,world"
the file will contain: "Hello,world"
(note the quotes encapsuling the whole string you just wrote and note that the comma is part of what you wrote)

Write #1, "Hello@world"
the file will contain: "Hello@world"
(note the quotes)

Write #1, "Hello", "World"
the file will contain: "Hello","World"
(note the quotes AND the use of two seperate fields and thus also the comma which isn't part of any of the two fields you've written; it is the seperator of the two variables. As seperator you can also use the ";" character when using the Write statement. But a comma will always be used as delimiter of the fields inside the file you've written to)

Write #1, "Hello", "World"; "Beep"
the file will contain: "Hello","World","Beep"
(note the mixed use of "," and ";" to seperate the fields in the code AND the comma inside the file which is always used when using the Write statement)

-----

Print #1, "Hello"
the file will contain: Hello
(note that there are no quotes)

Print #1, "Hello,world"
the file will contain: Hello,world
(note again the lack of quotes and note again that the comma was part of what you told 'Print' to write)

Print #1, "Hello@world"
the file will contain: Hello@world
(note that there are no quotes)

Print #1, "Hello", "World"
the file will contain: Hello        World
(note the 8 spaces! This is the 'delimeter' used when you use a comma to seperate fields because a comma means: put a tab between the two fields (and a tab=8 spaces) when using it with the Print statement.)

Print #1, "Hello"; "World"
the file will contain: HelloWorld
(note the lack of spaces or delimeter. ";" means concatenate the fields; "World" is just written right after "Hello". the use of ";" in this case is the same as using & (well almost but I'm not going into that atm))

Print #1, "Hello", "World"; "Beep"
the file will contain: Hello        WorldBeep
(note the difference between the output from this Print statement and the previous Write statement)

-----

So if your file contains:
guest|qwerty

You read it out with:
Line Input #1, MyStringVariable
or
Input #1, MyStringVariable
(and you need to either split the string up yourself by code and comparing the two parts seperatly (like in Matty's code) or compare the whole string directly and at once to a madeup/concatenated variable (like in my code))

You write such lines to the file with:
Print #1, "guest|qwerty"

Thus to spoon-feed you:
Print #1, txtUsername.Text & "|" & txtPassword.Text

or by splitting everything up:
Print #1, txtUsername.Text;
Print #1, "|";
Print #1, txtPassword.Text
mind the use of ";" here. It means: do not add a carriage return after the thing you wrote to the file (again stuff you can find in the help files)

or by using a premade variable:
MyStringVariable = txtUsername.Text & "|" & txtPassword.Text
Print #1, MyStringVariable




------------------------


quote:
Originally posted by Reaper66613
how do mask the password?
pretty pleeeeeeeaaaaaase learn to find out things by yourself *snif*. This question clearly shows that you don't take the time to find out things.

Did you even had a look at the 'Project3' which I gave to you in my previous post???? The password textbox is masked there. Now, instead of plainly asking about how you do that, why don't you look at the properties of that textbox yourself (right click and choose 'properties') ;).

And pleeeeeaaaase go fetch a book in your local library about VB...  :S


------------------------


I'm glad to be able to help out, but I can't keep spoon-feeding you like this though (and putting a hell of a lot of time in making these kind of posts *Segosa once asked :p*), you need to learn to find this stuff out by your own. And also important: learn the basics first (eg: that masked password question).

Do not be afraid to put some time in reading helpfiles and understanding them and trying some stuff out. In fact all that is mandatory if you want to learn something and it is even much quicker and more rewarding in the end than asking such stuff on forums.

To more you learn looking things up yourself, to more and quicker you'll learn about that stuff and the more you will be able to look at the right place the next time or to solve a problem by deduction. ;)

RE: VB Help [Another problem] by Reaper on 09-16-2005 at 01:50 PM

thanks again cookie :D
lol i think ive had enough of your spoon feeding for now :P
i guess i better use the MSDN from now on

thanks :D