How to call a function with option parameter in another module in Ms Access

nector

Member
Local time
Today, 05:20
Joined
Jan 21, 2020
Messages
462
I'm trying to call the function below in another module or form I keep on getting an error

Code:
Public Function SendStrTO(sock, ByVal StrToSend As String, Optional Timeout As Long = 10) As Boolean

I'm calling it as below:

SendstrTo("192.666.1.501:8888",strData, 10)

I keep on getting an error as "EXPECT ="

Any idea here
 
A function returns a value. This one should return True or False.
You have to assing the returned value to a variable. Something like
YourVariable = SendstrTo("192.666.1.501:8888",strData, 10)
 
That error description isn't helpful. We need more info. Is there an error message number that is part of the error being reported? That would really be helpful.

If you have the function defined in one of your modules, you can put a breakpoint on some line of code, including the invocation of the function. I might suggest that you try that to single-step to find the line that triggers the error. However, if that is a call from some product library (as opposed to "home-grown"), it would be good for us to find out which library defines it. I did a search but that entry point didn't show up, which makes me think it might represent a customized SEND routine. But I might have just missed it.

And for the record, TECHNICALLY it is legal in VBA to call a function as though it were a subroutine. The return value will simply be discarded. It is not WISE to do that - but it IS legal. It is PARTICULARLY unwise because WinSock-related routines return TRUE or FALSE to show whether something worked or didn't (respectively) but you need to call another function to get the specific error message.

See also this thread: https://www.access-programmers.co.u...ms-access-vba-with-socket-programming.318926/

That article was referenced in the "similar threads" list at the bottom of THIS thread. If you are getting an error from the WinSock or related routines, there is a second call you need that would get you a more detailed error code. WSAGetLastError would be used when a socket-related call returns FALSE (meaning "didn't work.") That might tell you what really happened.
 
In your call you need to remove the parentheses since you are not assigning the return to anything. The error message IS helpful. It explains exactly what you are doing.
 
Ok here is a full code which I wanted to call from a form , sorry about that I did not give full information , calling a function like below on the option line Im getting an error see the actual error in ready below:

Code:
' -----------------------------------------------------------------------------
' Sending data (String) on ??a sock socket with timeout in seconds
' This function returns True if the sending is correct
' -----------------------------------------------------------------------------
Public Function SendStrTO(sock, ByVal StrToSend As String, Optional Timeout As Long = 10) As Boolean
Dim lBuffer As String
Dim lfdr As FD_SET, lfdw As FD_SET, lfde As FD_SET
Dim lret As Long
Dim lti As TIMEVAL
' delay in seconds
lti.tv_sec = Timeout
' a socket to check
lfdw.fd_count = 1
' the socket passed as a parameter
lfdw.fd_array(1) = sock
' is the socket ready?
lret = wselect(0, lfdr, lfdw, lfde, lti)
' If no error and time not exceeded
If lret > 0 Then
    ' Loop while there is data to send
    Do
        ' If socket ready
        If lfdw.fd_count = 1 Then
            ' Mail
            lret = sendstr(sock, StrToSend, Len(StrToSend), 0)
            ' Test the return
            If lret = -1 Then
                ' errors
                SendStrTO = False
                Exit Do
            Else
                If lret < Len(StrToSend) Then
                    ' Partial shipment
                    StrToSend = Mid(StrToSend, lret + 1)
                Else
                    ' Send completed
                    SendStrTO = True
                    Exit Do
                End If
            End If
        End If
    Loop
End If
End Function
 

Attachments

  • Error messege.png
    Error messege.png
    133.3 KB · Views: 151
  • Original code.png
    Original code.png
    116.4 KB · Views: 155
Ok here is a full code which I wanted to call from a form , sorry about that I did not give full information , calling a function like below on the option line Im getting an error see the actual error in ready below:

Code:
' -----------------------------------------------------------------------------
' Sending data (String) on ??a sock socket with timeout in seconds
' This function returns True if the sending is correct
' -----------------------------------------------------------------------------
Public Function SendStrTO(sock, ByVal StrToSend As String, Optional Timeout As Long = 10) As Boolean
Dim lBuffer As String
Dim lfdr As FD_SET, lfdw As FD_SET, lfde As FD_SET
Dim lret As Long
Dim lti As TIMEVAL
' delay in seconds
lti.tv_sec = Timeout
' a socket to check
lfdw.fd_count = 1
' the socket passed as a parameter
lfdw.fd_array(1) = sock
' is the socket ready?
lret = wselect(0, lfdr, lfdw, lfde, lti)
' If no error and time not exceeded
If lret > 0 Then
    ' Loop while there is data to send
    Do
        ' If socket ready
        If lfdw.fd_count = 1 Then
            ' Mail
            lret = sendstr(sock, StrToSend, Len(StrToSend), 0)
            ' Test the return
            If lret = -1 Then
                ' errors
                SendStrTO = False
                Exit Do
            Else
                If lret < Len(StrToSend) Then
                    ' Partial shipment
                    StrToSend = Mid(StrToSend, lret + 1)
                Else
                    ' Send completed
                    SendStrTO = True
                    Exit Do
                End If
            End If
        End If
    Loop
End If
End Function
see how you can help
 

Attachments

  • Error messege.png
    Error messege.png
    133.3 KB · Views: 134
  • Original code.png
    Original code.png
    116.4 KB · Views: 140
See how you can help
What part of "remove the parentheses" from your call to the function did you not understand?
X= somefunction (123, "a")
But
Somefunction 123, "a"
When no return assigned
 
See how you can help
MajP has shown you what you can do.

If you are still confused here are 3 methods:

Method 1 - Call function as a sub (as suggested by MajP):
Code:
' ...
  SendStr hston, strData, Timeout
' or
  SendStr hston, strData
' ...

Method 2 - Assign result to a variable (which you can use or ignore):
Code:
' ...
  Dim blSuccess As Boolean
' ...
  blSuccess = SendStr(hston, strData, Timeout)
' or
  blSuccess = SendStr(hston, strData)
' ...

Method 3 - test the return result of the function:
Code:
' ...
  If SendStr(hston, strData, Timeout) Then
    MsgBox "It worked"
  Else
    MsgBox "Something went wrong"
  End If
' or
  If SendStr(hston, strData) Then
    MsgBox "It worked"
  Else
    MsgBox "Something went wrong"
  End If
' ...
 
MajP has shown you what you can do.

If you are still confused here are 3 methods:

Method 1 - Call function as a sub (as suggested by MajP):
Code:
' ...
  SendStr hston, strData, Timeout
' or
  SendStr hston, strData
' ...

Method 2 - Assign result to a variable (which you can use or ignore):
Code:
' ...
  Dim blSuccess As Boolean
' ...
  blSuccess = SendStr(hston, strData, Timeout)
' or
  blSuccess = SendStr(hston, strData)
' ...

Method 3 - test the return result of the function:
Code:
' ...
  If SendStr(hston, strData, Timeout) Then
    MsgBox "It worked"
  Else
    MsgBox "Something went wrong"
  End If
' or
  If SendStr(hston, strData) Then
    MsgBox "It worked"
  Else
    MsgBox "Something went wrong"
  End If
' ...
Thank you so much and well appreciated
 
I forgot one more method!

Method 4 - ignore the assignment of the result of the function, but still use the parentheses, using 'Call':
Code:
' ...
  Call SendStr(hston, strData, Timeout)
' or
  Call SendStr(hston, strData)
' ...

Many ways to skin this cat!
 

Users who are viewing this thread

Back
Top Bottom