Hi all
I have a file that I run an SQL connection to a database on load.
Whenever the file opens, I get the Run Time 91 error on load - Object variable or With block variable not set.
Yet I can end the macro and re-run it without any further error.
Thanks in advance
John
I have a file that I run an SQL connection to a database on load.
Whenever the file opens, I get the Run Time 91 error on load - Object variable or With block variable not set.
Yet I can end the macro and re-run it without any further error.
Code:
Private Sub Workbook_Open()
Call Load_Sap
End Sub
Sub Load_Sap()
'Fetch data from SAP
With ActiveWorkbook.Connections("Query from SAP_Live2").ODBCConnection
.CommandText = "SELECT T0.[ItemCode], T0.[ItemName], T0.[CodeBars], T0.[SalPackUn], T0.[U_TECSL],T0.[SWeight1] FROM OITM T0 WHERE T0.[ItmsGrpCod] = 105 and T0.[validFor] =('Y')"
.BackgroundQuery = False
.Refresh
End With
With ActiveWorkbook.Connections("Query from SAP_Live1").ODBCConnection
.CommandText = " SELECT T0.[DocNum], T0.[ItemCode], T0.[PlannedQty], T0.[DueDate] FROM OWOR T0 WHERE DateDiff(DD, T0.[DueDate], GetDate()) >-5 And DateDiff(DD, T0.[DueDate], GetDate()) < 5 AND (T0.[Status]='P') OR (T0.[Status]='R')"
.BackgroundQuery = False
.Refresh
End With
End Sub
Thanks in advance
John