How Do I fill in this Structure String Array in vbNET? - vb.net

Visual studio tells me the variable must be declared even though it already is.
I filled in a structured array in a similar way using a loop though the type was an Int.
I do not want to use a loop this time just hard code it.
Structure Sentence
Dim strWord As String
End Structure
Dim strArticles(1) As Sentence
strArticles(0).strWord = "The"
Thanks

Are you defining the Structure in your method body? It must be defined outside of a method, either in a module ore a class. See this example.
This works just fine:
Module Module1
Sub Main()
Dim s = New Sample()
s.DoIt()
End Sub
End Module
Class Sample
Structure Sentence
Dim strWord As String
End Structure
Public Sub DoIt()
Dim strArticles(1) As Sentence
strArticles(0).strWord = "The"
Console.WriteLine(strArticles(0).strWord)
End Sub
End Class

Related

Calling a function of a classmodul

probably just a stupid syntax error but when I try to call a function i created in a class module I get the error message that my "objectvarable or withblock is not declarde".
Here the minimal code example from both modules:
'calling
Dim AllZyklen1 As New ArrayList
For Each Wartungsplan In ArrayWartungsplan
Set AllZyklen1 = Wartungsplan.GetAllZyklen 'added set
next Wartungsplan
'function itself
Public Function GetAllZyklen() As ArrayList
Dim AllZyklen2 As New ArrayList
'allZyklen2 gets calculated, no other functions are called just local varaibles of the class are used
If Not AllZyklen2.Contains(Zyklus) Then
AllZyklen2.Add Zyklus
end if
Set GetAllZyklen = AllZyklen2 'added set
End Function
(numbers are added to "allzyklen" just for easier reading, they are actually both called "allzyklen" without number)
Shouldnt that work? I just cant see the error.
EDIT: As for the Solution, what the answer states is absolutley correct and was necessary for my code to work. Unfortunatley I also had an spelling error for a attribute in the classmodule. In which case vba just highlights the call of this function, but no errors within the function... I ended up moving the function from the classmodule to the main module where the correct line with the spelling error got highlighted and the mistake was easier to spot.
You need to use Set for Objects (ArrayList is an object).
So it should be:
'calling
Dim AllZyklen1 As New ArrayList
For Each Wartungsplan In ArrayWartungsplan
Set AllZyklen1 = Wartungsplan.GetAllZyklen
Next Wartungsplan
and
'function itself
Public Function GetAllZyklen() As ArrayList
Dim AllZyklen2 As New ArrayList
'allZyklen2 gets calculated, no other unctions are called just local varaibles of the class are used
Set GetAllZyklen = AllZyklen2
End Function
Full example that works:
Class Module ClassWartungsplan:
Option Explicit
Public Function GetAllZyklen() As ArrayList
Dim AllZyklen2 As New ArrayList
'allZyklen2 gets calculated, no other unctions are called just local varaibles of the class are used
AllZyklen2.Add "abc"
Set GetAllZyklen = AllZyklen2
End Function
Standard Module:
Option Explicit
Sub Example()
Dim AllZyklen1 As New ArrayList
Dim Wartungsplan As New ClassWartungsplan
Set AllZyklen1 = Wartungsplan.GetAllZyklen
Debug.Print AllZyklen1(0) ' prints ABC in the immediate window
End Sub

Using MSscriptControl in VB.net adding an object gives Specified cast is not valid error

Here a sample code that produces the error. I have used MSscript in VB projects in the past and those projects are functioning.
The error reported is: "When casting from a number, the value must be a number less than infinity"
Or if anyone has another suggested way to easily add scripting to a project.
Private Sub Run_Script()
Dim scriptEngine As New MSScriptControl.ScriptControl()
Dim TestClass As New Sample
Dim ScriptCode As String
scriptEngine.Language = "VbScript"
scriptEngine.AddObject("Test", TestClass, True)
ScriptCode = "MsgBox ""tests"" "
scriptEngine.AddCode(ScriptCode)
End Sub
End Class
Public Class Sample
Public Sub Test()
MessageBox.Show("This is a test")
End Sub
End Class
I found the answer. I needed to set com visible to true. This is found under "Assembly Information"

VBA use string for range

I need help for my VBA tool.
In my program I define a string keyword:
Public Sub SetKeyword()
Call Define_Variables.variables
Call Database.Allocation
strKeyword="KINDACC"
Now I would like to set the name of the range keyword with my string keyword, so it looks like this:
Public Sub SetKeyword()
Call Define_Variables.variables
Call Database.Allocation
strKeyword="KINDACC"
rngKeyword= r_KINDACC
End Sub
In my module "Database" I have those r_strKeywords already defined:
Sub Allocation()
'General accident information
...
Set r_KINDACC = Worksheets("Database").Range("N4:N14")
End sub
So later with my GUI I would like to define a string-keyword and my program automatically defines my range-keyword with my string keyword:
Public Sub SetKeyword()
Call Define_Variables.variables
Call Database.Allocation
strKeyword=blabla
rngKeyword= r_strKeyword
End Sub
Is that possible?
I hope it's not too confusing
thanks!!
Viktoria

Getting 'SubScript Out of Range' Error in VBA when calling class Method

I'm going to try to lay out this issue in as much detail as possible. Apparently I can't post images, as I am a New Member...So I will try to describe my situation as good as I possibly can.
So, I am working with a custom class called "Shifts". I have this class declared and set up in a Class Module in VBA. I declare an instance of the "Shifts" class inside a normal Module and call it "Shift".
Dim Shift As New Shifts
My "Shifts" class has 4 variables (String Arrays):
Private ShiftMembers() As String
Private ShiftCallSigns() As String
Private ShiftAssignments() As String
Private ShiftStatuses() As String
I have written a Sub within the class called "Clear" to clear the data in these variables (via ReDim):
Public Sub Clear()
ReDim ShiftMembers(-1) As String
ReDim ShiftCallSigns(-1) As String
ReDim ShiftAssignments(-1) As String
ReDim ShiftStatuses(-1) As String
End Sub
Now, when I call the Clear Sub of the "Shifts" class (declared as "Shift"):
Shift.Clear 'This is called from within the Module.
i get Subscript out of range (Error 9). My class is declared at the very top of the module, outside of any methods or functions. The Clear() sub within my class is declared Public. I don't understand why I can't seem to access my Clear Sub properly.
Any help would be greatly appreciated!
-Rob
I don't know if you can use ReDim that way. From the documentation, the subscripts argument is bound by the Option Base statement:
subscripts
Required
Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax: [lower To] upper [,[lower To] upper] . . . When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present.
The Option Base statement must be either 1 or 0, and -1 is out of bounds, so the error makes sense.
In order to clear your arrays use the Erase command instead of ReDim.
I put this in my class module (I use the _Initialize procedure just to make one of the array's non-empty, in order to verify that Erase did its job):
Private ShiftMembers() As String
Private ShiftCallSigns() As String
Private ShiftAssignments() As String
Private ShiftStatuses() As String
Sub Class_Initialize()
ReDim ShiftMembers(1 To 2)
End Sub
Public Sub Clear()
Erase ShiftMembers
Erase ShiftCallSigns
Erase ShiftAssignments
Erase ShiftStatuses
End Sub
I use the following to test:
Sub t()
Dim s As New Shift
s.Clear
End Sub

Calling a Sub or Function contained in a module using "CallByName" in VB/VBA

It is easy to call a function inside a classModule using CallByName
How about functions inside standard module?
''#inside class module
''#classModule name: clsExample
Function classFunc1()
MsgBox "I'm class module 1"
End Function
''#
''#inside standard module
''#Module name: module1
Function Func1()
MsgBox "I'm standard module 1"
End Function
''#
''# The main sub
Sub Main()
''# to call function inside class module
dim clsObj as New clsExample
Call CallByName(clsObj,"ClassFunc1")
''# here's the question... how to call a function inside a standard module
''# how to declare the object "stdObj" in reference to module1?
Call CallByName(stdObj,"Func1") ''# is this correct?
End Sub
I think jtolle's response addressed the question best - the small reference to Application.Run may be the answer. The questioner doesn't want to use simply func1 or Module1.func1 - the reason one would want to use CallByName in the first place is that the desired function.sub name is not known at compile time. In this case, Application.Run does work, e.g.:
Dim ModuleName As String
Dim FuncName As String
Module1Name = "Module1"
FuncName = "func1"
Application.Run ModuleName & "." & FuncName
You can also prepend the Project Name before the ModuleName and add another period ".".
Unfortunately, Application.Run does not return any values, so while you can call a function, you won't get its return value.
Although it is an old question and OP asked for CallByName in a standard module, the correct pieces of advice are scattered through answers and comments, and some may not be that accurate, at least in 2020.
As SlowLearner stated, Application.run DOES return a Variant, and in that way both branchs below are equivalent, except by handling errors, as commented around Horowitz's answer:
Dim LoadEnumAndDataFrom as Variant
'FunctionName returns a Variant Array
if fCallByName then
LoadEnumAndDataFrom = CallByName(ClassObj, "FunctionNameAtClass", VbMethod)
else
'After moving back function for a standard module
LoadEnumAndDataFrom = Application.Run("StandardModuleName" & "." & "FunctionNameAtStandard")
endif
I actually just did this above and had no errors at all, tested in Word, Excel and Access, and all return the same Array.
Unfortunately, there is an exception: Outlook's object Model is too protected and it does not have the Run method.
CallByName works only with class objects.
If your subroutine is in a standard module, you can do this:
Sub Main()
Module1.Func1
End Sub
If it's a function, then you'll probably want to capture the return value; something like this:
Sub Main()
Dim var
var = Module1.Func1
End Sub
Modules in VB6 and VBA are something like static classes, but unfortunately VB doesn't accept Module1 as an object. You can write Module1.Func1 like C.Func1 (C being an instance of some Class1), but this is obviously done by the Compiler, not at runtime.
Idea: Convert the Module1 to a class, Create a "Public Module1 as Module1" in your Startup-module and "Set Module1 = New Module1" in your "Sub Main".
Unfortunately it is not possible to prepend the ProjectName before the ModuleName and add another period "." In MS Word this throws a runtime error 438. The call is restricted to the use of simply ModuleName.ProcName.