VBA to SQL Server Connectivity | Connect SQL Server using VBA | Import SQL Server Table using VBA

preview_player
Показать описание
VBA to SQL Server Connectivity | Connect SQL Server using VBA | Import SQL Server Table using VBA

=============code are below================
Option Explicit

Sub sqltovba()
Dim con As String
con = "Driver={SQL Server}; Server=DESKTOP-R8EOO8P;Database=Dbmay"

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

cn.Open con
Set rs = cn.Execute("Select * from Student")

Dim col, i As Integer

For col = 0 To rs.Fields.Count - 1
Cells(1, col + 1).Value = rs.Fields(col).Name
Next

Range("A2").CopyFromRecordset rs
cn.Close
End Sub
======================================================
Рекомендации по теме