You are on page 1of 1

Sub dbconnection()

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim rs As ADODB.Recordset
Dim row As Integer
Dim col As Integer
Dim strconn As String
strconn = "driver={sql server};server=CUONGVO-
PC\SQLEXPRESS;database=pmsdb;uid=sa;pwd=kings"
cn.Open strconn
Set rs = New ADODB.Recordset
rs.ActiveConnection = cn
rs.Open "select* from table_1"
row = 5
col = 1
For Each fld In rs.Fields
Sheet1.Cells(row, col).Value = fld.Name
col = col + 1
Next
rs.MoveFirst
row = row + 1
Do While Not rs.EOF
col = 1
For Each fld In rs.Fields
Sheet1.Cells(row, col).Value = fld
col = col + 1
Next
row = row + 1
rs.MoveNext
Loop
End Sub

You might also like