Solved

Function error

Posted on 2000-04-13
13
152 Views
Last Modified: 2010-05-02
I have a function, sort inside of a module, with parameters as such:

Public Sub Sort(SortCol As Integer, LastRow As Integer, Optional FirstRow As Integer = 1, Optional direction As String = "a", Optional index As Integer = 0)

I try to call this function from within a form as such:

something = Sort(location, grid.Rows)

where something is a variant.. it's not dimmed..
location is an integer
grid is a flexgrid

I get an error "Compile error: Expected function or variable".

What's wrong?  How do I call this function?
0
Comment
Question by:d_jedi
  • 3
  • 3
  • 3
  • +3
13 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 2712084
you should declare it as a Funtion and not as a sub
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2712093
Yeah.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2712104
If a function returns a value, you must declare it as a function, not a sub:

Public Function Sort(...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:powlin
ID: 2712136
Sub don't return a value
you must use Function to return a value
Public Function Sort(SortCol As Integer, LastRow As Integer, Optional FirstRow As Integer = 1, Optional direction As String = "a", Optional index As Integer = 0) as Variant

As Variant is the type of value that the function return if not specified Variant is used. But it's always better to specify a real type.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2712167
powlin, RonaldBiemans AND Erick37 have already posted the same suggestion. According to EE "netiquette" you should not have locked the question. Would you kindly withdraw your answer?
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 2712196
Yeah
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2712222
Yeah
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2712227
<g>
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2712323
powlin:

Consider the condition that the function may return different data types depending on the parameters passed to it.  If this is the case, we do not want to implicitly define the return type, but can determine the type passed back later:

Public Function Sort(SortCol As Integer, LastRow As Integer)
    Dim sText As String
    sText = CStr(SortCol + LastRow)
    Sort = sText
End Function

Private Sub Command1_Click()
Dim something
something = Sort(10, 10)
Select Case VarType(something)
    Case vbEmpty
    Case vbNull
    Case vbInteger
    Case vbLong
    Case vbSingle
    Case vbDouble
    Case vbCurrency
    Case vbDate
    Case vbString
        MsgBox something & " is a string"
    Case vbError
    Case vbBoolean
    Case vbVariant
    Case vbDecimal
    Case vbByte
    Case vbArray
End Select
End Sub
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2712513
You may a collision between your function name and another Sub or Function of another components.

You have 2 solutions:
-Change your function name
-Put the name of the module in front of the function name when calling it (something = YourModuleName.Sort(location, grid.Rows) )
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
ID: 2713417
By the looks of the code (a FlexGrid sort).. it would appear that he does not need a return.. as the effects of what he is doing will be displayed in the FlexGrid.

If return information is NOT required.. then use the Sub as you have it.. and then use the following syntax:

Call Sort(location, grid.Rows)

If return information IS required.. then use a Function rather than a Sub.. as per the comments above.. <smile>.



0
 
LVL 1

Expert Comment

by:powlin
ID: 2713758
Erick if you don't specify the type of the variable Variant is used. So
Dim something
create a variant
Public Function Sort(SortCol As Integer, LastRow As Integer)
return a Variant
why not specify it when you know what type is return.
0
 
LVL 1

Expert Comment

by:powlin
ID: 2713769
tk ..
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question