Pulling multiple excel values for VBA SQL query and using IN - excel

I've a below VBA code and I want to read four values a, b, c, d from an excel column (A1:A4) so those can be part of my SQL query IN statement when I connect to database.
I build up the sWHEREclause to create ('a','b','c','d') string that but the final SQL query SQLStr seems to have some error and my code fails.
Can someone please help.
Thank you so much in advance.
Sub ADOExcelSQLServer()
Dim Cn As ADODB.Connection 'connection variable
Dim Server_Name As String ' text type
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset ' Database connection variable
Dim rRange As Range
Dim rCell As Range
Dim sWHEREclause As String
Set rs = New ADODB.Recordset
Server_Name = "server name"
Database_Name = "Database name"
Set rRange = Sheet1.Range("A1:A4")
For Each rCell In rRange
sWHEREclause = sWHEREclause & "'" & rCell & "',"
Next rCell
'SQLStr = "Select * from MASTER_QUERY mq where mq.ModelNum in ('a','b','c','d')" 'commented line
SQLStr = "Select * from MASTER_QUERY mq where mq.ModelNum IN (" & sWHEREclause & ")"
Set Cn = New ADODB.Connection 'set connection with databse
Cn.Open "Provider=SQLOLEDB;Data Source=" & Server_Name & "; Initial Catalog=" & Database_Name & "; Integrated Security=SSPI;" 'connection parameter to the database
rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
With Worksheets("sheet1").Range("b2:z500") ' Enter your sheet name and range here
' Range(A1).Value = "ID)"
.ClearContents ' clear content first in excel
.CopyFromRecordset rs
End With
' Tidy up
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub

Your string ends with a ,
do your loop like this:
Dim delim as string
delim = ""
For Each rCell In rRange
sWHEREclause = sWHEREclause & delim & "'" & rCell & "'"
delim = ", "
Next rCell

Related

Import from closed workbooks using ADODB

After so many hours in that field, I could be able to get data from all the worksheets in closed workbook and could get data from specific columns using ADODB.
#Siddharth Rout helped me to be able to get the sheet names in the order of tab.
The following code works fine for only one closed workbook. But in fact I am trying to do the same and get all the data from the specific column (Reference - Ref No - Number ..) from several workbooks
Sub ImportFromClosedWorkbook()
Dim e, ws As Worksheet, cn As ADODB.Connection, rs As ADODB.Recordset, rsHeaders As ADODB.Recordset, b As Boolean, sFile As String, shName As String, strSQL As String, iCol As Long
sFile = ThisWorkbook.Path & "\Sample.xlsx"
Dim con As Object
Set con = CreateObject("DAO.DBEngine.120")
Dim rsData As ADODB.Recordset
Set cn = New ADODB.Connection
cn.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & sFile & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"";"
Set ws = ThisWorkbook.ActiveSheet
Dim db As Object, i As Long
Set db = con.OpenDatabase(sFile, False, True, "Excel 12.0 XMl;")
For i = 0 To db.TableDefs.Count - 1
sName = db.TableDefs(i).Name
b = False
strSQL = "SELECT * FROM [" & sName & "]"
Set rsHeaders = New ADODB.Recordset
rsHeaders.Open Source:=strSQL, ActiveConnection:=cn, Options:=1
For iCol = 0 To rsHeaders.Fields.Count - 1
For Each e In Array("Ref No", "Reference", "Number")
If e = rsHeaders.Fields(iCol).Name Then
b = True: Exit For
End If
Next e
If b Then Exit For
Next iCol
If b Then
strSQL = "SELECT [" & e & "] FROM [" & sName & "]"
Set rsData = New ADODB.Recordset
Set rsData = cn.Execute(strSQL)
ws.Range("A" & ws.Cells(Rows.Count, 1).End(xlUp).Row + 1).CopyFromRecordset rsData
rsData.Close
End If
Next i
db.Close: Set db = Nothing
Set con = Nothing
cn.Close: Set cn = Nothing
End Sub
Is it suitable to build a public procedure or what's the best approach in that case and how can I release the objects in correct way?
I would break out your code even more - there are distinct activities which could be factored out into reusable methods.
FYI your tableDefs objects already contains the field names, so there's no need to separately query for those.
Eg:
Sub ImportFromClosedWorkbook()
Dim sFile As String, sheetName As String, colName As String, rs As ADODB.Recordset
Dim cols As Collection, col
sFile = ThisWorkbook.FullName
Set cols = FindColumns(sFile, Array("Ref", "Reference", "RefNo"))
'loop found columns
For Each col In cols
sheetName = col(0)
colName = col(1)
Debug.Print "##", sheetName, colName
Set rs = WorkBookQuery(sFile, "Select [" & colName & "] from [" & sheetName & "]")
If Not rs.EOF Then
' ActiveSheet.Cells(Rows.Count, "A").End(xlUp).CopyFromRecordset rs
End If
Next col
End Sub
'given a workbook path, find all column headings matching andname in arrNames
'returns a collections of [sheetName, columnName] arrays
Function FindColumns(wbFullPath As String, arrNames) As Collection
Dim tabledefs As Object, td As Object, f As Object, rv As New Collection
Set tabledefs = CreateObject("DAO.DBEngine.120") _
.OpenDatabase(wbFullPath, False, True, "Excel 12.0 XMl;").tabledefs
For Each td In tabledefs
For Each f In td.Fields
'Debug.Print td.Name, f.Name
If Not IsError(Application.Match(f.Name, arrNames, 0)) Then
rv.Add Array(td.Name, f.Name)
End If
Next f
Next td
Set FindColumns = rv
End Function
'run a SQL query against a workbook
Function WorkBookQuery(wbFullPath As String, SQL As String) As ADODB.Recordset
Dim rs As ADODB.Recordset
With New ADODB.Connection
.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & wbFullPath & "';" & _
"Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"";"
Set WorkBookQuery = .Execute(SQL, Options:=1)
End With
End Function
There seems to be a logical error in the process of cycling through the fields. It would be nice to use a user-defined function that checks if the field name exists.
Sub ImportFromClosedWorkbook()
Dim e, ws As Worksheet, cn As ADODB.Connection, rs As ADODB.Recordset, rsHeaders As ADODB.Recordset, b As Boolean, sFile As String, shName As String, strSQL As String, iCol As Long
Dim sField As String
sFile = ThisWorkbook.Path & "\Sample.xlsx"
Dim con As Object
Set con = CreateObject("DAO.DBEngine.120")
Dim rsData As ADODB.Recordset
Set cn = New ADODB.Connection
cn.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & sFile & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"";"
Set ws = ThisWorkbook.ActiveSheet
Dim db As Object, i As Long
Set db = con.OpenDatabase(sFile, False, True, "Excel 12.0 XMl;")
For i = 0 To db.TableDefs.Count - 1
sName = db.TableDefs(i).Name
b = False
strSQL = "SELECT * FROM [" & sName & "]"
Set rsHeaders = New ADODB.Recordset
rsHeaders.Open Source:=strSQL, ActiveConnection:=cn, Options:=1
For iCol = 0 To rsHeaders.Fields.Count - 1
' For Each e In Array("Ref No", "Reference", "Number")
' If e = rsHeaders.Fields(iCol).Name Then
' b = True: Exit For
' End If
' Next e
' If b Then Exit For
' Next iCol
' If b Then
sField = rsHeaders.Fields(iCol).Name
If isField(sField) Then
strSQL = "SELECT [" & sField & "] FROM [" & sName & "]"
Set rsData = New ADODB.Recordset
Set rsData = cn.Execute(strSQL)
ws.Range("A" & ws.Cells(Rows.Count, 1).End(xlUp).Row + 1).CopyFromRecordset rsData
rsData.Close
End If
Next iCol
Next i
db.Close: Set db = Nothing
Set con = Nothing
cn.Close: Set cn = Nothing
End Sub
Function isField(sField As String) As Boolean
Dim vName As Variant, e As Variant
vName = Array("Ref No", "Reference", "Number")
For Each e In vName
If e = sField Then
isField = True
Exit Function
End If
Next e
End Function
If all the files have the same structure and are in a folder, you could use the FileSystemObject reference as below:
"https://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba"
and you could run the existing code in a loop in the file system code, hope that works

Dynamic query call and connection string

Sub SavedConfiguration()
Dim cnn1 As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim iCols As Integer
Const TABLE_NAME = "TableName"
Const DRIVER = "{SQL Server}"
Dim conf
conf = Array("D11", "database", "D12", "server")
Dim wb As Workbook, wsConfig As Worksheet, wsOut As Worksheet
Dim i As Integer, strConn As String, sQry As String
Set wb = ThisWorkbook
Set wsConfig = Worksheets("Settings")
Set wsOut = Worksheets("SavedConfig")
' construct connection string
strConn = "driver=" & DRIVER & ";"
For i = 1 To UBound(conf) Step 2
strConn = strConn & conf(i) & "=" & wsConfig.Range(conf(i - 1)).Value & ";"
Next
' Debug.Print strConn
sQry = Worksheets("SQL-COMMON").Range("B3").Value
' connect to db and run sql
Set cnnl = New ADODB.Connection
cnnl.ConnectionString = strConn
cnnl.ConnectionTimeout = 30
cnnl.Open
wsConfig.Range("H35") = Now
' output
mrs.Open sQry, cnn1
For iCols = 0 To mrs.Fields.Count - 1
wsOut.Cells(1, iCols + 1).Value = mrs.Fields(iCols).Name
Next
Worksheets("SavedConfig").Range("A2").CopyFromRecordset mrs
wsConfig.Range("D35") = Now
mrs.Close
cnn1.Close
End Sub
I have edited the code, am getting unspecified error while executing it
How to make the connection string more dynamic instead of hard coding above, and picking up the values from cells of the worksheet of excel.
How to add the code for showing the Last run start time and date and last run end time and date of the query which is being executed, getting tabulated automatically in the excel cells.
Build the connection string by concatenating each parameter.
Sub Conn2SQL()
Const TABLE_NAME = "TableName"
Const DRIVER = "{SQL Server}"
Dim conf
conf = Array("A12", "database", "A13", "server", _
"A14", "password", "A15", "uid")
Dim wb As Workbook, wsConfig As Worksheet, wsOut As Worksheet
Dim i As Integer, strConn As String, sQry As String
Set wb = ThisWorkbook
Set wsConfig = wb.Sheets("Sheet1")
Set wsOut = wb.Sheets("SavedConfig")
' construct connection string
strConn = "driver=" & DRIVER & ";"
For i = 1 To UBound(conf) Step 2
strConn = strConn & conf(i) & "=" & wsConfig.Range(conf(i - 1)).Value & ";"
Next
'Debug.Print strConn
sQry = "SELECT * FROM [" & TABLE_NAME & "]"
' connect to db and run sql
Dim cnn1 As New ADODB.Connection, mrs As New ADODB.Recordset
Dim iCols As Integer
With cnn1
.ConnectionString = strConn
.ConnectionTimeout = 30
.Open
End With
wsConfig.Range("H35") = "LAST RUN STARTED " & Now
cnn1.Execute "USE " & wsConfig.Range(conf(0))
' output
mrs.Open sQry, cnn1
For iCols = 0 To mrs.Fields.Count - 1
wsOut.Cells(1, iCols + 1).Value = mrs.Fields(iCols).Name
Next
wsOut.Cells(2, 1).CopyFromRecordset mrs 'A2
wsConfig.Range("D35") = "LAST RUN COMPLETED " & Now
i = wsOut.Cells(Rows.Count, 1).End(xlUp).Row
MsgBox i & " rows added to " & wsOut.Name, vbInformation
mrs.Close
cnn1.Close
End Sub
Test the connection string with this code
Sub testconnect()
Dim conn As New ADODB.Connection, rs As New ADODB.Recordset, s As String
s = "" ' put connection string here
With conn
.ConnectionString = s
.ConnectionTimeout = 30
.Open
End With
Set rs = conn.Execute("SELECT CURRENT_TIMESTAMP")
MsgBox "Time is " & rs(0), vbOKOnly
conn.Close
End Sub

space in the name of sql table

the macro below exports data to SQL tables, when the name of the table has no space, it works perfectly, but if the name has a space, it blocks,
it blocks on the rs.Open line tableName, Cn, adOpenKeyset, adLockOptimistic
I tried the hooks [...] the apostrophe '...' and ... but nothing works
`
Sub Injection()
Dim Cn As ADODB.connection
Dim ServerName As String
Dim DatabaseName As String
Dim tableName As String
Dim UserID As String
Dim Password As String
Dim rs As ADODB.recordset
ServerName = "vmalsdisdb"
DatabaseName = "Produits"
tableName = "PRELEVEMENT PRODUIT"
UserID = ""
Password = ""
Set rs = New ADODB.recordset
Set Cn = New ADODB.connection
Cn.Open "Driver={SQL Server};Server=" & ServerName & ";Database=" & DatabaseName & _
";Uid=" & UserID & ";Pwd=" & Password & ";"
rs.Open tableName, Cn, adOpenKeyset, adLockOptimistic`
That's weird! It works fine for me in my test scenario!
Sub ADOExcelSQLServer()
'
' FOR THIS CODE TO WORK
' In VBE you need to go Tools References and check Microsoft Active X Data Objects 2.x library
'
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Server_Name = "EXCEL-PC\SQLEXPRESS" ' Enter your server name here
Database_Name = "Test Method" ' Enter your database name here
User_ID = "" ' enter your user ID here
Password = "" ' Enter your password here
SQLStr = "SELECT * FROM [mytable]" ' Enter your SQL here
Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
For iCols = 0 To rs.Fields.Count - 1
Worksheets("Sheet1").Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next
With Worksheets("sheet1").Range("a2:z500") ' Enter your sheet name and range here
'.ClearContents
.CopyFromRecordset rs
End With
' Tidy up
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub
I suppose you can try triple quotes around any 'weird' naming conventions, like spaces in names, or special characters in names.
thank you all for your answers, the problem was not worry in space but in one of the Tables, I was able to bypass the problem by replacing the cursor type "adOpenKeyset" by "adOpenDynamic"
but his understanding the problem, may be that another user worked on the table ?!

VBA get sql result using column index from CSV Files

using vba macro I used the below query and I can able to get the results
strsQL = "SELECT name,address, balance1,balance2,balance3 FROM userInfo.csv"
if there any possibility to get the sql results using column index , instead of using column name (address) in VBA macro
Try it this way.
Sub GetMyCSVData()
Dim xlcon As ADODB.Connection
Dim xlrs As ADODB.Recordset
Set xlcon = New ADODB.Connection
Set xlrs = New ADODB.Recordset
Dim currentDataFilePath As String
Dim currentDataFileName As String
Dim nextRow As Integer
currentDataFilePath = "C:\My Data Folder\"
currentDataFileName = "My Data File"
xlcon.Provider = "Microsoft.Jet.OLEDB.4.0"
xlcon.ConnectionString = "Data Source=" & currentDataFilePath & ";" & "Extended Properties=""text;HDR=Yes;FMT=Delimited;"""
xlcon.Open
xlrs.Open "SELECT FirstName, Surname, Age FROM [" & currentDataFileName & ".csv] WHERE SomeNumber > 10", xlcon
xlrs.MoveFirst
nextRow = Worksheets("Sheet1").UsedRange.Rows.Count + 1
Worksheets("Sheet1").Cells(nextRow, 1).CopyFromRecordset xlrs
xlrs.Close
xlcon.Close
Set xlrs = Nothing
Set xlcon = Nothing
End Sub

Always getting "connection failed" connection sqlserver using VBA

I am trying to connection sqlserver from VBA program, I refer to the following code to achieve it, but I
got the problem: connection failed. Any help. Thank you in advance.
Code:
Sub ADOExcelSQLServer()
' Carl SQL Server Connection
'
' FOR THIS CODE TO WORK
' In VBE you need to go Tools References and check Microsoft Active X
'Data Objects 2.x library
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Server_Name = "EXCEL-PC\EXCELDEVELOPER" ' Enter your server name here
Database_Name = "AdventureWorksLT2012" ' Enter your database name here
User_ID = "" ' enter your user ID here
Password = "" ' Enter your password here
SQLStr = "SELECT * FROM [SalesLT].[Customer]" ' Enter your SQL here
Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
With Worksheets("sheet1").Range("a1:z500") ' Enter your sheet name and range here
.ClearContents
.CopyFromRecordset rs
End With
' Tidy up
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub
test this.
Sub cn()
Dim con As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rst As New ADODB.Recordset
Dim i As Integer
con.ConnectionString = "Provider=SQLOLEDB.1;" _
& "Server=(local);" _
& "Database=AdventureWorksLT2012;" _
& "Integrated Security=SSPI;" _
& "DataTypeCompatibility=80;"
con.Open
Set cmd.ActiveConnection = con
cmd.CommandText = "SELECT * FROM [SalesLT].[Customer]"
Set rst = cmd.Execute
Range("A1").CopyFromRecordset rst
con.Close
Set con = Nothing
End Sub
I checked your code and it works for me. So I think it should be a SQL configuration problem e.g. permissions or port issue. You need to test connection status by using sqlserver client and check related connection problem.

Resources