Link to home
Create AccountLog in
Avatar of OB1Canobie
OB1CanobieFlag 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.
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

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
Avatar of pathakhemant
pathakhemant

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of OB1Canobie

ASKER

Great resource.  Thanks.