Solved

Function error

Posted on 2000-04-13
13
148 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 69

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now