Solved

Function error

Posted on 2000-04-13
13
149 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb6 connector to mongodb 2 81
VBA Shell can't Find Word document 11 92
fso.FolderExists("\\server\HiddenFolder$") 4 64
Send outlook email from VBS Script 2 28
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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

776 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