Vba 如何返回特定整数的值?

Vba 如何返回特定整数的值?,vba,Vba,我想知道如何在我的函数中返回intcon和intRed的值: Function CountInterface(parI As String, parC As String) As String ' mes variables Dim strSQLQueryCountingI As String Dim intCon As Integer Dim intRed As Integer Dim rstCountInt As DAO.Recordset

我想知道如何在我的函数中返回
intcon
intRed
的值:

Function CountInterface(parI As String, parC As String) As String
' mes variables
    Dim strSQLQueryCountingI As String
    Dim intCon As Integer
    Dim intRed As Integer
    Dim rstCountInt As DAO.Recordset
    
    strSQLQueryCountInterface = "SELECT " _
                                & "CcProd.F_Cpr," _
                                & "CcProd.F_Int," _
                                & "Sum(CcProd.F_Re) AS Total, " _
                                & "Sum(CcProd.F_Con) As ConPost " _
                            & "FROM " _
                                & "CcProd " _
                            & "GROUP BY " _
                                 & "CcProd.F_Cpr, " _
                                & "CcProd.F_Int" _
                            & "HAVING " _
                                & "(((CcProd.F_Int) Like '" & parI & "')" _
                                & " AND ((CcProd.F_Cpr) Like '" & parC & "' )" _
                                & ") "
                               
   
     Set rstCountInt = CurrentDb.OpenRecordset(strSQLQueryCountInterface, dbOpenDynaset)
        
            intCon = CInt(rstCountInt !Connected_Post)
            Debug.Print intCon
        
            intRed = CInt(rstCountInterface!Total_Post)
            Debug.Print intRed

End Function

您可以使用自定义的
类型
对象。例如:

Function CountInterface(ByVal parInterface As String, _
                        ByVal parCcProductId As String, _
                        ByRef outConnect As Integer, _
                        ByRef outRegistered As Integer) As String

    '...
    
    outConnect = 365
    outRegistered = 400
    
End Function
类型:

Public Type InterfaceType
    Connected As Integer
    Registered As Integer
End Type
返回上述类型的方法:

Function ReturnInterfaceType() As InterfaceType

    Dim t As InterfaceType
    
    t.Connected = 365
    t.Registered = 400
    
    ReturnInterfaceType = t
End Function
称之为:

Sub T()

    Dim t As InterfaceType
    
    t = ReturnInterfaceType()
    
    Debug.Print t.Connected
    Debug.Print t.Registered
    
    '365
    '400
End Sub
Sub T()

    Dim intConnect As Integer, intRegistered As Integer, s As String
    
    s = CountInterface("a", "b", intConnect, intRegistered)
    
    Debug.Print intConnect
    Debug.Print intRegistered
    
    '365
    '400
End Sub

要返回
ByRef
,需要更新方法签名以接受这两个变量。由于它们是通过引用传递的,因此函数可以更改它们的值

例如:

Function CountInterface(ByVal parInterface As String, _
                        ByVal parCcProductId As String, _
                        ByRef outConnect As Integer, _
                        ByRef outRegistered As Integer) As String

    '...
    
    outConnect = 365
    outRegistered = 400
    
End Function
称之为:

Sub T()

    Dim t As InterfaceType
    
    t = ReturnInterfaceType()
    
    Debug.Print t.Connected
    Debug.Print t.Registered
    
    '365
    '400
End Sub
Sub T()

    Dim intConnect As Integer, intRegistered As Integer, s As String
    
    s = CountInterface("a", "b", intConnect, intRegistered)
    
    Debug.Print intConnect
    Debug.Print intRegistered
    
    '365
    '400
End Sub

您可以使用自定义的
类型
对象。例如:

Function CountInterface(ByVal parInterface As String, _
                        ByVal parCcProductId As String, _
                        ByRef outConnect As Integer, _
                        ByRef outRegistered As Integer) As String

    '...
    
    outConnect = 365
    outRegistered = 400
    
End Function
类型:

Public Type InterfaceType
    Connected As Integer
    Registered As Integer
End Type
返回上述类型的方法:

Function ReturnInterfaceType() As InterfaceType

    Dim t As InterfaceType
    
    t.Connected = 365
    t.Registered = 400
    
    ReturnInterfaceType = t
End Function
称之为:

Sub T()

    Dim t As InterfaceType
    
    t = ReturnInterfaceType()
    
    Debug.Print t.Connected
    Debug.Print t.Registered
    
    '365
    '400
End Sub
Sub T()

    Dim intConnect As Integer, intRegistered As Integer, s As String
    
    s = CountInterface("a", "b", intConnect, intRegistered)
    
    Debug.Print intConnect
    Debug.Print intRegistered
    
    '365
    '400
End Sub

要返回
ByRef
,需要更新方法签名以接受这两个变量。由于它们是通过引用传递的,因此函数可以更改它们的值

例如:

Function CountInterface(ByVal parInterface As String, _
                        ByVal parCcProductId As String, _
                        ByRef outConnect As Integer, _
                        ByRef outRegistered As Integer) As String

    '...
    
    outConnect = 365
    outRegistered = 400
    
End Function
称之为:

Sub T()

    Dim t As InterfaceType
    
    t = ReturnInterfaceType()
    
    Debug.Print t.Connected
    Debug.Print t.Registered
    
    '365
    '400
End Sub
Sub T()

    Dim intConnect As Integer, intRegistered As Integer, s As String
    
    s = CountInterface("a", "b", intConnect, intRegistered)
    
    Debug.Print intConnect
    Debug.Print intRegistered
    
    '365
    '400
End Sub

可以将它们作为数组返回

Function CountInterface(parInterface As String, parCcProductId As String) As Variant
' mes variables
Dim strSQLQueryCountInterface As String
Dim intConnect As Integer
Dim intRegistered As Integer
Dim rstCountInterface As DAO.Recordset

    strSQLQueryCountInterface = "SELECT " _
                                & "CcProductid_details.F_Cc_Product_Id," _
                                & "CcProductid_details.F_Interface," _
                                & "Sum(CcProductid_details.F_Registered) AS Total_Post, " _
                                & "Sum(CcProductid_details.F_Connected) As Connected_Post " _
                                & "FROM " _
                                & "CcProductid_details " _
                                & "GROUP BY " _
                                & "CcProductid_details.F_Cc_Product_Id, " _
                                & "CcProductid_details.F_Interface " _
                                & "HAVING " _
                                & "(((CcProductid_details.F_Interface) Like '" & parInterface & "')" _
                                & " AND ((CcProductid_details.F_Cc_Product_Id) Like '" & parCcProductId & "' )" _
                                & ") "


    Set rstCountInterface = CurrentDb.OpenRecordset(strSQLQueryCountInterface, dbOpenDynaset)

    intConnect = CInt(rstCountInterface!Connected_Post)
    Debug.Print intConnect

    intRegistered = CInt(rstCountInterface!Total_Post)
    Debug.Print intRegistered

    CountInterface = Array(intConnect, intRegistered)

End Function
或者一本字典

Function CountInterface(parInterface As String, parCcProductId As String) As Variant
' mes variables
Dim strSQLQueryCountInterface As String
Dim intConnect As Integer
Dim intRegistered As Integer
Dim rstCountInterface As DAO.Recordset
Dim dicCounts As Object

    strSQLQueryCountInterface = "SELECT " _
                                & "CcProductid_details.F_Cc_Product_Id," _
                                & "CcProductid_details.F_Interface," _
                                & "Sum(CcProductid_details.F_Registered) AS Total_Post, " _
                                & "Sum(CcProductid_details.F_Connected) As Connected_Post " _
                                & "FROM " _
                                & "CcProductid_details " _
                                & "GROUP BY " _
                                & "CcProductid_details.F_Cc_Product_Id, " _
                                & "CcProductid_details.F_Interface " _
                                & "HAVING " _
                                & "(((CcProductid_details.F_Interface) Like '" & parInterface & "')" _
                                & " AND ((CcProductid_details.F_Cc_Product_Id) Like '" & parCcProductId & "' )" _
                                & ") "


    Set rstCountInterface = CurrentDb.OpenRecordset(strSQLQueryCountInterface, dbOpenDynaset)

    intConnect = CInt(rstCountInterface!Connected_Post)

    intRegistered = CInt(rstCountInterface!Total_Post)

    Set dicCounts = CreateObject("Scripting.Dictionary")

    dicCounts("Connect") = intConnect
    dicCounts("Registered") = intRegistered

    Set CountInterface = dicCounts

End Function

可以将它们作为数组返回

Function CountInterface(parInterface As String, parCcProductId As String) As Variant
' mes variables
Dim strSQLQueryCountInterface As String
Dim intConnect As Integer
Dim intRegistered As Integer
Dim rstCountInterface As DAO.Recordset

    strSQLQueryCountInterface = "SELECT " _
                                & "CcProductid_details.F_Cc_Product_Id," _
                                & "CcProductid_details.F_Interface," _
                                & "Sum(CcProductid_details.F_Registered) AS Total_Post, " _
                                & "Sum(CcProductid_details.F_Connected) As Connected_Post " _
                                & "FROM " _
                                & "CcProductid_details " _
                                & "GROUP BY " _
                                & "CcProductid_details.F_Cc_Product_Id, " _
                                & "CcProductid_details.F_Interface " _
                                & "HAVING " _
                                & "(((CcProductid_details.F_Interface) Like '" & parInterface & "')" _
                                & " AND ((CcProductid_details.F_Cc_Product_Id) Like '" & parCcProductId & "' )" _
                                & ") "


    Set rstCountInterface = CurrentDb.OpenRecordset(strSQLQueryCountInterface, dbOpenDynaset)

    intConnect = CInt(rstCountInterface!Connected_Post)
    Debug.Print intConnect

    intRegistered = CInt(rstCountInterface!Total_Post)
    Debug.Print intRegistered

    CountInterface = Array(intConnect, intRegistered)

End Function
或者一本字典

Function CountInterface(parInterface As String, parCcProductId As String) As Variant
' mes variables
Dim strSQLQueryCountInterface As String
Dim intConnect As Integer
Dim intRegistered As Integer
Dim rstCountInterface As DAO.Recordset
Dim dicCounts As Object

    strSQLQueryCountInterface = "SELECT " _
                                & "CcProductid_details.F_Cc_Product_Id," _
                                & "CcProductid_details.F_Interface," _
                                & "Sum(CcProductid_details.F_Registered) AS Total_Post, " _
                                & "Sum(CcProductid_details.F_Connected) As Connected_Post " _
                                & "FROM " _
                                & "CcProductid_details " _
                                & "GROUP BY " _
                                & "CcProductid_details.F_Cc_Product_Id, " _
                                & "CcProductid_details.F_Interface " _
                                & "HAVING " _
                                & "(((CcProductid_details.F_Interface) Like '" & parInterface & "')" _
                                & " AND ((CcProductid_details.F_Cc_Product_Id) Like '" & parCcProductId & "' )" _
                                & ") "


    Set rstCountInterface = CurrentDb.OpenRecordset(strSQLQueryCountInterface, dbOpenDynaset)

    intConnect = CInt(rstCountInterface!Connected_Post)

    intRegistered = CInt(rstCountInterface!Total_Post)

    Set dicCounts = CreateObject("Scripting.Dictionary")

    dicCounts("Connect") = intConnect
    dicCounts("Registered") = intRegistered

    Set CountInterface = dicCounts

End Function

您想返回整数还是包含两个整数的字符串?您想将值返回到哪里?我想返回整数值,并希望在我正在执行的另一个子函数中调用它们时返回它们。您可以创建自定义类型或类,并让您的函数返回该值,或者将这两个参数作为
ByRef
parameters.ByRef参数传递BigBen 9分钟前,你能举个例子吗?当它们被声明为参数时,我如何在函数中返回值?你想返回整数还是包含两个整数的字符串?你想将值返回到哪里?我想返回整数值,并希望在另一个子函数中调用它们时返回它们这样,您可以创建自定义类型或类并让函数返回该类型或类,也可以作为
ByRef
parameters.ByRef参数传递这两者BigBen 9分钟前,您能否给出一个示例,当它们被声明为参数时,我如何在函数中返回值当值已传入参数(byRef)时我如何返回它请参见上面的更新。当值已传入参数(byRef)时我如何返回它请参见上面的更新。