Solved

Function error

Posted on 2000-04-13
13
155 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

617 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