Database Connection and Login Form Visual Basic 6 Tutorial

2. Select Standard EXE. A project will be created with a default form1 on it.


3. To use ADDODB connection, select Project>References and check Microsoft Active X Data Objects 2.8 Library

4. Add a new module for the database connection string

5. Select Open and add this following code on the module you just added

Public dbconnect As New ADODB.Connection

Sub main()

dbconnect.CursorLocation = adUseClient

dbconnect.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\sample.mdb" 'MS access database connection string

If dbconnect.State = adStateOpen Then 'check if database connection is successful

MsgBox "Successfully connected to the database"

Form1.Show 'after connecting to the database form1 will show

Else

MsgBox "database connection failed"

End If

End Sub

6. Right click on the Project and select Project Properties

Change the Startup Object from form1 to Sub Main and click OK. This way the sub main will execute first

7. On form 1, put the necessary textboxes, label and command button for login form.

8. Double click on login button and paste the following code

Private Sub Command1_Click()

Dim rslogin As New ADODB.Recordset 'declare a new recordset that will hold the value of the database connection

rslogin.Open "Select * from tuser where username = '" & Text1.Text & "' and password = '" & Text2.Text & "'", dbconnect, adOpenKeyset, adLockOptimistic 'open the database connection and check if the username and password exist on the tuser table

If rslogin.RecordCount <> 0 Then 'if record exist then

MsgBox "Login success"

'you can add code here like mdimain.show and unload the login form

Else

MsgBox "invalid username/password"

End If

End Sub

9. After this you can add mdiform for your main form and any other form.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working