Results 1 to 12 of 12

Thread: How to pass variable to other Function/Function return a response?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    4

    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

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,121

    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>

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    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..

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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?

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    4

    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.

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    4

    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

    Quote Originally Posted by techgnome View Post
    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
    Quote Originally Posted by Final9800 View Post
    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?

  10. #10
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    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) ?

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    4

    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

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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
  •  



Click Here to Expand Forum to Full Width