Вопрос в следующем:
Можно ли из программы на VBA выполнить SQL запрос к таблице Excel?
если да, то как?
With Connection.OpenSchema(adSchemaTables)
Do Until (.BOF Or .EOF)
If .Fields("TABLE_TYPE") = "TABLE" Then
Debug.Print .Fields("TABLE_NAME")
End If
.MoveNext
Loop
.Close
End With
With Connection.OpenSchema(adSchemaTables)
Public|Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
cnn.Open ConnectionString:="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
cnn.Close
Set cnn = Nothing
With cnn.Execute("select * from [Sheet1$]")
Do Until .EOF
Debug.Print .Fields(0)
.MoveNext
Loop
.Close
End With
Sub MySQLQuery()
'Подключение к БД:
Set cnn = New ADODB.Connection
cnn.Open ConnectionString:="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
'Выборка данных:
With cnn.Execute("select * from [Данные]")
Do Until .EOF
Debug.Print .Fields(0) & " | " & .Fields(1)
.MoveNext
Loop
.Close
End With
'Отключение:
cnn.Close
Set cnn = Nothing
End Sub
cnn.Open ConnectionString:="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Private cnn As ADODB.Connection
Private r As ADODB.Recordset
Sub MySQLQuery()
Set cnn = New ADODB.Connection
cnn.Open ConnectionString:="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Dim cmd As New ADODB.Command
cmd.CommandText = "select * from [DAT]"
cmd.ActiveConnection = cnn
Set r = New ADODB.Recordset
Set r = cmd.Execute(0, 0, 0)
ActiveSheet.Cells(20, 1).CopyFromRecordset r
With cnn.Execute("select * from [DAT]")
Do Until .EOF
Debug.Print .Fields(0) & " | " & .Fields(1)
.MoveNext
Loop
.Close
End With
cnn.Close
Set cnn = Nothing
End Sub
Set r = cmd.Execute(0, 0, 0)
Set r = cmd.Execute(0, 0, 0)
cmd.Execute(0, 0, 0)
cnn.Open ConnectionString:="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Сейчас этот форум просматривают: AhrefsBot, Google-бот и гости: 100