How to Connect MS SQL Server and Visual Basic 6.0

June 15, 2012 // In: VB 6.0
Today, I’m going to show you how to connect SQL Server database using VB 6.0. Just follow the instructions.

Note down your database name and table along with its fields to work with. We’ll connect through ADODB.
Just follow the steps..

1. Open Visual Studio and Create a new standard project.
2. Goto Project menu and Click on References
3. select Microsoft ActiveX Data Objects 2.0 Library and Press OK button.

4. Now Lets Suppose we have database with the name “SharpCoders” and a table with the name “tbl_users” (username, password)
5. Place 2 text boxes, and a command button on the form.

6. Copy and paste following code.

Dim Con As New ADODB.Connection
Dim Rec As New ADODB.Recordset
Private Sub Command1_Click()
Con.Open “Driver=SQL Server;Server=.\SQLEXPRESS;Database=SharpCoders;uid=sa;pwd=sa;”

Rec.Open “Select * from tbl_users where  username='” & Text1.Text & “‘ and password='” & Text2.Text & “‘”, Con, adOpenStatic, adLockReadOnly

If Rec.RecordCount > 0 Then
MsgBox “Record Found”
MsgBox “No Record Found”
End If

End Sub

Hide Windows with Source Code ( VB 6.0 )
  • Hassan Sadek


  • Junaks