-
Jun 4th, 2021, 02:34 AM
#1
Thread Starter
New Member
How to pass variable to other Function/Function return a response?
Hi all, I would like to ask of how does the visual basic have to perform to get the variable of other functions. In my case I would like to have the called function to pass the variable to the method that call it. In simple term I would like to get the response of the function that I have been called.
here is the code example of what I mean
Code:
Private Sub ViewResponse()
Call HttpGetRequest(url.Text)
'Call the variable of resp\HttpGetRequest to view in this method
output.Text = 'resp\HttpGetRequest
End Sub
Private Function HttpGetRequest(url As String)
Dim req As XMLHTTP60
Set req = New XMLHTTP60
req.Open "GET", url, False
req.send ""
Dim resp As DOMDocument60
If req.responseText <> vbNullString Then
Set resp = New DOMDocument60
resp.LoadXml req.responseText
Else
Set resp = req.responseXML
End If
Set HttpGetRequest = resp
End Function
-
Jun 4th, 2021, 03:26 AM
#2
Re: How to pass variable to other Function/Function return a response?
> Hi all, I would like to ask of how does the visual basic have to perform to get the variable of other functions.
No, does not make sense.
> In my case I would like to have the called function to pass the variable to the method that call it.
No, does not make sense.
> In simple term I would like to get the response of the function that I have been called.
No, does not make sense.
>> Code example
No, does not make sense.
Well, at least I tried :-))
Probably you need to use CallByName function but not sure.
cheers,
</wqw>
-
Jun 4th, 2021, 03:32 AM
#3
Re: How to pass variable to other Function/Function return a response?
A Function() should return a result and a Sub() does not return anything.
A very basic sample (air code)
Code:
Private Sub Form_Load()
Debug.Print IsEqual(3, 1)
Debug.Print IsEqual(2, 2)
Text1.Text = IsEqualResponse(2, 2)
End Sub
Private Function IsEqualResponse(ByVal Value1 As Long, ByVal Value2 As Long) As String
' Using the return value of IsEqual to generate some text
If IsEqual(Value1, Value2) Then
IsEqualResponse = "The values are the same"
Else
IsEqualResponse = "The values are not the same"
End If
End Function
Private Function IsEqual(ByVal Value1 As Long, ByVal Value2 As Long) As Boolean
If Value1 = Value2 Then IsEqual= True
End Function
// Edit: Maybe I misunderstood your question..
-
Jun 4th, 2021, 04:32 AM
#4
Re: How to pass variable to other Function/Function return a response?
No idea what the question is, but you need lots of error checking for any robustness:
Code:
Dim XMLHTTP60 As MSXML2.XMLHTTP60
Dim ErrNum As Long
Dim ErrDesc As String
Dim MIME As String
Dim DOMDocument60 As MSXML2.DOMDocument60
Set XMLHTTP60 = New MSXML2.XMLHTTP60
With XMLHTTP60
.open "GET", "http://producthelp.sdl.com/sdl%20trados%20studio/client_en/sample.xm", False
On Error Resume Next
.send
ErrNum = Err.Number
ErrDesc = Err.Description
On Error GoTo 0
If ErrNum = 0 Then
If 200 <= .Status And .Status <= 299 Then
'The supported MIME types for MSXML 6.0 are: "text/xml", "application/xml"
'or anything that ends with "+xml", for example "application/rss+xml".
MIME = LCase$(Right$(.getResponseHeader("Content-Type"), 4))
If MIME = "/xml" Or MIME = "-xml" Then
Set DOMDocument60 = .responseXML
Debug.Print DOMDocument60.xml
Else
Debug.Print .getResponseHeader("Content-Type")
End If
Else
Debug.Print .Status, .statusText
End If
Else
Debug.Print ErrNum, ErrDesc
End If
End With
-
Jun 4th, 2021, 07:19 AM
#5
Re: How to pass variable to other Function/Function return a response?
The problem is this:
Code:
Private Sub ViewResponse()
Call HttpGetRequest(url.Text)
'Call the variable of resp\HttpGetRequest to view in this method
output.Text = 'resp\HttpGetRequest
End Sub
Specifically this line: Call HttpGetRequest(url.Text)
That call all be reduced to this:
Code:
Private Sub ViewResponse()
outputText.Text = HttpGetRequest(url.Text)
End Sub
Functions are subs that return a value... you can't `CALL` them ... you just invoke them and catch the returned value.
-tg
-
Jun 4th, 2021, 09:13 AM
#6
Re: How to pass variable to other Function/Function return a response?
Perhaps slavish (slavering?) use of the obsolete Call keyword just claimed another victim?
-
Jun 11th, 2021, 01:45 AM
#7
Thread Starter
New Member
Re: How to pass variable to other Function/Function return a response?
Well, I have tried this out but it returned me Run-time Error '438'(object doesn't support this property or method) as my HttpGetRequest value is on Object. How could I convert any var type value to a string for it to view in my Text box?
Or is there any method for the textbox to view output in any var?
Last edited by Final9800; Jun 11th, 2021 at 02:44 AM.
-
Jun 11th, 2021, 07:59 AM
#8
Re: How to pass variable to other Function/Function return a response?
What did you try "this" means nothing without the code it refers to.
-
Jun 13th, 2021, 08:43 PM
#9
Thread Starter
New Member
Re: How to pass variable to other Function/Function return a response?
Well sorry, I'm new to this forum and did not know how to referred the reply, but here is the method suggested by techgnome but I still get the error as I explain before
Originally Posted by techgnome
The problem is this:
Code:
Private Sub ViewResponse()
Call HttpGetRequest(url.Text)
'Call the variable of resp\HttpGetRequest to view in this method
output.Text = 'resp\HttpGetRequest
End Sub
Specifically this line: Call HttpGetRequest(url.Text)
That call all be reduced to this:
Code:
Private Sub ViewResponse()
outputText.Text = HttpGetRequest(url.Text)
End Sub
Functions are subs that return a value... you can't `CALL` them ... you just invoke them and catch the returned value.
-tg
This is the error that I got from implemented the method above in the code
Originally Posted by Final9800
Well, I have tried this out but it returned me Run-time Error '438'(object doesn't support this property or method) as my HttpGetRequest value is on Object. How could I convert any var type value to a string for it to view in my Text box?
Or is there any method for the textbox to view output in any var?
-
Jun 13th, 2021, 10:31 PM
#10
Re: How to pass variable to other Function/Function return a response?
You could attach your project (trimmed if you prefer).
Do you know how to attach a project to vbforums thread (this thread) ?
-
Jun 16th, 2021, 06:47 AM
#11
Thread Starter
New Member
Re: How to pass variable to other Function/Function return a response?
Here is the attachment of the simple project that I'm testing on. I intend trying to recreate something similar to postman using VB6
web Services.zip
-
Jun 16th, 2021, 08:26 AM
#12
Re: How to pass variable to other Function/Function return a response?
I tried to load your project but it has references to objects that are not on this system so can test it.
I do see some issues with your function though.
1. you do not have the return type defined.
2. you are not trying to return the data type that you are trying to retrieve.
Code:
Public Function HttpGetRequest(url As String) As String
HttpGetRequest=req.responseText
The part in red tells it to return a string to the caller
The other line tells it what that string should be.
If you want to return a DOMDocument60 then you need to define the function as such
Code:
Public Function HttpGetRequest(url As String) As DOMDocument60
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|