如何从函数返回值

时间:2012-01-03 10:10:46

标签: vb6 return-value

如何从函数

返回值

代码

Private Function LeaveCheck(empid As String)
    Dim rdoRs1 As rdoResultset
    Dim desc As String
    Dim sSQL As String
    sSQL = "Select name from table1 wher empcode = '" & empid & "'"
    Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)
    If rdoRs1.RowCount > 0 Then
    desc = rdors1!name        
    return desc 'Showing error in this line    
    End If
    rdoRs1.Close
End Function

如何从上面的代码中返回值。

需要Vb6代码帮助

3 个答案:

答案 0 :(得分:13)

您需要指定退货类型。

Private Function LeaveCheck(empid As String) As String ' Notice the As String
    Dim rdoRs1 As rdoResultset  
    Dim desc As String  
    Dim sSQL As String  
    sSQL = "Select name from table1 wher empcode = '" & empid & "'"  
    Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)  
    If rdoRs1.RowCount > 0 Then  
        desc = rdors1!name          
    End If  
    rdoRs1.Close  

    LeaveCheck = desc ' This will be blank or populated
End Function

这是一个link,对于理解VB6中的函数是一个很好的阅读

修改

阅读完评论后,我会创建一个类来存储您的值。

Public Class MyClass
    Dim name As String
    Dim dept As String
    Dim country As String
End Class

然后,您可以在代码中实例化此类的新实例:

Private Function LeaveCheck(empid As String) As MyClass
    Dim myClass As New MyClass
    Dim rdoRs1 As rdoResultset
    Dim sSQL As String   
    sSQL = "Select name, dept, country from table1 wher empcode = '" & empid & "'"   
    Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)   
    If rdoRs1.RowCount > 0 Then   
        myClass.name = rdors1!name           
        myClass.dept = rdors1!dept
        myClass.country = rdors1!country
    End If   
    rdoRs1.Close   

    LeaveCheck = myClass
End Function

答案 1 :(得分:3)

您需要将值设置为函数名称和返回类型:

Private Function LeaveCheck(empid As String) As String
    Dim rdoRs1 As rdoResultset
    Dim desc As String
    Dim sSQL As String
    sSQL = "Select name from table1 wher empcode = '" & empid & "'"
    Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)
    If rdoRs1.RowCount > 0 Then
      desc = rdors1!name        
    End If
    rdoRs1.Close

    LeaveCheck = desc

End Function

有关详细信息,请参阅this document

答案 2 :(得分:0)

Function returnArray() As Variant
   RTA[ab]=0
   if a=b then RTA[ab]=1

   RTA[xy]=0
   if a=b then RTA[xy]=1

   returnArray=RTA
end function