Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2004

    Lightbulb Unanswered: VB to MySQL Login Help

    Hi all, i am a student. Currently working on a course project which i would like to have the login function. Could anyone of you here teach me or show me the sample code to VB on how to write a login code. The login will be connected to mysql database in user table, and how to encrypt the password. I have my sample code here, i will show you guys later.

    Also, my question is how do i compare the userid and password that the user has input to the database to chech whether the user is existed in the database. And, how do i create a welcome message to tell the user if he/she has been login successfully. Thanks

    below are my simple login code:

    Private Sub cmdLogin_Click()
    Dim cnMySql As New rdoConnection
    Dim sConnect As String
    Dim sADOConnect As String
    Dim sDAOConnect As String

    sConnect = sConnect & "UID=" & txtUserID.Text & ";"
    sConnect = sConnect & "Password=" & txtPassword.Text & ";"

    cnMySql.CursorDriver = rdUseOdbc
    cnMySql.Connect = sConnect & _
    "driver={MySQL ODBC 3.51 Driver};database=bus;dsn='';"


    'If cnMySql.Connect Then
    MsgBox "Conglatulation " & txtUserID.Text & ", connection established."
    'End If
    Load frmvb6Mysql

    'sADOConnect = "PROVIDER=MSDASQL;" & sDSN & sConnect
    'sDAOConnect = "ODBC;" & sDSN & sConnect
    End Sub

    any help will do a lot. thanks in advance

  2. #2
    Join Date
    Jul 2004

    Supposing that you already set a connection to the database (in Form_Load probably), now it's time to kick some you're Login button under its click event type something like this:

    Private Sub cmdLogIn_Click()
    Dim Rs As New ADODB.Recordset
    Dim StrSql As String

    StrSql = "Select * From tblUsers Where User_Name = '" & txtUserName.Text & "'" _
    & " And User_Password = '" & DecEncPass(txtPassword.Text) & "'"

    Rs.Open StrSql, con, adOpenKeyset

    If Rs.RecordCount = 0 Then
    MsgBox "Invalid User Name or Password."
    txtUserName = ""
    txtPassword = ""
    Unload Me
    Set Rs = Nothing
    Set con = Nothing
    Call OpenDB
    End If
    End Sub

    Function DecEncPass(p_str As String) As String
    Dim i As Integer
    Dim strs As String

    For i = 1 To Len(p_str) Step 1
    strs = strs + Chr(Asc(Mid(p_str, i, 1)) * 2)
    Next i
    DecEncPass = strs
    End Function

    con = the active connection
    OpenDB = user defined procedure to open a db after successfull
    connect (not included in this post)
    frmMain = the form used to load after successfull connect
    DecEncPass = a very "simple" decoding/encryption algorithm (u can
    find more "hard-to-crack" algo in the net...)

    * to encrypt, just pass a string parameter, in our example, the txtPassword control
    * to decrypt, again, just pass a parameter just like i mentioned above.

    Hope this will help you. Good luck to your project.

Posting Permissions

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