Me and my brother are creating a simple program, and we want a login form so that this program is only for our family and friends. We have a login box created use this code:
That is for the submit button, the others are just regular text boxes.Code:Private Sub cmdLogin_Click() If txtUsername = "Respected" And txtPassword = "password" Then Form2.Show Me.Hide End If If txtUsername = "SpyderZ" And txtPassword = "password" Then Form2.Show Me.Hide End If End Sub
We want to be able to add people through a database, so we can add someone new without dishing out a new copy of it, or changing to for each new person. Is there a way to let it access a database on my website? I am currently using a MySQL database. It also needs to know that for each username that there needs to be a specific password for it. We don't mind modifying it to work properly.
If you are willing to help, please tell us the coding, and tell us what we need to do for the database.
Thank you, P.S. We are pretty new to programming.
You have a table in your database that stores user info? Such as first name, last name, user name, password? There are controls you can put on your form that will connect to a database.
You have to send sql statement(s) to verify user name and password before displaying form2 (assuming form1 is the login screen and form2 is supposed to be shown on successful login).
Option Explicit
I understand what you need. Your users will need MySQL Connector/ODBC 3.51, a little driver needed to establish your connection. Then you will use ADO(ActiveX Data Objects) to surf in the database.
How to do this?
First the program will look for the user. If the user doesn’t exist it shows the error. If the user exists it will move to that record and check if the passwords are the same. If the second condition is true then it will show the LOGGED form. To secure passwords I suggest you using the MD5 Class to convert passwords in Base64 Hashes.
How to code this?
First add the following code in a module to simplify things:
Now go to the Project’s References Dialog(Click the “Project” menu then “References”) and find “Microsoft ActiveX Data Objects 2.7 Library”, tick it and click OK. Create your login form and declare the following variables:Code:Option Explicit Dim conn As ADODB.Connection Dim MySQL1 As ADODB.Recordset Dim username As String Dim passwd As String Dim serverIP As String Dim db As String Public Function connectMysql(username As String, passwd As String, serverIP As String, db As String, conn As ADODB.Connection, MySQL1 As ADODB.Recordset) Set conn = New ADODB.Connection Set MySQL1 = New ADODB.Recordset conn.CursorLocation = adUseClient conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & serverIP & ";UID=" & username & ";PWD=" & passwd & ";DATABASE=" & db & ";" On Error GoTo Err: conn.Open On Error GoTo Err: Exit Function Err: MsgBox "Error connecting to the Server, try again please! ", vbCritical End Function
Add the following code to the Form_Load() sub:Code:Option Explicit Dim conn As ADODB.Connection Dim rs As ADODB.Recordset Dim username As String Dim passwd As String Dim serverIP As String Dim db As String Dim ssql As String
You MUST change UserHere with your MySQL Username, PassHere with your MySQL User Password, ServerNameHere with your Server’s IP Address or domain, DBNameHere with your Database name and in the next line TableNameHere with your table in wich the users are stored.Code:Call connectMysql("UserHere", "PassHere", "ServerNameHere", "DBNameHere", conn, rs) ssql = "SELECT * FROM TableNameHere" rs.Open ssql, conn, adOpenStatic, adLockOptimistic
In the table there must be 2 fields “User” and “Pass”. Add users and their passwords on this table and you will be able to log with them immediately.
Now to the “LogIn” button add the following code:
If you still have trouble understanding something, download the attachment and study it.Code:If txtUser = "" Then MsgBox "Please enter the Username! ", vbCritical Else rs.MoveFirst rs.Find "[User] like '*" & txtUser & "*'" If rs.EOF Then MsgBox "The Username that you have entered doesn't exist in the database! ", vbCritical Else If txtPass = rs![Pass] Then LOGGED.Show Else MsgBox "The password is incorrect, please retype it! ", vbCritical End If End If End If
PS: You can run your MySQL Server on your PC if you have a public IP or if your clients are in LAN and don’t forget to install the MySQL Connector/ODBC 3.51 Driver to the clients.
Wish you all the best,
Dren
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks