Avatar of OB1Canobie
OB1Canobie
Flag for United States of America asked on

Connecting to SQL Server with ViB Form

I have an SQL Server database "RCM" which has a table "data_live".  I have a visual basic application that has a form "worklist" which will display all data in the table.  Can anyone help me complete this task?  The form is a datagrid view.  Thanks.
Visual Basic ClassicMicrosoft SQL Server

Avatar of undefined
Last Comment
OB1Canobie

8/22/2022 - Mon
Daniel Wilson

ICA

Assuming you've named your datagrid as grd, and you want to load data after you click a button Command1, this code snippet will work just fine. Make sure you put a reference to Microsoft ActiveX Data Objects
Private Sub Command1_Click()
On Error GoTo errhan
    Dim con As New ADODB.Connection
    Dim rec As New ADODB.Recordset
    con.Open "DRIVER=SQL Server;SERVER=192.168.0.57\SQLEXPRESS;PASSWORD=dynamite;UID=MyUserName;DATABASE=RCM"
    rec.Open "SELECT * FROM data_live", con, adOpenStatic, adLockOptimistic
    Set grd.DataSource = rec
    Exit Sub
errhan:
    MsgBox Err.Description
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
pathakhemant

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
OB1Canobie

ASKER
Great resource.  Thanks.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck