![]() |
textboxes and datatypes
I've used a class module to define my object with various properties and methods which interact with each other. I then populate a collection (database) with my objects (records). I have a userform for adding a new record, or calling up an existing record. The fields (textboxes) on the form correspond to the properties of my class module.
In the class module I have written all the code for the interaction between the properties, and I want exactly the same rules to apply to the userform. I've been trying to accomplish this by using the same code, but my problem is that textboxes hold text, and the various properties of my class module can hold dates, currency, etc. There's no "type" property for my textboxes. Can I tell VBA what type of data is stored in each of the textboxes in my form so that my calculations will work? and / or Is there another way around this that I'm missing? Thanks very much for any help Rob 'I populate my collection Set DataBase = New Collection For i = 2 To LastRow(3, DataSheet.Name, ThisWorkbook.Name) Set Record = New MyClass Record.OrderNumber = DataSheet.Cells(i, 3) DataBase.Add Record Next 'when I change the OrderNumberTextbox on my form: Private Sub OrderNumber_Change() Set ThisOrder = New MyClass With ThisOrder .OrderNumber = OrderNumber 'If the order number already exists in my collection the other properties are set '...then I fill in the textboxes OrderDate = .OrderDate DeliveryDate = .DeliveryDate Supplier = .Supplier Description = .Description OrderValue = .OrderValue InvoiceValue = .InvoiceValue 'when i try to do a calculation, eg. if OrderDate = DeliveryDate then CalculationTextBox = OrderValue - InvoiceValue 'everything goes wrong because it thinks everything is text End With |
textboxes and datatypes
Do you want to avoid using datatype conversion functions?
CStr, Cdate, CInt, etc. -Brad -----Original Message----- I've used a class module to define my object with various properties and methods which interact with each other. I then populate a collection (database) with my objects (records). I have a userform for adding a new record, or calling up an existing record. The fields (textboxes) on the form correspond to the properties of my class module. In the class module I have written all the code for the interaction between the properties, and I want exactly the same rules to apply to the userform. I've been trying to accomplish this by using the same code, but my problem is that textboxes hold text, and the various properties of my class module can hold dates, currency, etc. There's no "type" property for my textboxes. Can I tell VBA what type of data is stored in each of the textboxes in my form so that my calculations will work? and / or Is there another way around this that I'm missing? Thanks very much for any help Rob 'I populate my collection Set DataBase = New Collection For i = 2 To LastRow(3, DataSheet.Name, ThisWorkbook.Name) Set Record = New MyClass Record.OrderNumber = DataSheet.Cells(i, 3) DataBase.Add Record Next 'when I change the OrderNumberTextbox on my form: Private Sub OrderNumber_Change() Set ThisOrder = New MyClass With ThisOrder .OrderNumber = OrderNumber 'If the order number already exists in my collection the other properties are set '...then I fill in the textboxes OrderDate = .OrderDate DeliveryDate = .DeliveryDate Supplier = .Supplier Description = .Description OrderValue = .OrderValue InvoiceValue = .InvoiceValue 'when i try to do a calculation, eg. if OrderDate = DeliveryDate then CalculationTextBox = OrderValue - InvoiceValue 'everything goes wrong because it thinks everything is text End With . |
textboxes and datatypes
I'm not entirely sure what the problem is but it seems to me you could
change the line where it 'all goes wrong' to use the values from the class e.g. With ThisOrder If .OrderDate = .DeliveryDate Then CalculationTextBox.Text = CStr(.OrderValue - .InvoiceValue) End If End With Another more general suggestion is to make your class more flexible by using textboxes as (optional) properties. For example you could have a OrderDateField (textbox) property and your OrderDate property would use the textbox if it had been set, otherwise it would defulat to the internal variable e.g. Option Explicit Private WithEvents m_oOrderDateField As MSForms.TextBox Private m_dtmOrderDate As Date Public Property Set OrderDateField(ByVal oTextBox As MSForms.TextBox) Set m_oOrderDateField = oTextBox End Property Public Property Let OrderDate(ByVal NewValue As Date) ' <Validation code here If m_oOrderDateField Is Nothing Then m_dtmOrderDate = NewValue Else m_oOrderDateField.Text = CStr(NewValue) End If End Property Public Property Get OrderDate() As Date If m_oOrderDateField Is Nothing Then OrderDate = m_dtmOrderDate Else ' <Validation code here OrderDate = CDate(m_oOrderDateField.Text) End If End Property This probably won't exactly fit your needs but I hope you get the idea of making your class more flexible. -- Rob wrote in message ... I've used a class module to define my object with various properties and methods which interact with each other. I then populate a collection (database) with my objects (records). I have a userform for adding a new record, or calling up an existing record. The fields (textboxes) on the form correspond to the properties of my class module. In the class module I have written all the code for the interaction between the properties, and I want exactly the same rules to apply to the userform. I've been trying to accomplish this by using the same code, but my problem is that textboxes hold text, and the various properties of my class module can hold dates, currency, etc. There's no "type" property for my textboxes. Can I tell VBA what type of data is stored in each of the textboxes in my form so that my calculations will work? and / or Is there another way around this that I'm missing? Thanks very much for any help Rob 'I populate my collection Set DataBase = New Collection For i = 2 To LastRow(3, DataSheet.Name, ThisWorkbook.Name) Set Record = New MyClass Record.OrderNumber = DataSheet.Cells(i, 3) DataBase.Add Record Next 'when I change the OrderNumberTextbox on my form: Private Sub OrderNumber_Change() Set ThisOrder = New MyClass With ThisOrder .OrderNumber = OrderNumber 'If the order number already exists in my collection the other properties are set '...then I fill in the textboxes OrderDate = .OrderDate DeliveryDate = .DeliveryDate Supplier = .Supplier Description = .Description OrderValue = .OrderValue InvoiceValue = .InvoiceValue 'when i try to do a calculation, eg. if OrderDate = DeliveryDate then CalculationTextBox = OrderValue - InvoiceValue 'everything goes wrong because it thinks everything is text End With |
All times are GMT +1. The time now is 02:28 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com