Object required when retrieving value from InputBox [duplicate] - vba

This question already has answers here:
Object Required Error When Declaring String Variable
(1 answer)
VBA Excel "Compile error: Object Required"
(2 answers)
Closed 1 year ago.
Hey guys I have a a coding issue in vba.
Dim table As String
Set table = InputBox("Text","Title","Default")
I then get the message:
Error while compiling
Object required
The phrase
table =
Is marked blue
Why do I get the error while compiling?

If InputBox does return a String you have to remove the Set (which is only needed for objects):
Dim table As String
table = InputBox("Text","Title","Default")

The Set statement is used with variables that are an Object type.
Per the VBE Glossary for object-type;
A type of object exposed by an application through Automation, for example, Application, File, Range, and Sheet. Use the Object Browser or refer to the application's documentation for a complete listing of available objects.
A String is not an object data type so you don't need to include the Set keyword.

Related

In ETAS INCA, how can I extract specific calibration values by accessing the COM-API via VBA?

I'm trying to construct a VBA script that opens INCA and extracts specific calibration values.
To start, I found a Youtube video by ETAS (https://www.youtube.com/watch?v=OQAvE0UT5xk), and I copied the code from there:
Private Function inca_com_api() As Variant
Dim Inca As Object
Dim DB As Object
Dim DBname As String
Dim devices() As Variant
Dim calValue As Variant
Set Inca = CreateObject("inca.inca")
Set DB = Inca.GetCurrentDataBase
DBname = DB.GetName
inca_com_api = DBname
Inca.DisconnectFromTool
End Function
The code opens INCA and runs as intended, returning the database name, but what I want is not the database name, but specific calibration values.
I was under the impression that I could just use the API functions listed in the User's Guide for INCA-MIP (https://www.etas.com/en/downloadcenter/18008.php), which is the INCA COM-API interface package for Matlab.
I don't have INCA-MIP, I just thought I could call those functions through VBA, by using the same function name or similar function names.
But when I try code like:
devices() = DB.IncaGetDevices
or
devices() = DB.GetDevices
or
devices() = Inca.IncaGetDevices
or
devices() = Inca.GetDevices
or the same variations of GetCalibrationValue, IncaGetCalibrationValue, I get:
Run-time error '438': Object doesn't support this property or method
I was planning to use IncaGetDevices to show me a device name, which I could then use as an input argument for IncaGetCalibrationValue, for each of my specific calibration names that I want returned.
Again, I don't have INCA-MIP, but I am wondering how I can return these calibration values using the VBA and the COM-API for INCA?
Also, I tried looking through the following posts for answers:
In ETAS INCA, is there a way to quickly retrieve an item from an arbitrary path in an ASAP2Project?
In ETAS INCA, what classes correspond to each type of database item?
but I don't really know what they're talking about.
Any help would be appreciated, thanks

Why doesn't a String of Nothing cause cause an exception? (BC42104) [duplicate]

This question already has answers here:
Difference between Equals/equals and == operator?
(11 answers)
Closed 3 years ago.
If I declare a string but do not assign a value, the Equals function throws an exception but it doesn't throw an exception if it is compared to a value.
The error list warns about the problem:
Warning BC42104 Variable a is used before it has been assigned a
value. A null reference exception could result at runtime.
Dim a As String
Dim b as string = "bar"
a.Equals("foo") 'causes System.NullReferenceException
a = "foo" 'No exception although a is nothing
a = b 'No exception although a is nothing
I know the warning says it COULD cause an exception, but does anyone know why this is the happening?
This is because Dim a As String declares the type of variable a but doesn't assign anything to it. This is basically saying : "This variable was made to hold a String object, but it doesn't hold any right now". On the other hand, Dim b As String = "bar" declares the variable and its type, but also assigns a String object to it ("bar"). The reason a.Equals("foo") returns an exception is because you only declared it without assigning anything to it (so you are trying to access an object that isn't there). a = "foo" works because you are assigning a String object of value "foo" to the variable a. It's like saying : "This variable now holds a String object with value 'foo'".
Edit :
While your code points to the assignment of the variable a, I was made aware that you wanted to know why the = operator, as a comparison operator, works. This is because what I said earlier isn't entirely true when I said it didn't hold an object. It actually is of Nothing value (which sets it as a null reference) if no String object was assigned to it (String is a nullable object).
See : https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing
Hope this helps.

Make a string equal to what a named range refers to?

I have a named range called tempPrintArea
it refers to ="A1:J59"
I created it with VBA and the scope became local. I don't know if the scope matters.
I used this line to create the named range:
wks.Names.Add Name:="tempPrintArea", RefersTo:="A1:J59"
Now I want to set the value of a string to "A1:J59". I've tried the following:
Dim test As String
test = Range("tempPrintArea").RefersTo
but I get the error message Method 'Range' out of object '_Gloabl' failed
What can I change in these code lines to get this working?
When you're using
wks.Names.Add Name:="tempPrintArea", RefersTo:="A1:J59"
code doesn't create named range that refers to A1:J59, instead code creates named range with text "A1:J59".
For creating named range use this one instead:
wks.Names.Add Name:="tempPrintArea", RefersTo:=wks.Range("A1:J59")
and then
Dim test As String
test = wks.Range("tempPrintArea").Address(False, False) ' returns A1:J59

VBA Adding a class to a collection [duplicate]

This question already has answers here:
VB6 pass by value and pass by reference
(2 answers)
Closed 1 year ago.
I have a class module called Holding. In it are several public variables. My code is this:
Dim holdings as Collection
Dim h as Holding
Set holdings = new Collection
For i = 1 to last
Set h = new Holding
h.x = y
'... etc
holdings.Add(h)
Next i
This gives me error "object doesnt support this property or method" on the holdings.Add(h) line, but everywhere I look, it gives this exact example of how to achieve this. What am I missing?
Remove the parentheses.
holdings.Add h
Otherwise you are trying to add to the collection the value of the default property of your Holding instance, and it doesn't have a default property.

object reference not set to an instance of object [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 2 years ago.
I have been getting an error in VB .Net
object reference not set to an instance of object.
Can you tell me what are the causes of this error ?
The object has not been initialized before use.
At the top of your code file type:
Option Strict On
Option Explicit On
Let's deconstruct the error message.
"object reference" means a variable you used in your code which referenced an object. The object variable could have been declared by you the or it you might just be using a variable declared inside another object.
"instance of object" Means that the object is blank (or in VB speak, "Nothing"). When you are dealing with object variables, you have to create an instance of that object before referencing it.
"not set to an " means that you tried to access an object, but there was nothing inside of it for the computer to access.
If you create a variable like
Dim aPerson as PersonClass
All you have done was tell the compiler that aPerson will represent a person, but not what person.
You can create a blank copy of the object by using the "New" keyword. For example
Dim aPerson as New PersonClass
If you want to be able to test to see if the object is "nothing" by
If aPerson Is Nothing Then
aPerson = New PersonClass
End If
Hope that helps!
sef,
If the problem is with Database return results, I presume it is in this scenario:
dsData = getSQLData(conn,sql, blah,blah....)
dt = dsData.Tables(0) 'Perhaps the obj ref not set is occurring here
To fix that:
dsData = getSQLData(conn,sql, blah,blah....)
If dsData.Tables.Count = 0 Then Exit Sub
dt = dsData.Tables(0) 'Perhaps the obj ref not set is occurring here
edit: added code formatting tags ...
In general, under the .NET runtime, such a thing happens whenever a variable that's unassigned or assigned the value Nothing (in VB.Net, null in C#) is dereferenced.
Option Strict On and Option Explicit On will help detect instances where this may occur, but it's possible to get a null/Nothing from another function call:
Dim someString As String = someFunctionReturningString();
If ( someString Is Nothing ) Then
Sysm.Console.WriteLine(someString.Length); // will throw the NullReferenceException
End If
and the NullReferenceException is the source of the "object reference not set to an instance of an object".
And if you think it's occuring when no data is returned from a database query then maybe you should test the result before doing an operation on it?
Dim result As String = SqlCommand.ExecuteScalar() 'just for scope'
If result Is Nothing OrElse IsDBNull(result) Then
'no result!'
End If
You can put a logging mechanism in your application so you can isolate the cause of the error. An Exception object has the StackTrace property which is a string that describes the contents of the call stack, with the most recent method call appearing first. By looking at it, you'll have more details on what might be causing the exception.
When working with databases, you can get this error when you try to get a value form a field or row which doesn't exist. i.e. if you're using datasets and you use:
Dim objDt as DataTable = objDs.Tables("tablename")
you get the object "reference not set to an instance of object" if tablename doesn't exists in the Dataset. The same for rows or fields in the datasets.
Well, Error is explaining itself. Since You haven't provided any code sample, we can only say somewhere in your code, you are using a Null object for some task. I got same Error for below code sample.
Dim cmd As IDbCommand
cmd.Parameters.Clear()
As You can see I am going to Clear a Null Object. For that, I'm getting Error
"object reference not set to an instance of an object"
Check your code for such code in your code. Since you haven't given code example we can't highlight the code :)
In case you have a class property , and multiple constructors, you must initialize the property in all constructors.