Run Time 91 error on load

Jat999

New Member
Joined
May 7, 2016
Messages
49
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.

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
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top