Thursday, March 6, 2014

Create Login with Database using VB6 and MS Access

This tutorial will teach you how to create Log-in with database using VB6 and MS Access.


Keep in mind that your project and database file should be on the same folder. 

Here are the codes: 


CODES FOR MODULE1
====================================================================
Option Explicit

Public cnn As ADODB.Connection
_____________________________________________________________
Public Sub getconnected()
Set cnn = New ADODB.Connection
cnn.CursorLocation = adUseClient
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database1.mdb" & "; Persist Security Info=False;"
cnn.Open

End Sub
==============================================================
NOTE: The red text is the name of your database file.
==============================================================


CODES FOR FORM1 
==============================================================
Option Explicit

Private Sub cmdlogin_Click()
If txtuser.Text = "" Then
MsgBox "Username is Empty.", vbInformation
txtuser.SetFocus
Exit Sub
ElseIf txtpass.Text = "" Then
MsgBox "Password is Empty"
txtpass.SetFocus
Exit Sub
Else
Call login
End If
End Sub

Private Sub login()
Module1.getconnected
Dim rs As New ADODB.Recordset
rs.Open "Select * From Table1 Where username = '" & txtuser.Text & "'", cnn, adOpenStatic, adLockReadOnly
If rs.RecordCount < 1 Then
MsgBox "Username is Invalid. Please try again.", vbInformation
txtuser.SetFocus
Exit Sub
Else
If txtpass.Text = rs!pass Then
Unload Me
Load Form2
Form2.Show
Exit Sub
Else
MsgBox "Password is Invalid. Please try again.", vbInformation
txtpass.SetFocus
Exit Sub
End If
End If
Set rs = Nothing
End Sub

Private Sub Form_Load()

End Sub
==============================================================
NOTE: The red text is the name of your table in the database.
==============================================================

To avoid some errors, you should do the following:

-Check the name of your database. 
-Check the name of your table in your database. 
-The vb6 project and database file should be on the same folder.
-Check the variables if you typed them correctly.


If you have any questions, feel free to post a comment below.





7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi! in the following line: rs.Open "Select * From Table1 Where username = '" & txtuser.Text & "'", cnn, adOpenStatic, adLockReadOnly
    I received the following error message: "No value given for one or more required parameter"
    Can you please help me?
    Thank you!

    ReplyDelete
  3. under in module1 user-defined type not defined why ?

    ReplyDelete
  4. what are the codes to be used? and it will need some visual basic?

    ReplyDelete
  5. Error cnn.Open
    the same with u please help

    ReplyDelete
  6. can I create a history of login? in vb6.0? with this codes?

    ReplyDelete