Assigning object reference to a Variant/Long - vba

I have sometime some error like this: "Required object" (translate from my french error.)
But I can't understand what is exactly an object in VBA (with Access, if it's change something).
For example, I have do an SQL SELECT with ADODB.recordset and I see in the spy of the Access IDE for VBA, the value is an "Variant/Long". Okay, I create an Long variable and I set the value of query in the long variable.
Set userId = RS.Fields(0).value
But it's doesn't work...
At the start, I have think: the object is like an array. But the array already exist in VBA.
Someone can help me ?

The Long data type can hold a 32-bit integer. It isn't an object reference, so don't use Set.
Dim userId As Long
userId = RS.Fields(0).value
For more on what is and isn't an object, see Data Type Summary for VBA.

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

VBA How to programmatically set objects name?

In FactoryTalk View SE I’m trying to set an objects name in VBA based on another value.
This works:
Dim PumpSP As Object
Set PumpSP = ThisDisplay.PumpSetpoint1
PumpSP.Vaule = 10
This doesn’t work:
Dim PumpSP As Object
Set PumpSP = "ThisDisplay.PumpSetpoint" & "1"
PumpSP.Vaule = 10
How do I get it to take a concatenated string?
Thanks.
How do I get it to take a concatenated string?
You don't, and you can't. PumpSP is an Object, not a String. The only thing you can ever Set it to, is an object reference.
This is very similar to, say, how you would access the controls on a UserForm:
Set box = Me.TextBox1
The reason you can get that object with a string, is because a form has a Controls collection that lets you provide a string, works out which control has that name, and returns the Control object for it:
Set box = Me.Controls("TextBox" & i)
So in your case to go from this:
Set PumpSP = ThisDisplay.PumpSetpoint1
I don't know what ThisDisplay is, but maybe it has a similar collection:
Set PumpSP = ThisDisplay.Controls("PimpSetpoint" & i)
If there's no collection that lets you retrieve items by name with a string literal, you're out of luck.
That is the hard part as we have paid support with Rockwell but they will not help with any VBA questions. Ok fine but there VBA commands and class are not like 90% of the Excel or MS Office VBA you find online. Anyway I was able to find the correct class member.
So this work for me:
Set PumpSP = ThisDisplay.FindElement("PumpSetpoint" & “1”) Thanks for all the help.

Difference between Long and Object data type in VBA

In VBA, the Long and Object data type are both 4-bytes, which is the size of a memory address. Does this mean that, technically, the Object data type doesn't do anything that a Long couldn't do? If yes, then is it safe to say that the Object data type exists simply to make it easier for the programmer to distinguish between the purpose of the variable?
This question came up as I was considering Win32 API function declarations. They are often times declared as Long, and, unless I am mistaken, their return value is simply a memory address. Seems like defining these functions as Object would have been more appropriate, then.
Am I totally off? Thanks in advance.
Based on VBA/MSDN help:
Long (long integer) variables are stored as signed 32-bit (4-byte)
numbers ranging in value from -2,147,483,648 to 2,147,483,647.
and the other definition:
Object variables are stored as 32-bit (4-byte) addresses that refer to
objects. Using the Set statement, a variable declared as an Object can
have any object reference assigned to it.
From practical point of view they are both different and used in different situation. Which are essential: Long >> refers to numbers and Object >> refers to object.
Look into the following VBA code (for Excel) where I added comments which is allowed and which is not:
Sub test_variables()
Dim A As Object
Dim B As Long
'both below are not allowed, throwing exceptions
'A = 1000
'Set B = ActiveSheet
'both are appropriate
Set A = ActiveSheet
B = 1000
End Sub
Finally, in terms of API it's better to stay with original declaration and not manipulate with that to avoid any risk on unexpected behaviour of API functions.

Object variable or With Block variable not set error when accessing members of BuildingBlockEntries collection

I am attemping to access members of a template's BuildingBlockEntries collection through a macro in Microsoft Word 2007. As it is a collection, I first thought a For Each loop would be the best way to to this:
For Each bBlock In NormalTemplate.BuildingBlockEntries
MessageBox.Show (bBlock.Name)
Next bBlock
However this attempt through the error: Object doesn't support property or method.
So I tried this method which was suggested here:
Templates.LoadBuildingBlocks
Dim iBB As Integer
iBB = NormalTemplate.BuildingBlockEntries.Count()
Dim bb As Word.BuildingBlock
Dim i As Integer
Dim objCounter As Object
If iBB > 0 Then
For i = 1 To iBB
objCounter = i
bb = NormalTemplate.BuildingBlockEntries.Item(objCounter)
MessageBox.Show (bb.Name)
Next
End If
However, this is resulting in the error shown in the title: Object variable or With Block variable not set.
I have tried just using an integer variable for the index, i specifically, but with now avail. How can I acheive the desired effect? What is wrong with my attempt?
Thank you for the help.
With your 2nd question the answer is that you need to use Set, as bb is an object:
Set bb = NormalTemplate.BuildingBlockEntries.Item(objCounter)
For more info on Set take a look at this SO question.
With your For/Next loop, it's not clear how you've declared bBlock. I guess it should be something like:
Dim bBlock as BuildingBlock
And perhaps the For line should reference BuildingBlocks instead of BuildingBlockEntries:
For Each bBlock In NormalTemplate.BuildingBlocks
I don't know for sure though, as I'm just looking at what pops up in Intellisense.

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.