Closed Thread
Results 1 to 3 of 3

Thread: Need Help Connecting to a Database for Logins - VB6

  1. #1
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

    Need Help Connecting to a Database for Logins - VB6

    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:

    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
    That is for the submit button, the others are just regular text boxes.

    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    o0TheNerd0o's Avatar
    o0TheNerd0o is offline Learning Programmer
    Join Date
    Sep 2007
    Location
    I.E. So-cal
    Posts
    61
    Rep Power
    0

    Re: Need Help Connecting to a Database for Logins - VB6

    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

  4. #3
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

    Re: Need Help Connecting to a Database for Logins - VB6

    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:

    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
    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 rs As ADODB.Recordset
    Dim username As String
    Dim passwd As String
    Dim serverIP As String
    Dim db As String
    Dim ssql As String
    Add the following code to the Form_Load() sub:

    Code:
    Call connectMysql("UserHere", "PassHere", "ServerNameHere", "DBNameHere", conn, rs)
    
    ssql = "SELECT * FROM TableNameHere"
    rs.Open ssql, conn, adOpenStatic, adLockOptimistic
    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.

    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:

    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
    If you still have trouble understanding something, download the attachment and study it.

    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
    Attached Files Attached Files

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner Connecting to MYSQL Database!
    By Epatron in forum PHP Tutorials
    Replies: 7
    Last Post: 08-16-2011, 11:14 AM
  2. Connecting to MySQL database
    By Vswe in forum Database & Database Programming
    Replies: 4
    Last Post: 08-25-2009, 02:34 PM
  3. Connecting database with applet
    By ahmed in forum Java Help
    Replies: 1
    Last Post: 07-01-2009, 10:25 PM
  4. Connecting to a Database
    By reachpradeep in forum Java Help
    Replies: 3
    Last Post: 03-04-2007, 10:52 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts