?
Solved

Expected End of Statement Error in Function

Posted on 2008-10-23
19
Medium Priority
?
1,539 Views
Last Modified: 2013-11-27
For the life of me, I can not figure out what's wrong with the following:

Public Function getFName(path as String) as String
      Dim pos1 As Integer
      Dim pos2 As Integer
      Dim tmpPos As Integer
      pos1 = 1
            Do Until (InStr(pos1, path, "\") = 0)
                  tmpPos = InStr(pos1, path, "\")
                  pos1 = tmpPos + 1
            Loop
      pos2 = InStr(path, ".") - pos1
      fName = Mid(path, pos1, pos2)
      MsgBox fName
      getFName = fName
End Function

Why do I keep getting an "Expected end of statement" error
0
Comment
Question by:gandalf2041
[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
  • 4
  • 4
  • 3
  • +3
19 Comments
 
LVL 18

Expert Comment

by:exx1976
ID: 22789517
On what line?
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22789580
Could it be the fact you did not Dim 'fName'?

When I dropped your source code in my compiler, the compiler complained about fName not define (I have Option Explicit turn on).  Once I added "Dim fName as String", the code compiled without a problem.  If that isn't the issue, could it be something for source code that appears before this function (like a subroutine that didn't include an "End Function" or "End Sub"?
0
 
LVL 18

Expert Comment

by:exx1976
ID: 22789606
That's what I'm thinking..  Something on a line above this one.  I've looked over this code 3 or 4 times now, and I can't find anything syntactically incorrect about it..

@HooKooDooKu - fName could be dim'd globally..  ?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22789736
It looks like you are tryinng to get the filename without the path or extension?

    Function GetFName(ByVal path As String) As String
        Static fso As Object
        If fso Is Nothing Then
            fso = CreateObject("Scripting.FileSystemObject")
        End If
        GetFName = fso.GetBaseName(path)
    End Function
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 22789905
slight change

    Function GetFName(ByVal path As String) As String
        Static fso As Object
        If fso Is Nothing Then
            set fso = CreateObject("Scripting.FileSystemObject")
        End If
        GetFName = fso.GetBaseName(path)
    End Function
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22789918
Thanx...I keep switching from VB6 to VB.Net...
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22790612
Hi, there are no errors in that code that I can see.....perhaps the error is to with the way you're calling the function....

You should have something like

strFullPath = "C:\Temp\Script.vbs"
strBaseName = getFName(strFullPath)

Regards,

Rob.
0
 

Author Comment

by:gandalf2041
ID: 22794905
Thanks for all the feedback.  The code above the function compiles fine when I take it out.  I also suspected there was an error above but that doesn't seem to be the case (at least I can't find it). I've checked the call and that's OK too.

When I paste the function in a new editor window, I get the same error.  I'll try some of the suggestions above and post back
0
 

Author Comment

by:gandalf2041
ID: 22794908
exx1976:  Sorry, I forgot to answer your question.  It's on the first line
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22796779
What ENVIRONMENT are you working in?

VBScript
VBA
VB6
VB.Net
something else
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 22796869
hmmm... in the NEIGHBORHOOD? ( computer user answer to the Help desk q ) :-)
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22797165
Here's something to focus on.

When the function is pasted into a whole new program, the only error that is raised is the fact that within the scope of the function, the variable fName is not Dimmed.  That basically means the function itself is sound as long as fName is Dimmed somewhere.

But when you paste the function into a new file that is still within your project, the error doesn't go away.  That would imply that it has something to do with the application as a whole and not this function in isolation.

The 1st thing that comes to mind for such a problem is that you are using the same name for different things within your application.  Since you say the error appers on the first line (I assume you mean the line nameing the function and not the 1st line of executable code within the function), my guess is that you are using the name "getFName" somewhere else in your application (a global variable, a class, a form, or a control on a form, another function, another function in a referenced DLL).  

Suggestions:

Try removing the guts of the function so that you only have...
Public Function getFName(path as String) as String
End Function
...and see if you still get the same error. (Hard code a return value if you have to).  That should indicate if the error has something to do with the name 'getFName' or it's something in the body of the code.

Remove the function entirely from the application.  Insert the simple function...
Public Function WhatIsWrongWithgetFName(path as String) as String
    WhatIsWrongWithgetFName = "Hard Coded Value"
End Function
...and update all function calls to 'getFName' with 'WhatIsWrongWithgetFName' and try to recompile again.  If that compiles without error, then is must be something with the name 'getFName'.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22805617
I second Idle_Mind....knowing exactly which environment you are working in may help provide a more accurate suggestion....
0
 

Author Comment

by:gandalf2041
ID: 22812510
My apologies for not being clear.  I thought the vbScript tag would let you know the environment but, it is indeed vbScript.  As I said, I'm not really a programmer.  I ended up scrapping the function and simply putting my code in the body of the program.  It works fine now.  I'll try HooKooDooKu's suggestion and see if I can get it to compile.  Thanks
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1200 total points
ID: 22813030
For VBScript, you need to get rid of the DataTypes...the "As XXX" parts as everything in VBScript is a Variant:
http://msdn.microsoft.com/en-us/library/0ad0dkea(VS.85).aspx


Public Function getFName(path)
      Dim pos1, pos2, tmpPos
      pos1 = 1
            Do Until (InStr(pos1, path, "\") = 0)
                  tmpPos = InStr(pos1, path, "\")
                  pos1 = tmpPos + 1
            Loop
      pos2 = InStr(path, ".") - pos1
      fName = Mid(path, pos1, pos2)
      getFName = fName
End Function

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22827050
^^^ Agreed.  Sorry we didn't pick up on that!  As Idle stated, VBScript does not support data type declarations....all variables are declared as a "variable" data type.

Regards,

Rob.
0
 
LVL 18

Expert Comment

by:exx1976
ID: 22828069
+1.   I didn't pick up on it either, been doing a lot of .NET lately...
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23223331
Hi Dhaest, my suggestion is that Idle_Mind's answer is accepted as a more accurate solution: ID: 22813030

Regards,

Rob.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

765 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