Solved

Expected End of Statement Error in Function

Posted on 2008-10-23
19
1,515 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
  • 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
 
LVL 85

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 119

Expert Comment

by:Rey Obrero
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 85

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 85

Expert Comment

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

VBScript
VBA
VB6
VB.Net
something else
0
 
LVL 119

Expert Comment

by:Rey Obrero
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 85

Accepted Solution

by:
Mike Tomlinson earned 300 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

IT, Stop Being Called Into Every Meeting

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!

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

706 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

20 Experts available now in Live!

Get 1:1 Help Now