简体   繁体   中英

Pulling data from SQL SP in Excel spreadsheet using VBA

This is an odd one, and I apologise beforehand - I have the below VBA Code in an ActiveX button called 'Refresh':

Sub Refresh_Click()

With ActiveWorkbook.Connections("Lender Pipeline").OLEDBConnection
.CommandText = "EXECUTE dbo.sp_Lender_Pipeline " & Range("Q2").Value
End With

ActiveWorkbook.Connections("Lender Pipeline").Refresh

End Sub

What it basically does, is pull data from an SQL SP based on the value of the Q2 cell, and refresh the SP table based on that value - which is a number I manually add in the cell. This number is typically between 1 and 120 (based on IDs within the SP).

The problem:

  • It seems to only pull in the data when inputting certain numbers. ie 29
  • For other numbers, I have to manually click 'Refresh All'.
  • If I execute the SP manually in SQL Server Management, it works perfectly fine, so the problem seems to lie within the VBA.
  • If I manually execute the SP from within the Excel spreadsheet, it also works fine...

Does anyone have a clue as to why this may happen? Could it be the VBA, or something else? Let me know if further clarification is needed.

Thank-you.

Oddly enough a colleague of mine had this same issue before. Tried an assortment of fixes and none appeared to work but rebuilding the workbook in the exact same manner seemed to correct it.

Not the best solution, but it has worked in the past. If not, try using VBA to call the refreshall command once changing the command text and see if that helps.

非常感谢您的建议-似乎“连接属性”中禁用后台刷新起到了作用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM