• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1552
  • Last Modified:

Expected End of Statement Error in Function

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
gandalf2041
Asked:
gandalf2041
  • 4
  • 4
  • 3
  • +3
1 Solution
 
exx1976Commented:
On what line?
0
 
HooKooDooKuCommented:
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
 
exx1976Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Rey Obrero (Capricorn1)Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Thanx...I keep switching from VB6 to VB.Net...
0
 
RobSampsonCommented:
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
 
gandalf2041Author Commented:
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
 
gandalf2041Author Commented:
exx1976:  Sorry, I forgot to answer your question.  It's on the first line
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What ENVIRONMENT are you working in?

VBScript
VBA
VB6
VB.Net
something else
0
 
Rey Obrero (Capricorn1)Commented:
hmmm... in the NEIGHBORHOOD? ( computer user answer to the Help desk q ) :-)
0
 
HooKooDooKuCommented:
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
 
RobSampsonCommented:
I second Idle_Mind....knowing exactly which environment you are working in may help provide a more accurate suggestion....
0
 
gandalf2041Author Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
RobSampsonCommented:
^^^ 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
 
exx1976Commented:
+1.   I didn't pick up on it either, been doing a lot of .NET lately...
0
 
RobSampsonCommented:
Hi Dhaest, my suggestion is that Idle_Mind's answer is accepted as a more accurate solution: ID: 22813030

Regards,

Rob.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 4
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now