Error calling Class Module

Tracy
Tracy used Ask the Experts™
on
Hi guys,

I have a class module called cls_Cfg and this is a function in the class module:

Private Function EncryptFile(ByVal InFile As String, ByVal OutFile As String, _
 ByVal Overwrite As Boolean, ByVal Key As String) As Boolean

I want to call this routine from a regular module, but when I try the below code I get a compile error saying method or data members not found.  I'm not sure what the syntax is, I don't use class modules that often.

Thanks,
Tracy
Sub testCall()

    Dim clsX As cls_Cfg
    
    Set clsX = New cls_Cfg
    clsX.EncryptFile("H:\tracy\cfg\temp.txt", "H:\tracy\cfg\temp2.txt", True, "test") = True
    Set clsX = Nothing

End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Tracy

Can you share the class module?

Sid
Commented:
if the function declaration inside the class is "Private" you cannot see this function from the outside world (as in your module) - change the declaration to "Public" and the method will be found

Commented:
Yes, open Excel, expand the modules are and open the cls_Cfg file and search for the EncryptFile

If the intellisense works and shows that when you put in clsX with the dot after it, then it should be value and ready to receive information as described.  However, show us at least the signature (Sub line)
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Commented:
hmmm...also just noticed that you end the line with "= True"
Why are you trying to assign something to a method...I think that might be the problem.

Try just this:


    clsX.EncryptFile("H:\tracy\cfg\temp.txt", "H:\tracy\cfg\temp2.txt", True, "test")
TracyVBA Developer

Author

Commented:
>>Can you share the class module?
Yes, I'll post in a few

>>hmmm...also just noticed that you end the line with "= True"
Yeah, I put that in because I was getting an "expected = error", so I was just trying stuff.

>>if the function declaration inside the class is "Private" you cannot see this function from the outside world (as in your module) - change the declaration to "Public" and the method will be found
I thought I had tried that, but maybe it was a different function I changed to public

>>If the intellisense works and shows that when you put in clsX with the dot after it, then it should be value
No intellisense
TracyVBA Developer

Author

Commented:
Attached is the code in the class module.

I changed to Public and now intellisense is there, which is good.  However, when I take the "= True" part off it gives the "expected =" error again.

Thanks,
Tracy
cls-Cfg.cls

Commented:
OK, so I see this signature:

Public Function EncryptFile(ByVal InFile As String, ByVal OutFile As String, _
 ByVal Overwrite As Boolean, ByVal Key As String) As Boolean

So it will return a boolean and expects 4 parameters.

You're giving this:

    clsX.EncryptFile("H:\tracy\cfg\temp.txt", "H:\tracy\cfg\temp2.txt", True, "test") = True

String, String, boolean, String.
Excellent!

Try this calling line instead:


    Debug.Print clsX.EncryptFile("H:\tracy\cfg\temp.txt", "H:\tracy\cfg\temp2.txt", True, "test") '= True

See if a result shows up in the Intermediate / Output window.
TracyVBA Developer

Author

Commented:
>>  Debug.Print clsX.EncryptFile("H:\tracy\cfg\temp.txt", "H:\tracy\cfg\temp2.txt", True, "test")
Yes, it returns True

Commented:
I think your problem was that VB likes to have functions stored somewhere.  When it said that "=" was missing, it wanted it on the left of the function and assigned to some variable.  I applied it to the print command but you'll probably want to save it in a variable for future use like:

strEncryptedFile = clsX.EncryptFile("H:\tracy\cfg\temp.txt", "H:\tracy\cfg\temp2.txt", True, "test")
Commented:
Sorry...it's returning a true/false, so you probably want something like this:

If clsX.EncryptFile("H:\tracy\cfg\temp.txt", "H:\tracy\cfg\temp2.txt", True, "test") Then
   Msgbox "File successfully encrypted"
Else
  Msgbox "File encryption failed"
End If

TracyVBA Developer

Author

Commented:
Great, thanks for the help.
Guess i am late for the party :)

Good Job Rob :)

Sid
Top Expert 2010

Commented:
Tracy,

Happy to see you got the help you needed.

In the future, if you want to make a class property/method "invisible" to the outside world, but accessible from the rest of your project, try declaring the property/method as Friend instead of Private.

Patrick
TracyVBA Developer

Author

Commented:
Good tip Patrick, thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial