Shady Level: VB Guru

 Registered: 08-07-2002 Posts: 305
|
Re: Help creating a login program
Hi there,
Right I have tried the following code on my PC and it works fine, so here goes.
Create a MYSQL database called LOGIN, with a table called DETAILS and a field called NAME.
I have assumed there is no password on the MySQL database.
OK, next create a form with a button and a text box, then paste the following code underneath the command button:-
'Create connections
Dim ADOsqlCON As ADODB.Connection
Dim ADOsqlRS As ADODB.Recordset
Set ADOsqlCON = New ADODB.Connection
Set ADOsqlRS = New ADODB.Recordset
'Log into MYSQL Dbase
ADOsqlCON.CursorLocation = adUseClient
ADOsqlCON.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
& "SERVER=andreavb.ipowermysql.com;" _
& "DATABASE=login;" _
& "UID=root;" _
& "PWD=;"
'Open Recordset and select all records
ADOsqlCON.Open
ADOsqlRS.Open "SELECT * FROM Details", ADOsqlCON, adOpenStatic, adLockOptimistic
'Count number of records held in database
SQL_Records = ADOsqlRS.RecordCount
'Begin Loop to cycle through all records
For x = 1 To SQL_Records
'Check to see if text in text box matches current record
If ADOsqlRS!Name = Text1.Text Then
'If it matches do this
MsgBox "LOGIN", vbInformation
GoTo Close_ADO
Else
'Else move to the next record
ADOsqlRS.MoveNext
End If
'Continue Loop
Next x
'If no match has been made do this
MsgBox "No LOGIN", vbExclamation
Close_ADO:
'Close conection
ADOsqlRS.Close
ADOsqlCON.Close |
and hopefully there you have a very simple login program, you could also use this code to encrypt your entries within the database for extra security:-
'Decrypt text encrypted with EncryptText
Private Function DecryptText(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase$(strPwd)
#End If
'Decrypt string
If Len(strPwd) Then
For i = 1 To Len(strText)
c = Asc(Mid$(strText, i, 1))
c = c - Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff = strBuff & Chr$(c And &HFF)
Next i
Else
strBuff = strText
End If
DecryptText = strBuff
End Function |
'Encrypt text
Private Function EncryptText(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase$(strPwd)
#End If
'Encrypt string
If Len(strPwd) Then
For i = 1 To Len(strText)
c = Asc(Mid$(strText, i, 1))
c = c + Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff = strBuff & Chr$(c And &HFF)
Next i
Else
strBuff = strText
End If
EncryptText = strBuff
End Function |
its very simple to use, to encrypt your details use it like this
| ADOsqlRS!Name = EncryptText((UserText), PassText) |
and if you want to use it with the login code I provided just use this IF statement.
| If ADOsqlRS!Name = EncryptText((UserText), PassText) Then |
Hope that is of some use to you!
Regards
Shady
____________________________
I don't wanna die... but I ain't keen on livin' either
|