How to fix 'Automation Error' in vba when querying worksheets - sql

I am trying to join three tables to show inventory by location for similar Items associated by "Key".
So to do that, I am joining my product list, DS, to SM which is my master list with associated products on KEY, which is an index of different product information. Finally I need to pull each one of those item's inventory information by location.
It is throwing an "Automation error" from VBA when it is trying to establish the recordset. This works when I dont have the second join in my SQL string (attaching INV table).
Private Sub CommandButton1_Click()
Dim strFile
Dim strCon
Dim strSQL
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim I
Dim SQL
strSQL = "SELECT s1.KEY, s1.Item, Loc, QOH, Total_Ship_To_WIP, Tot_Opn+pick_Units, Reserve " _
& "FROM ([SM$] AS s1 " _
& "RIGHT JOIN [DS$] AS s2 ON s1.[KEY] = s2.[KEY]) " _
& "LEFT JOIN [INV$] AS s3 ON s2.[Item] = s3.[Full_Style]"
strFile = ActiveWorkbook.FullName
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
rs.Open strSQL, cn 'error
For I = 0 To rs.Fields.Count - 1
ActiveWorkbook.Sheets("Sheet1").Range("a10").Offset(0, I).Value = rs.Fields(I).Name
Next
ActiveWorkbook.Sheets("Sheet1").Range("a11").CopyFromRecordset rs
End Sub

Related

Excel VBA SQL multi JOIN, Multi file types

I've looked for awhile now and have found some useful tips for formatting but I still can't get my code to work. It works when I remove the second JOIN but gives a Parameter error with the second JOIN added.
My code is:
Sub MultiJoinQuery()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim csvPath As String
Dim xlsxPath As String
Dim con As String
Dim strSQL As String
Dim i As Long
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
csvPath = "C:\Users\Me\"
xlsxPath = "C:\Users\Somewhere Else\Book1.xlsx"
con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & xlsxPath & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes;"";"
strSQL = "SELECT * " _
& "FROM ([Sheet1$] a " _
& "LEFT JOIN [Text;FMT=Delimited;HDR=Yes;DATABASE=" & csvPath & "].users.csv b ON a.[Name]=b.[full_name]) " _
& "LEFT JOIN [Text;FMT=Delimited;HDR=Yes;DATABASE=" & csvPath & "].enrollments.csv c ON c.[user_id]=b.[user_id] "
rs.Open strSQL, con, 3, 3
For i = 0 To rs.Fields.Count - 1
Cells(1, i + 1).value = rs.Fields(i).Name
Next
Range("A2").CopyFromRecordset rs
''Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
With parentheses I get
Without parentheses, I get
Why do I keep getting a Param error? How can I make this code work?
Your help is appreciated.

Inner Join String & Integer Value both in 1 column in VBA

Please find my below table and VBA code.
table 1:-
table 2:-
Required Output
I am getting Type Mismatch in expression. error and i think it is because in table 2 String & Integer Value both are available.
Please find my below VBA Code and guide how can i execute inner join for the same.
Sub sql()
sql_string = "SELECT [Sheet1$].[Sr], [Name],[Sheet3$].[Names] FROM" & _
"[Sheet3$] INNER JOIN [Sheet1$] ON [Sheet1$].[Sr]=[Sheet3$].[Sr]"
sq = SQL_query(sql_string)
end sub
Function SQL_query(ByRef sql_string As Variant)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
strFile = ThisWorkbook.FullName
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = sql_string
rs.Open strSQL, cn ''Here i am getting error...
Sheet5.Range("A2").CopyFromRecordset rs
End Function
Change the sql query to ensure that both Sr are compared as strings
sql_string = "SELECT [Sheet1$].[Sr], [Sheet3$].[Names] FROM [Sheet3$] INNER JOIN [Sheet1$] ON CStr([Sheet1$].[Sr])=CStr([Sheet3$].[Sr])"

SQL ADODB Recordset select distinct is in alphabetical order

I'm using an ADODB record set in VBA. When I use Select Distinct in a SQL query of the ADODB record set the results come out in alphabetical order. I need the results in the order they are in the data. Is that possible?
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
DatPath = ThisWorkbook.Path & "\Temp\" & TB.Name
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatPath & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
cn.Open strCon
strSQL = "SELECT Distinct(Period) from [Data$] "
rs.Open strSQL, cn, 3, 3
ThisWorkbook.Sheets("ADO Out").Cells.Clear
ThisWorkbook.Sheets("ADO Out").Activate
ThisWorkbook.Sheets("ADO Out").Cells(1, 1).CopyFromRecordset rs
Here are two workarounds that both involve retrieving all Period records.
Range.RemoveDuplicates will remove the duplicates while preserving the order
Sub UnorderedPeriod()
Dim DatPath As String, strCon As String, strSQL As String
Dim cn As Object, rs As Object
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
DatPath = ThisWorkbook.Path & "\Temp\" & TB.Name
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatPath & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
cn.Open strCon
strSQL = "SELECT [Period] from [Data$]"
rs.Open strSQL, cn, 3, 3
With ThisWorkbook.Sheets("ADO Out")
.Cells.Clear
.Cells(1, 1).CopyFromRecordset rs
.Activate
.Columns(1).RemoveDuplicates Columns:=1
End With
End Sub
Use an ArrayList to remove the duplicates
Function CopyDistinctFromRecordset(rs As Object, Target As Range)
Dim list As Object
Dim data
Dim x As Long
rs.MoveFirst
data = rs.getRows
Set list = CreateObject("System.Collections.ArrayList")
For x = 0 To UBound(data, 2)
If Not list.Contains(data(0, x)) Then list.Add data(0, x)
Next
Target.Resize(list.Count).Value = Application.Transpose(list.ToArray)
End Function

How to LOAD data from recordset into separate columns?

I have created a macro which search for all employees who were on trainings this year.
I'm weak in SQL and I need somebodys help.
Currently my macro works like this that it creates 3 column in Excel and loads in it all the data
Name||First Name||TrainingName
Employee1.Name||Employee1.FirstName||TrainingName1
Employee1.Name||Employee1.FirstName||TrainingName2
Employee2.Name||Employee2.FirstName||TrainingName1
Employee3.Name||Employee3.FirstName||TrainingName1
I want that the data will be showed like this:
Name||First Name||TrainingName1||TrainingName2 etc..
Employee1.Name||Employee1.FirstName||TrainingName1||TrainingName2 etc..
How to change the SQL line to get what I want.
Sub TRaining()
Dim con As ADODB.connection
Dim rs As ADODB.Recordset
Dim path1 As String, SQLstr As String, conString As String, i
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "GDZIE JEST PLIK?"
.Show
path1 = .SelectedItems(.SelectedItems.Count)
End With
Set con = New ADODB.connection
conString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & path1 & ";" & _
"Extended Properties=Excel 12.0"
con.Open conString
SQLstr = "SELECT o.[Last Name] AS [LastName], o.[First Name] AS [FirstName], s.[TRAINING] AS [T0] FROM [pracownicy$] o LEFT JOIN [szkolenia$] s ON s.[GUID]=o.[GUID] WHERE s.[GUID] IS NOT NULL"
Set rs = New ADODB.Recordset
rs.Open SQLstr, con, adOpenUnspecified, adLockUnspecified
With ThisWorkbook.ActiveSheet
For i = 0 To rs.Fields.Count - 1
ActiveSheet.Cells(1, i + 1).Value = rs.Fields(i).Name
Next i
Range("A2").CopyFromRecordset rs
End With
End Sub
Thank you for your help!
Hi check this once i have some quires for you
INSERT INTO [TABLE]([Name, First Name, Training],[Employee1,Employee1,Training1],[Employee1,Employee1,Training 2],[Employee2,Employee 2,Training1],[Employee3,Employee 3,Training1])VALUES(#[Name, FirstName,Training] ,#[Employee1,Employee1,Training1] ,#[Employee1,Employee1,Training 2] ,#[Employee2,Employee 2,Training1] ,#[Employee3,Employee 3,Training1])

Tweaking a Excel Pivot Table to display a OrderNumber instead of a calculation?

Using Microsoft Excel 2007 (or 2002) is it possible to create pivot data like this?
Specifically I would like to know if I can display '01(Y 0)' as a non-calculated text value instead of just a SUM/COUNT/MAX/etc value.
With ADO
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer
''This is not the best way to refer to the workbook
''you want, but it is very convenient for notes
''It is probably best to use the name of the workbook.
strFile = ActiveWorkbook.FullName
''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used.
''This is the Jet 4 connection string, you can get more
''here : http://www.connectionstrings.com/excel
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
''Late binding, so no reference is needed
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "TRANSFORM First(t.[Order Number]) AS OrdNo " _
& "SELECT t.[Slot Number], t.Time " _
& "FROM [Sheet2$] t " _
& "GROUP BY t.[Slot Number], t.Time " _
& "PIVOT t.Company"
rs.Open strSQL, cn, 3, 3
''Pick a suitable empty worksheet for the results
With Worksheets("Sheet3")
For i = 1 To rs.Fields.Count
.Cells(1, i) = rs.Fields(i - 1).Name
Next
.Cells(2, 1).CopyFromRecordset rs
End With
''Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing