Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a MySql login dialog. 1

Status
Not open for further replies.

systems0nemesis

Programmer
Joined
Aug 23, 2005
Messages
2
Location
US
I am new to vb6 and what I am making is a program that when run ask for a user ID and password it then looks up in the database and makes sure the user and password is there. I can get it to connect to the database and it will verify the user and password but I need a way for the user to enter that database server address because because they will be accessing diffrent server databases. How would I go about doing this? Also I would like to add a way to the program where the login checks to make sire the user and password have not been band. I hope this is not to much trouble. I have been looking around and have not see much for mysql, maybe I over looked it but any help would be nice. Thanks very much here is the login code

Private Sub cmdOK_Click()
Dim conn As adodb.Connection
Dim rs As New adodb.Recordset
Dim fld As adodb.Field
Dim sql As String
'connect to MySQL server using MySQL ODB
' C 3.51 Driver
Set conn = New adodb.Connection
conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=test; UID=test; PWD=test; OPTION=3"
conn.Open

sql = "select * from accounts Where User='" & User & "' AND Password='" & Password & "'"
rs.Open sql, conn
If rs.EOF = True Then
MsgBox "Invalid User or Password, try again!", , "Login"
User.SetFocus
SendKeys "{Home}+{End}"

Else
MsgBox "Welcome, " & User
me.hide
End If
End Sub
 
Hi,

I hope this gives you a guide.

1. Create a new form (frmLogin) containing four textboxes (txtServer, txtDatabase, txtUsername and txtPassword) and two commandbuttons (cmdOK and cmdCancel)
2. Add the follwing code to frmLogin
Code:
Option Explicit

Private Sub cmdCancel_Click()

Me.Hide

End Sub

Private Sub cmdOK_Click()

Me.Tag = "OK"
Me.Hide

End Sub

Private Sub Form_Unload(Cancel As Integer)

If Me.Tag = "" Then
   Cancel = -1
   Me.Hide
End If

End Sub
3. In the Form_Load event of your application put
Code:
Private Sub Form_Load()

Dim conn As ADODB.Connection
Dim rs As New ADODB.Recordset
Dim fld As ADODB.Field
Dim sql As String

On Error Resume Next

Do
   frmLogin.Show vbModal, Me
   
   If frmLogin.Tag = "OK" Then
      'connect to MySQL server using MySQL ODB
      '     C 3.51 Driver
      Set conn = New ADODB.Connection
      conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" & _
                              "SERVER=" & frmLogin.txtServer.Text & ";" & _
                              "DATABASE=" & frmLogin.txtDatabase.Text & ";" & _
                              "UID=" & frmLogin.txtUsername.Text & ";" & _
                              "PWD=" & frmLogin.txtPassword.Text & ";" & _
                              "OPTION=3"
      conn.Open
      
      If Err.Number <> 0 Then
         If MsgBox("Error " & Err.Number & vbLf & Err.Description, _
                   vbRetryCancel + vbExclamation, "Login") = vbCancel Then
            frmLogin.Tag = "Cancel"
            Unload frmLogin
            End
         End If
         Err.Clear
         
      Else
         sql = "select * from accounts " & _
               "Where User = '" & frmLogin.txtUsername.Text & "' " & _
               "AND Password = '" & frmLogin.txtPassword.Text & "'"
         rs.Open sql, conn
         
         If Err.Number <> 0 Then
            If MsgBox("Error " & Err.Number & vbLf & Err.Description, _
                      vbRetryCancel + vbExclamation, "Login") = vbCancel Then
               frmLogin.Tag = "Cancel"
               Unload frmLogin
               End
            End If
            Err.Clear
         ElseIf rs.EOF = True Then
            MsgBox "Invalid User or Password, try again!", vbExclamation, "Login"
            frmLogin.Tag = ""
         Else
           MsgBox "Welcome, " & frmLogin.txtUsername.Text
           Unload frmLogin
         End If
      End If
   Else
      frmLogin.Tag = "Cancel"
      Unload frmLogin
      End
   End If
Loop

End Sub
Obviously this could be tidied up, for example
frmLogin could contain some checking to ensure all textboxes have been filled in
or
frmLogin.txtServer could be replaced with a combobox pre-populated with valid server names

Trevor
 
Thanks very much. That was what I was wanting Thanks very much. Your a life saver!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top