我想在 public 子中创建一个 if 语句以根据按钮单击打开特定查询

I want to create an if statement in a public sub to open a specific query based on button click

我想合并我的 VBA 以便更容易管理和查看而不是多次复制它。 然后我想在 sub 中写一个 if 语句,根据我点击的按钮打开记录集 有4个按钮 cmdAllSuppliers cmdActive cmdInactive cmdArrangments 每个按钮都有调用子 EmailQuery 和错误处理的 VBA。子代码如下

Sub EmailQuery()

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim strEmail As String
    Dim strQryAll As String
    Dim strQryActive As String
    Dim strQryInactive As String
    Dim strQryArrangement As String

    Set cn = CurrentProject.Connection
    Set rs = New ADODB.Recordset

    strQryAll = "qryAllSuppliers"
    strQryActive = "qryActiveSuppliers"
    strQryInactive = "qryInactiveSuppliers"
    strQryArrangement = "qryAgreementEmail"

    rs.Open strQryAll, cn
    rs.Open strQryActive, cn
    rs.Open strQryInactive, cn
    rs.Open strQryArrangement, cn

    With rs
        Do While Not .EOF
            strEmail = strEmail & .Fields("Email") & ";"
            .MoveNext
        Loop
        .Close
    End With
    
    strEmail = Left(strEmail, Len(strEmail) - 1)
    
    DoCmd.SendObject , , , , , strEmail, , , True, False

End Sub

rs.open 显然不会像现在这样工作,但我希望 if 语句根据我按下的按钮打开这四个按钮之一。

Sub EmailQuery(strQueryName as string)

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim strEmail As String
   
    Set cn = CurrentProject.Connection
    Set rs = New ADODB.Recordset
    rs.Open strQueryName, cn
    With rs
   .movelast 
   .movefirst


        Do While Not .EOF
            strEmail = strEmail & .Fields("Email") & ";"
            .MoveNext
        Loop
        .Close
    End With
    
    strEmail = Left(strEmail, Len(strEmail) - 1)
    
    DoCmd.SendObject , , , , , strEmail, , , True, False

End Sub
'EmailQuery is a sub so no () around parameters
Private Sub cmdAllSuppliers_Click()
EmailQuery strQryAll 
End Sub