PDA

View Full Version : Solved: Userform & Class Module Interaction



godawgs85
07-18-2010, 07:26 PM
Hello All. Just joined the forum not long ago and am looking forward to the help. This is my first post.

I'm somewhat of a newbie to VBA and am learning as I go. I am working on developing an application for my company that involves data entry into a userform and then an analysis of that data.

Currently I have created a Class Module as I have heard Class modules are the best way to go to keep your codes clean and useful in other projects.

The current problem I am having now is translating properties that have been set up in the class module over to the userform.

I have the following code entered into my class module for a property displaying a buildings square footage:


Property Let BuildingRSF(newBuildingRSF As Long)
SFErrorMessage = "Please Enter a Valid SF."
If IsNumeric(newBuildingRSF) = True Then
p_BuildingRSF = newBuildingRSF
Else
MsgBox SFErrorMessage, vbOKOnly, "Square Footage Error"
End If
End Property

In translating this property over to the userform, I have the following code to set the input value as this property:


Private Sub BuildingRSF_AfterUpdate()
u_Lease.BuildingRSF = BuildingRSF
End Sub

I have already initialized the class (u_Lease) within the userform and as long as the value input into the BuildingRSF textbox is numeric the code works properly and the class property is defined. If a non numeric value is input, however, I get an error message.

My question is why is the error handling code that I defined in the class property not working? Is there a simple fix to this or do I need to handle the error in the userform? I'm hoping the former is the case otherwise my userform is going to have way too much code as I have a lot of these instances to handle.

Thanks in advance for any help that can be provided.

Also, what is the best resource to learn more about class modules and how they can be used in userforms, other modules, etc.?

Thanks!

Bob Phillips
07-19-2010, 12:23 AM
That is because you define the datatype of the property as long, so if you have a non-long, VBA complains.

Change it to variant.

godawgs85
07-19-2010, 05:46 PM
Thanks xld! That did the trick.

I do have a follow up question though. The userform I am developing is going to require several instances of this, and I'm afraid a majority of my class properties will need to be defined as variants to handle this sort of error checking and reduce human input error (lots of inputs of dates, dollar amounts, etc) I want the the class properties to only allow certain data inputs. I've always heard to try to avoid variant data types as they take up a lot of memory. Is this something I should be worried about? If not, I will continue, but if so, what are my other options?

Thanks in advance!

mikerickson
07-19-2010, 08:21 PM
Another way would be to force BuildingRSF into a numeric
Private Sub BuildingRSF_AfterUpdate()
u_Lease.BuildingRSF = Val(BuildingRSF.Text)
End Sub

Bob Phillips
07-20-2010, 12:47 AM
Thanks xld! That did the trick.

I do have a follow up question though. The userform I am developing is going to require several instances of this, and I'm afraid a majority of my class properties will need to be defined as variants to handle this sort of error checking and reduce human input error (lots of inputs of dates, dollar amounts, etc) I want the the class properties to only allow certain data inputs. I've always heard to try to avoid variant data types as they take up a lot of memory. Is this something I should be worried about? If not, I will continue, but if so, what are my other options?

Thanks in advance!

What is many? 100? Most likely irrelevant? 1,000? Most likely irrelevant? 1,000,000? What are you doing with so many anyway?