login code - Visual Basic 6.0

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 79529 Views - Last Post: 24 August 2010 - 08:10 AM Rate Topic: -----

#1 antukin_cs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-October 07

login code - Visual Basic 6.0

Post icon  Posted 12 October 2007 - 04:09 AM

Private Sub cmdok_Click()
Static y As Integer
 Adodc1.Refresh
	With Adodc1.Recordset
	
	If .RecordCount > 0 Then
		 If .EOF = False Then
		.MoveFirst
		If !UserName = txtUsername And !Password = txtPassword Then
		MsgBox "Login Successful!"
		frmMenu.Show
		Unload Me

	Else
		MsgBox "Invalid username/password!", vbOKOnly + vbCritical, "Access denied!"
		txtUsername.Text = ""
		txtPassword.Text = ""
		txtUsername.SetFocus
	End If
	End If
	End If
	End With
End Sub



somebody please help me fix these code.. my username and password are saved on MS Access...the problem is that this code only accepts the 1st username and password stored on Access, if I try to login with the 2nd username and password stored on Access, it displays the msgbox stating "Invalid username/password!"...need immediate help! thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: login code - Visual Basic 6.0

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 04:56 AM

For starters, you're telling it to move to the first record (reason for this I don't know) when you use the .MoveFirst, all you need to check for is a record count, which you do with If .RecordCount > 0. When you return a recordset the cursor is always at the first record.

Second, in all my years programming (maybe I have used it its just been to long for my old mind to remember lol) Ive never seen !UserName or the ! used with anything involving a RecordSet. Ive made some changes to your code, just to get rid of some stuff I dont really think is necessary.

Now, I'd like to see your query please, the problem may lie there.

Private Sub cmdok_Click()
Static y As Integer
Adodc1.Refresh
	With Adodc1.Recordset
	
	If .RecordCount > 0 Then
		If !UserName = txtUsername And !Password = txtPassword Then
			MsgBox "Login Successful!"
			frmMenu.Show
			Unload Me
		Else
			MsgBox "Invalid username/password!", vbOKOnly + vbCritical, "Access denied!"
			txtUsername.Text = ""
			txtPassword.Text = ""
			txtUsername.SetFocus
		End If
	End With
End Sub


Was This Post Helpful? 0
  • +
  • -

#3 antukin_cs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-October 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 05:10 AM

what query?? you mean the username and password saved in MS Access?? i used a table not a query..is that the problem?

i just tested the code you gave me but it didnt worked....same thing happened..

This post has been edited by antukin_cs: 12 October 2007 - 05:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 05:31 AM

You have to have something somewhere to get the data from the database like select something from some table where something = something, you don't have that anywhere?

Also, I never said the modified code would work, I said I modified it to remove some unnecessary code :)
Was This Post Helpful? 0
  • +
  • -

#5 antukin_cs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-October 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 05:38 AM

no i dont have it.. i just learned the code recently and i just cant debug it myself...the thing you said about "select something from some table where something = something", i dont know how to manipulate a table or a query whatever it is...the code is workin fine but it only accepts the first username and password saved..

This post has been edited by antukin_cs: 12 October 2007 - 05:39 AM

Was This Post Helpful? 0
  • +
  • -

#6 antukin_cs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-October 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 05:46 AM

hey man! i fixed the damn code! you were ryt, the error comes from the code you removed (.MoveFirst)

the thing is, i thought about what u said about "select something from some table where something = something"

i just removed the .MoveFirst and replaced it with .MoveNext and it worked!
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 05:53 AM

Try using the ADODC.RecordSet.Find() Function and see if that helps:

Private Sub cmdok_Click()
Static y As Integer
Adodc1.Refresh
	With Adodc1.Recordset
		Dim query As String
		query = "username = '" & txtUsername.Text & "'"
		query = query & " and password = '" & txtpassword.Text & "'"
		.Find (query)
		
		If .RecordCount > 0 Then
			MsgBox "Login Successful!"
			frmMenu.Show
			Unload Me
		Else
			MsgBox "Invalid username/password!", vbOKOnly + vbCritical, "Access denied!"
			txtUsername.Text = ""
			txtpassword.Text = ""
			txtUsername.SetFocus
		End If
	End With
End Sub
End Sub



Replace the fields I used in my query variable with the column names in your database.

Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#8 antukin_cs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-October 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 06:09 AM

Private Sub cmdok_Click()
Static y As Integer
Adodc1.Refresh
With Adodc1.Recordset
Dim query As String
query = "username = '" & txtUsername.Text & "'"
query = query & " and password = '" & txtpassword.Text & "'"
.Find (query)

If .RecordCount > 0 Then
MsgBox "Login Successful!"
frmMenu.Show
Unload Me
Else
MsgBox "Invalid username/password!", vbOKOnly + vbCritical, "Access denied!"
txtUsername.Text = ""
txtpassword.Text = ""
txtUsername.SetFocus
End If
End With
End Sub
End Sub



hey man. i replaced the bold "query" with Username and Password..but when i logged in..the following error popped up... "Arguements are of the wrong type, are out of acceptable range, or are with conflict with one another" and then when i pressed debug.. the bold and underlined code was highlighted..

This post has been edited by antukin_cs: 12 October 2007 - 06:14 AM

Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 06:19 AM

You're right the Find function requires more arguments than just the 1 I provided. Read the above link and it will show what other arguments are required.

Quote

recordset.Find Criteria, SkipRecords, SearchDirection, Start

Was This Post Helpful? 0
  • +
  • -

#10 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 06:22 AM

I was just wondering. Is criteria just one search condition or can you have more by using AND or something. AND didn't work so I ended up using SQL queries to get the records when I had to do my school project.
Was This Post Helpful? 0
  • +
  • -

#11 antukin_cs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-October 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 06:28 AM

well lucky for you Louisda you already know how to use SQL bcoz i dnt...

hey PsychoCoder, i dont have any idea how am i gonna use these "recordset.Find Criteria, SkipRecords, SearchDirection, Start",,il read the link you gave but i dont think il get if its something about SQL...noob hir... :D

This post has been edited by antukin_cs: 12 October 2007 - 06:31 AM

Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 06:31 AM

I've never used it Louisda16th, but then again Ive never used a drag & drop data control either
Was This Post Helpful? 0
  • +
  • -

#13 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 06:34 AM

Quote

well lucky for you Louisda you already know how to use SQL bcoz i dnt...

Well as far as your project goes. You can learn what you need in a jiffy. Its not all that hard. :)

View PostPsychoCoder, on 12 Oct, 2007 - 07:01 PM, said:

I've never used it Louisda16th

Im Confused :). It refers to Find or SQL statements.

Quote

, but then again Ive never used a drag & drop data control either

You mean ADODC, DAO right?
Was This Post Helpful? 0
  • +
  • -

#14 antukin_cs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-October 07

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 06:50 AM

hey PsychoCoder i didnt get anything there man....i couldnt understand what those codes mean...man i have to study those codes! :(
Was This Post Helpful? 0
  • +
  • -

#15 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: login code - Visual Basic 6.0

Posted 12 October 2007 - 07:18 AM

Let me help you out. Firstly set your ADODC's recordsource property to 1-adCommandText. Now Try this piece of code.
	Adodc1.RecordSource = "SELECT * FROM USERS WHERE USERNAME ='" + txtUserName.Text + "'"
	Adodc1.Refresh

	If (Adodc1.Recordset.EOF = False) Then
		If (txtPassword.Text = Adodc1.Recordset.Fields("Password")) Then
			MsgBox ("Login Successful")
		Else
			MsgBox ("Login Failure. Incorrect Password")
		End If
	Else
		MsgBox ("Login Failure. Username does not exist.")
	End If


Put this in the Click event of your button. Here's how SQL works. The statement is pretty straightforward. It basically means "Select all records from the table User where the content of the field UserName is equal to the content of the textbox txtUsername". The rest is easy. Also notice the two ' surrounding txtUserName.text. This is because for comparing strings, SQL statements go as
SELECT * FROM Users WHERE UserName = '<your_username>'


* refers to selecting all fields.
Hope this helps :).

P.S. I don't know what name you have used for your table. In the SQL statement replace "Users" with your table's name. Just for reference, The general syntax for a simple SQL query statement is:
SELECT <columnlist> FROM <table_name> WHERE <search_condition>


In the columnlist, separate each column name with a comma. Multiple search conditions can be used used AND, OR and I think NOT as well.

Whew! Long time since I used SQL :P!!

This post has been edited by Louisda16th: 12 October 2007 - 07:27 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3