I have been working on this for quite a while and I can't seem to figure it out. I am trying to create a query in excel that combines information from an excel table and database. I can do each of them separately no problem.
Here is the VBA code for the excel query:
Sub ExcelQuery()
'
Range("Table_Query_from_Excel_Files5[[#Headers],[Customer:]]").Select
With Selection.ListObject.QueryTable
.Connection = Array(Array("ODBC;DSN=Excel Files;DBQ=Z:\OEM Office\Trevor Weinrich\Projects\BOM Template 2.0\BOM template 2017-08-16 1.xlsm;DefaultDir=Z:\OEM Office\Trevor Weinrich\Projects\BOM Template 2.0;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"))
.CommandText = Array( _
"SELECT `BOM$`.`Customer:`" & Chr(13) & "" & Chr(10) & "FROM `BOM$` `BOM$`" & Chr(13) & "" & Chr(10) & "WHERE (`BOM$`.`Customer:` Is Not Null)" _
)
.Refresh BackgroundQuery:=False
End With
'
End Sub
And here is the VBA code for the database query:
Sub DatabaseQuery()
'
With Selection.ListObject.QueryTable
.Connection = _
"ODBC;DSN=OEM;Description=OEM;UID=trevor.weinrich;Trusted_Connection=Yes;APP=Microsoft Office 2016;WSID=DFP-OEM-0913-A;DATABASE=OEM"
.CommandText = Array( _
"SELECT DISTINCT p21_view_item_uom.item_id, p21_view_item_uom.unit_of_measure, p21_view_item_uom.purchasing_unit" & Chr(13) & "" & Chr(10) & "FROM OEM.dbo.p21_view_item_uom p21_view_item_uom" & Chr(13) & "" & Chr(10) & "WHERE (p21_view_item_uom.delete_flag=" _
, "'N')" & Chr(13) & "" & Chr(10) & "ORDER BY p21_view_item_uom.item_id")
.Refresh BackgroundQuery:=False
End With
'
End Sub
I want to join these together because there are about 140,000 line items in the database query, and I only care about the the instances where the "item_id" field from the database matches up with the "Customer:" field. I just can't figure out how to join the two of them. I would greatly appreciate the help.
Here is the code where I am just trying to pull in a variable that gives me the error after 165 characters:
Sub Update_Item_Tables()
'
' UOM_Update Macro
'
Dim Items As String
Items = Sheets("UOM").Range("K1").Value
Sheets("UOM").Visible = True
Sheets("UOM").Select
Range("Table_Query_from_OEM[[#Headers],[item_id]]").Select
With Selection.ListObject.QueryTable
.Connection = _
"ODBC;DSN=OEM;Description=OEM;UID=trevor.weinrich;Trusted_Connection=Yes;APP=Microsoft Office 2016;WSID=DFP-OEM-0913-A;DATABASE=OEM"
.CommandText = Array( _
"SELECT DISTINCT p21_view_item_uom.item_id, p21_view_item_uom.unit_of_measure, p21_view_item_uom.purchasing_unit" & Chr(13) & "" & Chr(10) & _
"FROM OEM.dbo.p21_view_item_uom p21_view_item_uom" & Chr(13) & "" & Chr(10) & _
"WHERE (p21_view_item_uom.item_id In (" _
, _
"" & Items & ")) AND (p21_view_item_uom.delete_flag='N')" & Chr(13) & "" & Chr(10) & _
"ORDER BY p21_view_item_uom.purchasing_unit DESC" _
)
.Refresh BackgroundQuery:=False
End With
End Sub