[AccessD] Need help with return in a function
Heenan, Lambert
Lambert.Heenan at aig.com
Thu Apr 28 13:40:39 CDT 2016
I'll make some comments on this code, line by line...
Function Delete_File(aFile) ' - declares a function but fails to define the return value type
Dim B As String ' - variable not used
On Error Resume ' - not a legal statement in VBA
Kill aFile ' - try to delete the file
On Error GoTo 0 ' restore default error handling
return Len(Dir$(aFile)) > 0 ' - "return something" is not legal in VBA
End Function
For a function to return a value in VBA you simply assign that value to the function name. So the 'return' line above should have been...
Delete_File = Len(Dir$(aFile)) > 0
Dir$(aFile) will return a file name and extension if 'aFile' exists, so it's length will be > 0. So if the file is still present Len(Dir$(aFile)) > 0 = TRUE. So this function is supposed to return TRUE if the file did not get deleted, else it returns FALSE.
Here's a version of this code that works...
Function Delete_File(aFile) As Boolean ' declare the return type
On Error Resume Next - turn off error handling
Kill aFile
On Error GoTo 0 ' resume error handling
Delete_File = Len(Dir$(aFile)) = 0 ' return True/False
End Function
This function returns TRUE if the file got deleted. It returns FALSE if the file was not deleted OR IF IT DOES NOT EXIST.
No idea why you are getting "Expected end of statement" though. Sound like a C/C++/C# error message.
Lambert
-----Original Message-----
From: AccessD [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Kaup, Chester
Sent: Thursday, April 28, 2016 1:56 PM
To: Access Developers discussion and problem solving
Subject: [AccessD] Need help with return in a function
Came across a bit of code in a database and I don't understand what one line is supposed to do or how it is intended to work. I get a syntax error of "Expected end of statement" The line in question is return Len(Dir$(aFile)) >0. Thanks for the assistance.
Here is the whole function
Function Delete_File(aFile)
Dim B As String
On Error Resume
Kill aFile
On Error GoTo 0
return Len(Dir$(aFile)) > 0 ' Make sure it actually got deleted.
End Function
--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com
More information about the AccessD
mailing list