Solved

Function error

Posted on 2000-04-13
13
153 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 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