11

How to return the value from function

Code

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

How to return the value from the above code.

Need Vb6 code Help

0

3 Answers 3

15

You need to specify your return type.

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

Here is a link that is a good read for understanding Functions in VB6

EDIT

After reading your comment, I would create a class to store your values.

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

Then, you can instantiate a new instance of this class inside of your code:

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
Sign up to request clarification or add additional context in comments.

2 Comments

If i want to send multiple values means, how do modify my code, for example name, dept, country i want to return, for that what i have to modify my code....
@Gopal: If you want to return the RecordSet, change the return type to rdoResultSet. However, you are closing the RecordSet before you are leaving the function and this may cause issues later on.
3

You need to set the value to the function name and the return type:

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

See this document for more information.

4 Comments

If i want to send multiple values means, for example name, dept, country i want to return, for that what i have to modify my code....
@Gopal - Why not return the whole result set? Or create an object to hold these values and return that.
Ok i will return the result set, from other end how can i use that record set. For example, i am return the resultset rdo then in the formload how i can use that rdo result, Can you please help me....
RDO is not ADO, but a supplement to DAO that was a stopgap for VB5 before VB6 came along with good ADO support.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.