VBA compile error: Method or data member not found - vba

everyone. here's my code. When debugging it says "VBA compile error: Method or data member not found" and highlights line: Familienkutsche.strFarbe = "Blau"
If I outcomment it, it says the same thing about the line that follows. What does it not like? Everything is written in one block, so why doesn't he recognize either "strFarbe" or "Geschwindigkeit"? having said that, if I remove Familienkutsche and just leave .strFarbe = "Blau" everything works. Thank you in advance.
Option Explicit
Public strFarbe As String
Private bytTempo As Byte
Private blnTempoSperre As Boolean
Public Property Let Geschwindigkeit(Speed As Long)
If (Speed > 250) Then
bytTempo = 250
blnTempoSperre = True
Else
bytTempo = Speed
blnTempoSperre = False
End If
End Property
Public Property Get Geschwindigkeit() As Long
Geschwindigkeit = bytTempo
End Property
Public Property Get abgeriegelt() As Boolean
abgeriegelt = blnTempoSperre
End Property
Public Sub Autos()
Dim Familienkutsche As Auto
Let Familienkutsche = New Auto
Familienkutsche.strFarbe = "Blau"
Familienkutsche.Geschwindigkeit = 320
Debug.Print Familienkutsche.Geschwindigkeit
Debug.Print Familienkutsche.abgeriegelt
End Sub

The first part of your code must be in a class module Auto.
Public Sub Autos() must be in a standard module. Then it works (with changing Let to Set).
Output:
250
Wahr

Related

Using a Sub with Arguments in a class module [duplicate]

This question already has answers here:
VBA does not accept my method calling and gives Compile error: Syntax error [duplicate]
(2 answers)
Closed 4 years ago.
I have a Sub in a regular Module where my code is mainly taking place. In a class module i have a sub, requiring Arguments which are not properties of the class module itself. Now i have trouble with getting the code started.
I made an example code with a minimum of lines, so you can see what i planned to do.
First the class module "clsCity":
Option Explicit
Public area As Single
'Public l As Single 'Working, but not desired solution:
'Public h As Single 'Working, but not desired solution:
Sub calcArea(length As Single, height As Single)
area = length * height
End Sub
Now the program itself:
Sub Country()
Dim BikiniBottom As New clsCity
Dim l As Single
Dim h As Single
Bikinibottom.calcArea(l,h) 'Error: "vba: Compile Error: expected: ="
'Working, but not desired solution:
'Bikinibottom.calcArea 'Where l and h are properties of clsCity
End Sub
As you can see, i plan to calculate the area of Bikinibottom with variables "l" and "h", which are not properties of clsCity. I get the error "vba: Compile Error: expected: =" when pressing enter. What do i do wrong? This is a sub and not a function, a "=" should not be necessary.
I know it would work to use the commented code. However, this is just a short example, please keep in mind that in my real code it is not a sophistic solution to make "l" and "h" a property of clsCity (they are properties of other class modules!)
Ok i managed to do it this way:
in class module:
Function calcArea(length As Single, height As Single)
calcArea = length * height
End Function
and in my main code:
BikiniBottom.area = BikiniBottom.calcArea(l, h)
So basicly my problem is solved.
Still curious how it is possible to use Subs with Arguments in a class module. Ideas?
I'd write this
Option Explicit
Sub Country()
Dim BikiniBottom As New clsCity
BikiniBottom.Length = 2
BikiniBottom.Height = 3
Debug.Print BikiniBottom.area
End Sub
and then for the class I'd use property procedures ...
Option Explicit
Private m_Length As Single
Private m_Height As Single
Public Property Let Length(rhs As Single)
m_Length = rhs
End Property
Public Property Get Length() As Single
Length = m_Length
End Property
Public Property Let Height(rhs As Single)
m_Height = rhs
End Property
Public Property Get Height() As Single
Height = m_Height
End Property
Public Function area() As Single
area = m_Length * m_Height
End Function

How do I declare a constant variable with a value of a function call

In a VBA module I have the following declaration of constants:
Private Const const_abc = 3000
Private Const const_def = 900
Private Const const_etc = 42
' and so on and so forth
Now, I have to initialize these values with a one time function call, ideally something like so
Private Const const_abc = someFunc(18)
Private Const const_def = someFunc( 7)
Private Const const_etc = someFunc( 5)
' and so on and so forth
Of course, this won't work in VBA. So, is there a common pattern on how to deal with such a requirement?
I probably could go like so
Private const_abc As Double
Private const_def As Double
Private const_etc As Double
sub initConsts()
const_abc = someFunc(18)
const_def = someFunc( 7)
const_etc = someFunc( 5)
end sub
But then I'd have to make sure that initConsts is called which I'd rather not do.
Edit As per the question of S O, I am using MS-Access.
Create a class that reads the cell and presents a Get-only interface to the value.
Here's a class called ItsMyValueClass
Option Explicit
Private pMyVal As Integer
Public Property Get MyValue() As Integer
MyValue = pMyVal
End Property
Private Sub class_initialize()
'pMyVal = Sheet.Range("somewhere)
pMyVal = 17
End Sub
And here's the code in your module:
Option Explicit
Sub IsItReadOnly()
Dim valu As ItsMyValueClass
Dim x As Integer
Set valu = New ItsMyValueClass
x = valu.MyValue
'valu.MyValue = 23 'compile error "Can't assign to read-only property"
End Sub
Public Function White() as Long
White = RGB(255,255,255)
End function
Private Sub TestIt()
Debug.Print "White is " & White
White = 123 ' <-- compile error
End Sub
in a one-liner that works with modules and classes alike for pure constant-like access:
Public Property Get myConst() As Integer: myConst = 3: End Property
you would use it like this:
Sub test()
Debug.Print "myConst: " & myConst 'would print: "myConst: 3"
End Sub
and if it has to be initialized with a custom value once, one could do it with a static property and one or many private variables:
Private ci As Boolean 'constants initialized
Private myConst1_ As Integer
Private myConst2_ As Integer
Static Property Get myConst1() As Integer
If Not ci Then init
myConst1 = myConst1_
End Property
Static Property Get myConst2() As Integer
If Not ci Then init
myConst2 = myConst2_
End Property
Private Sub init()
'these can come from anywhere:
myConst1_ = 3
myConst2_ = 5
ci = True
End Sub
they are initialized on the first access of the first "constant" property
if you have to initialize them earlier one could just call the init function earlier (and optionally remove the ci variable and all related lines if it is ensured that the properties are not accessed earlier)

VBA Object module must Implement ~?

I have created two classes, one being an interface for the other. Each time I try to instantiate Transition_Model I get:
Compile error: Object Module needs to implement '~' for interface'~'
To my understanding Implementing class is supposed to have a copy of all public subs, function, & properties. So I don't understant what is the problem here?
Have seen similar questions come up but either they refer to actual Sub or they include other complications making answer too complicated for me to understand.
Also note I tried changing Subs of Transition_Model to Private and add 'IModel_' in front of sub names(Just like top answer in second question I linked) but I still receive the same error.
IModel
Option Explicit
Public Enum Model_Types
Transition
Dummy
End Enum
Property Get M_Type() As Model_Types
End Property
Sub Run(Collat As Collateral)
End Sub
Sub Set_Params(key As String, value As Variant)
End Sub
Transition_Model
Option Explicit
Implements IModel
Private Transitions As Collection
Private Loan_States As Integer
Private Sub Class_Initialize()
Set Transitions = New Collection
End Sub
Public Property Get M_Type() As Model_Types
M_Type = Transition
End Property
Public Sub Run(Collat As Collateral)
Dim A_Transition As Transition
Dim New_Balance() As Double
Dim Row As Integer
For Row = 1 To UBound(Collat.Curr_Balance)
For Each A_Transition In Transitions
If A_Transition.Begining = i Then
New_Balance = New_Balance + Collat.Curr_Balance(Row) * A_Transition.Probability
End If
Next A_Transition
Next
End Sub
Public Sub Set_Params(key As String, value As Double)
Dim Split_key(1 To 2) As String
Dim New_Transition As Transition
Split_key = Split(key, "->")
Set New_Transition = New Transition
With New_Transition
.Begining = Split_key(1)
.Ending = Split_key(2)
.Probability = value
End With
Transitions.Add New_Transition, key
End Sub
Lastly the Sub I am using to test my class
Sub Transition_Model()
Dim Tested_Class As New Transition_Model
Dim Collat As New Collateral
'Test is the model type is correct
Debug.Assert Tested_Class.M_Type = Transition
'Test if Model without transition indeed does not affect balances of its collateral
Collat.Curr_Balance(1) = 0.5
Collat.Curr_Balance(2) = 0.5
Tested_Class.Run (Collat)
Debug.Assert ( _
Collat.Curr_Balance(1) = 0.5 And _
Collat.Curr_Balance(2) = 0.5)
End Sub
Actaully Per the second question I linked has the correct answer which I missed.
All subs need to start with 'IModel_' and rest ot the name has to match the name in IModel.
AND
This is the part i missed, you cannot use underscore in the Sub name.

use module class in vba

I have a module class file "CombinaisonLine" in my class modules folder :
Private pAdult As String
Private pChild As String
Public Property Get Adult() As String
Adult = pAdult
End Property
Public Property Let Adult(Value As String)
pAdult = Value
End Property
Public Property Get Child() As String
Child = pChild
End Property
Public Property Let Child(Value As String)
pChild = Value
End Property
In my modules folder, I have a function called when I click on a button in my sheet :
Function Test()
Dim Line As CombinaisonLine
If (Sheets("Feuil1").Cells(3, 6).Value = "YES") Then
Line.Adult = "1"
Line.Child = "0"
End If
End Function
I am getting an error 91 at line "Line.Adult="1"" with the following message (I am using a french version, so I have translated the message into english):
execution error "91":
Object variable or Bloc variable With not defined
I don't know what I am missing. Thanks in advance for your help
You need to create object of class CombinaisonLine first, and destroy when you do not need it:
Function Test()
Dim Line As CombinaisonLine
Set Line = New CombinaisonLine
If (Sheets("Feuil1").Cells(3, 6).Value = "YES") Then
Line.Adult = "1"
Line.Child = "0"
End If
Set Line = Nothing
End Function

VBA calling class let method, compile error

Beginner to excel class modules here. I am having trouble with the basics-
When I set (let) the property, I get "Compile error: Wrong number of arguments or invalid property assessment" with the .Name property:
Sub test()
Dim acc As account
Set acc = New account
MsgBox (acc.Name("First Account").rowNum())
End Sub
And this is the "account" class module:
Private strAccName As String
Private mlngRowNum As Long
Public Property Let Name(strN As String)
strAccName = strN
End Property
Public Property Get rowNum(exists As Boolean)
dim rowNum as Long
'...some logic here...
'...
getRowNum = rowNum
End Property
So supposedly I am going wrong in the Let method? Advice greatly appreciated
you can assign a value to a property LET (for normal dataTypes) or property SET (for Object) by the equal sign, not vith parenthesis (used for method instead), or read a property GET assigning the value to another variable, like this:
acc.Name = "xyz"
MsgBox acc.Name
This might help you:
Sub test_class()
Dim acc As account
Set acc = New account
acc.Name = "First Account"
MsgBox acc.rowNum(1)
End Sub
class (account):
Private strAccName As String
Private mlngRowNum As Long
Public Property Let Name(strN As String)
strAccName = strN
End Property
Public Property Get rowNum(exists As Boolean)
'Dim rowNum As Long
'...some logic here...
'...
If exists Then
'getRowNum = rowNum
rowNum = 5
Else
rowNum = 10
End If
End Property