hi im edison and i need help regarding visual basic 6
im making a project where the class would take an exam..
before they take the exam, i want them to enter their username and password (which i already made in Visdata).so if they enter a username then password, the program would look it up in the database and if their name and password were there, they could take the test. i already put the data control with databasename and recordsourcetype, and then i set the datafield and datasource for the textboxes. i just dont know how to write a simple code that would read the data in the database to check whether they are in that list and if their username and password is correct.
Also, im planning to make the questions in random so that they would answer different questions. at the end, the program would evaluate their scores and see how they did in the exam. how do u do all that?
im hoping that you would answer me as soon as possible.. i need it badly. thanks for helping me.
With regard to the user/password validation, you should forget about trying to databind the fields to your database.
The process is this (basically)
1. select the password using the username (keyfield in database) using an sql select query would probably be best. e.g "SELECT User.Password FROM User Where User.UserID = '" & txtUserName.Text & "'"
2. check whether the password in the text field matches the one returned from the database. (or whether the user does actually exist. If no records are returned, there is no user).
e.g
Code:
' Goto Project > References and add a reference to 'Microsoft ActiveX Data Objects 2.x'
Dim myRS As ADODB.Recordset
Dim myConnectionString As String
Private Sub Form_Load()
Set myRS = New ADODB.Recordset
' If you need help with your connection string, visit www.connectionstrings.com
myConnectionString = "[put your connectionstring here]"
myRS.Open "SELECT User.Password FROM User WHERE User.UserID = '" & txtUserID.Text & "'", myConnectionString
If myRS.BOF And myRS.EOF Then
' No user found
Else
If myRS.Fields(0).Value = txtPassword.Text Then
' User found and password matched.
Else
' Password was incorrect.
End If
End If
End Sub
Hope this helps.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
i dont get it that much.. is there a simpler code for that? that looks complicated.. i dunno how to use those connection strings etc. when i used excel with VB before, it was easier but with VB alone, im having a hard time. any other options in VB that i can do?thanks for the reply...
Someone might have submitted a password logon system to www.planetsourcecode.com .... you might find something there.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
For the rest of your question, you could store each of your questions, answers, and correct answer as a single row in a database table, with each record having a unique id [most db's will auto assign for you]. Your database might look like chart below.
Then, once user gets past password stuff, you write routine that will randomly pull rows of questions to present to the user. The answers can be presented as the captions of option buttons. Then you can check the caption of the option chosen for a question against the correct_answer column for that record to see if user's answer was right or wrong. You could use various scoring techniques in another routine at the end.
That's about it. Check back if you run into problems doing the coding.
If kids were left to their own devices, would they ever come up with a thing like war?......The Wheel / Todd Rundgren
Do canibals not eat clowns because they taste funny?
im making a project where the class would take an exam..
before they take the exam, i want them to enter their username and password (which i already made in Visdata).so if they enter a username then password, the program would look it up in the database and if their name and password were there, they could take the test. i already put the data control with databasename and recordsourcetype, and then i set the datafield and datasource for the textboxes. i just dont know how to write a simple code that would read the data in the database to check whether they are in that list and if their username and password is correct.
Also, im planning to make the questions in random so that they would answer different questions. at the end, the program would evaluate their scores and see how they did in the exam. how do u do all that?
im hoping that you would answer me as soon as possible.. i need it badly. thanks for helping me.
Okay, binding the textboxes to the datacontrol [as you say you've setup] is useful for editing and displaying usernames and passwords in the database -- not really for querying against the database. What you've got going here is a screen for an admin/teacher/whoever to enter the names of people who should be able to take the exam.
You'll want to have a txtName and txtPassword on a form and leave them unbound. You'll want to create a recordset in code (not data control) through which you will check to see if the values entered in txtName and txtPassword can be found in the database.
The example given by <ahem> HairyMonkeyMan shows you how to create the recordset object. He's showing how, using the myRS.Open method you can tell the database you only want to see records that have (WHERE) userID = 'blah' and userpwd='blah'. The blahs would be the values located in your txtName and txtPassword controls.
Now, after doing the 'Open' method, myRS will contain only the records found in the database which met the criteria of username=txtname and password=txtpassword. This means that all you have to do, to check if the name and password are found in the database, is check to see if myRS has anything in it at all. If it does, then the values entered were found in the database and returned to the myRS object. If there is nothing in myRS, then no record in the database contained the given username and password.
can u specify what database are you using( with version)? n are u storing the questions in the database too? if so plz also tell me the names of the tables that u are using in this cotext.
im using microsoft access... i havent done a database for questions yet.. just for the username and password first.. coz i dunno what connectionstring to put. if i make the questions in microsoft access, how would i make it appear in my form? am i going to make different forms for those questions? thanks for helping me
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
i did this but it says "data type mismatch in criteria expression"
i dont know where i went wrong...:
Dim myConnectionString As String
Dim MyRS As ADODB.Recordset
Private Sub Form_Load()
Set MyRS = New ADODB.Recordset
myConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Owner\Desktop\namelist.mdb;User Id=admin;Password=;" '(do i still have to put user ID and password for myconnectionstring?)
End Sub
Private Sub cmdidsubmit_Click()
MyRS.Open "SELECT List.password FROM List Where List.id_number = '" & txtid.Text & "'", myConnectionString
If MyRS.Fields(0).Value = txtpass.Text Then
MsgBox "correct"
Else
MsgBox "wrong"
MyRS.Open "SELECT List.password FROM List Where List.id_number = '" & txtid.Text & "'", myConnectionString
I suspect this is the expression where you are getting the data type mismatch, yes?
Its because when you are constructing an sql statement, the DBMS (msaccess) needs to know what the data type is that you are referring to in the string.
i.e your WHERE clause looks like this: 'WHERE List.id_number = '" & txtid.Text & "'"
Lets say for example you have the number 123 in txtid.Text, your string will be presented to the DBMS like this: WHERE List.id_number = '123'. This is because of the ' marks in the statement (which should be used to denote a string).
Try changing that line to the following:
Code:
MyRS.Open "SELECT List.password FROM List Where List.id_number = " & txtid.Text, myConnectionString
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
thanks so much for helping me.. it worked.. the username and password.. i just have to add other restrictions. ill just message if i need other help.. thanks for helping me guys
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.