所以,我有一个宏LoadData
,我希望在两个单独的宏TransmitNewAndOldRecords
和refresh
中调用这些宏,然后从自定义标签上的按钮调用这些宏Excel功能区。
TransmitNewAndOldRecords
工作正常。这是它的代码。您可以在宏的倒数第二行看到该调用。
Sub TransmitNewAndOldRecords(control As IRibbonControl)
'Reference Microsoft ActiveX Data Objects 6.1 Library
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim pgconn As String
Dim Year As String
Dim SQLDelete As String
Dim SQLInsert As String
Set conn = New ADODB.Connection
Set cmd = New ADODB.Command
Year = ActiveWorkbook.Sheets("SQL Creator").Range("Year").Value
pgconn = "Driver={PostgreSQL};" & _
"Server = me;" & _
"Port = 1234;" & _
"Database = database;" & _
"Uid = me;" & _
"Pwd = me;"
conn.ConnectionString = pgconn
conn.Open "PostgreSQL35W", "me", "me"
cmd.CommandType = adCmdText
cmd.ActiveConnection = conn
'Tests if there are any records to delete. If so, define SQLDelete variable and execute SQL string that is assigned to SQLDelete variable.
If WorksheetFunction.CountIf(Range("DELETE"), "Y") <> 0 Then
SQLDelete = DeleteRecords(ActiveWorkbook.Sheets("Budget").Range("DELETE"), Year)
cmd.CommandText = SQLDelete
cmd.Execute
End If
'Tests if there are any records to insert. If so, define SQLInsert variable and execute SQL string that is assgined to SQLInsert variable.
If WorksheetFunction.CountIf(Range("INSERT"), "Y") <> 0 Then
SQLInsert = InsertRecords(ActiveWorkbook.Sheets("Budget").Range("INSERT"), Year)
cmd.CommandText = SQLInsert
cmd.Execute
End If
conn.Close
'If there are no new/old records to insert/delete, then display message box stating that. IOF
If WorksheetFunction.CountIf(Range("DELETE"), "Y") = 0 And WorksheetFunction.CountIf(Range("INSERT"), "Y") = 0 Then
MsgBox "No data to insert/delete into/from table.", vbOKOnly
Else
MsgBox "Data saved successfully!", vbOKOnly
End If
Set conn = Nothing
Set cmd = Nothing
Call LoadData
End Sub
我还在一个非常简单的宏LoadData
(见下文)中调用refresh
宏,只调用LoadData
。我运行refresh
时收到错误(参数数量错误或属性分配无效)。但是,如果我将“control as iRibbonControl”参数添加到LoadData
,则从refresh
调用时它可以正常工作,但我在TransmitNewAndOldRecords
中收到“Argument not optional”错误调用LoadData
时的宏。
Sub refresh(control As IRibbonControl)
Call LoadData
End Sub
我真的很困惑,因为如果我将“control as IRibbonControl”参数添加到LoadData
,那么refresh
宏会起作用,但TransmitNewAndOldRecords
宏会出错。但是,如果我没有将“control as IRibbonControl”参数添加到LoadData
,则refresh
宏错误输出和TransmitNewAndOldRecords工作。
有人知道为什么会这样吗?
以下是LoadData宏,以防需要:
Sub LoadData()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim pgconn As String
Dim Team As Range
Dim FC As Range
Dim SQL As String
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
Set Team = ActiveWorkbook.Sheets("Info").Range("Info_Team")
Set FC = ActiveWorkbook.Sheets("Info").Range("Info_FundingCategory")
SQL = ActiveWorkbook.Sheets("SQL Creator").Range("BudgetSQL")
pgconn = "Driver={PostgreSQL};" & _
"Server = me;" & _
"Port = 1234;" & _
"Database = database;" & _
"Uid = me;" & _
"Pwd = me;"
conn.ConnectionString = pgconn
conn.Open "PostgreSQL35W", "me", "me"
cmd.CommandType = adCmdText
cmd.ActiveConnection = conn
cmd.CommandText = SQL
rs.Open cmd
ActiveWorkbook.Sheets("Budget").Range("A11:Y500").ClearContents
ActiveWorkbook.Sheets("Budget").Range("A11").CopyFromRecordset rs
rs.Close
conn.Close
End Sub