vba动态搜索存储过程mysql

时间:2016-10-21 09:06:45

标签: mysql excel vba excel-vba stored-procedures

我有一个存储过程,在给定输入参数的情况下从MySQL表中检索记录。例如,有人将山东作为unit_state,将中国作为p_country,该程序将产生该地区的所有公司。

我通过VBA在excel中调用此存储过程。我有一个电子表格,个人可以输入任何一个搜索条件,点击刷新搜索按钮,表格结果将填入表格中的表格。

存储过程100%正常运行。我的vba偶尔会工作,但它是零星的,我试图找出为什么它只会在点击按钮时有选择地刷新。

以下VBA:

Private Sub RefineryUnitSearchRefresh_Click()

Dim wsSearch As WorkbookConnection

Dim company_name As String  'Declare the company_name as string
Dim plant_name As String    'Declare the plant_name as string
Dim world_region As String  'Declare the world_region as string
Dim p_country As String     'Declare the p_country as string
Dim unit_state As String    'Declare the unit_state as string
Dim phys_city  As String    'Declare the phys_city as string
Dim unit_cd As String       'Declare the unit_cd as string

company_name = Worksheets("Searches").Range("B2").Value 'Pass value from B2 to company_name variable
plant_name = Worksheets("Searches").Range("B3").Value   'Pass value from B3 to plant_name variable
world_region = Worksheets("Searches").Range("B4").Value 'Pass value from B4 to world_region variable
p_country = Worksheets("Searches").Range("B5").Value    'Pass value from B5 to p_country variable
unit_state = Worksheets("Searches").Range("B6").Value   'Pass value from B6 to unit_state variable
phys_city = Worksheets("Searches").Range("B7").Value    'Pass value from B7 to phys_city variable
unit_cd = Worksheets("Searches").Range("B8").Value      'Pass value from B8 to unit_cd variable

'Pass the Parameters values to the Stored Procedure used in the Data Connection
With ActiveWorkbook.Connections("refinery_unit_search").ODBCConnection
'.CommandText = "Call warehouse.refinery_uni_search '" & company_name & "','" & plant_name & "','" & world_region & "','" & p_country & "','" & unit_state & "','" & phys_city & "','" & unit_cd & "'"
.CommandText = "CALL `warehouse`.`refinery_unit_search`('" & company_name & "','" & plant_name & "','" & world_region & "','" & p_country & "','" & unit_state & "','" & phys_city & "','" & unit_cd & "');"
'CALL `warehouse`.`refinery_unit_search`('','','','','','','');
ActiveWorkbook.Connections("refinery_unit_search").Refresh


End With
End Sub

我对VBA很新,如果有人能帮助我理解为什么它只在某些时候起作用,我会非常感激。谢谢!

0 个答案:

没有答案