Hi,
I want to pick only one value from an SQL base. I know how to bring a whole tabel to Excel. I use:
As you can see, I can get all those columns into a sheet with headers. What I want to do is bring only one value without a header that I can assign to A1 cell or a variable.
I tried something like:
However, it does not work. Can you please help me?
I want to pick only one value from an SQL base. I know how to bring a whole tabel to Excel. I use:
Code:
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DRIVER=SQL Server;SERVER=192.168.0.10\SQL;UID=sa1;PWD=password;APP=System operacyjny Microsoft® Windows®;WSID=User1;DATABASE=Wyposaz" _
), Array("enie")), Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT S_operatorzy.ID, S_operatorzy.Login, S_operatorzy.Phraser, S_operatorzy.Upr_sprzet_inf, S_operatorzy.Upr_sprzet_mod, S_operatorzy.Upr_odziez_inf, S_operatorzy.Upr_odziez_mod, S_operatorzy.Upr_p" _
, _
"ersonel_inf, S_operatorzy.Upr_personel_mod, S_operatorzy.Upr_alerty" & Chr(13) & "" & Chr(10) & "FROM Wyposazenie.dbo.S_operatorzy S_operatorzy" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Tabela_Kwerenda_z_sql22"
.Refresh BackgroundQuery:=False
End With
End Sub
As you can see, I can get all those columns into a sheet with headers. What I want to do is bring only one value without a header that I can assign to A1 cell or a variable.
I tried something like:
Code:
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Driver={SQL Server};Server=SARA;UID=sa1;Password=password;Database=Wyposazenie"
nSQL = "SELECT S_operatorzy (S_operatorzy.Login" & vbNewLine & "WHERE S_operatorzy.Login 'mylogin')"
cnn.Execute nSQL
Range("A1").Value = cnn
However, it does not work. Can you please help me?