How to return a value from a function

How to return a value from a function

the 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 a value from the above code.

Need help on Vb6 code

+6
source share
3 answers

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 reads well for understanding functions in VB6

EDIT

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

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

You can then instantiate a new instance of this class inside 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 
+9
source

You need to set a value for the function name and 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 details.

+2
source
 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 
0
source

Source: https://habr.com/ru/post/904974/


All Articles