Having a variable reference an object in VBA - vba

I'm writing some VBA code to dynamically position some elements on a form. What I would like to do is create a two dimensional array of references to 11 rows of label and text box objects, then step through the array and position each "row" differently based on the properties of the above rows.
Unfortunately, I cannot seem to find a way to store the references! The following code
produces the error: "Object doesn't support this property or method"
Dim fields(10,10)
fields(0,0) = txtFirstName
If I assign an object to a single variable like so
Dim field
field = txtFirstName
MsgBox field.Top
Then I get no error message from the assignment, but the reference to field.Top gives me the error "Object Required".
Please, what am I doing wrong here. I'm new to stack overflow and VBA coding so forgive me if this is an easy fix, but I've leveraged my google-fu and I'm just not finding a solution. Many thanks to anyone who can help.

You need to add the Set keyword, since you are dealing with object references.
Set fields(0,0) = txtFirstName
The reason you get the error, is because VBA will default to the text property of the textbox, giving you a string instead of the textbox object.

Related

Retrieve Column 1 value from VBA ComboBox after using "combobox.value =" to modify selected index value

Hello I am working with VBA and I have run into a strange situation. I am hoping that someone out there has run into the same thing and can give me a little help. In my program, there is a 2 column combo box that has it's value set to a string. The value is set like this:
ComboBox1.Value = "this_string"
Initially this ComboBox has 2 legit columns. I am able to confirm this by using the Immediate Window Later in code the first column of that combo box is checked. For example:
value = ComboBox1.Column(0)
But the code breaks here. I get this error:
Could not get the Column property. Invalid property array index.
After the value assignment. I can no longer get any Column values. Why is this?
Thank you for your help,
Billy
This question should not have a vb6 tag. - Unlike VBA, Vb6 only has single column comboboxes.

How to make Listbox.List keep type information?

If I affect a full array to a ListBox, using ListBox1.List = [{1;2;3;4}] for example, the Listbox's .List items keep their correct type (here numbers), but if I use ListBox1.AddItem 5 or Listbox1.List(5) = 6 to set an individual item the type is automatically changed to String.
Sample Code:
Private Sub UserForm_Initialize()
ListBox1.List = [{1;2;3;4}]
ListBox1.AddItem 5
ListBox1.AddItem
ListBox1.List(5) = 6
End Sub
Later on, when comparing values, I get wrong results because numbers are not equals to text (5 <>"5").
Is there any easy (1) way to ensure the type of the list items is not converted to String?
(1) I know I can explicitly make the conversion to String, but I rather keep my values as numbers instead of "numbers-strored-as-text" in the listbox
I guess that will be impossible when using AddItem. According to https://learn.microsoft.com/en-us/office/vba/api/access.listbox.additem, the first parameter is a string, so everything you pass will be converted to a string.
Probably your best bet is to collect all items in an array and assign the array using ListBox1.List. Or you have to live with numbers stored as string...
Update
I mixed the pages up - link to the Access page was wrong.
Anyhow, Documentation is rather poor. It says "valid object" but doesn't define what that means. At least it is not possible to add a object - that throws a type mismatch.
Also, the documentation states that a Variant is returned, but it seems thatis not true - when I try to get the result, the compiler throws an error.
As a conclusion, I assume that AddItem converts everything to a string (and throws an error if that fails). So I still assume that you have to build up an array and assign it to List if you want to have real numbers.

Read/get the current value of an object in Project VBA

I'm struggling to save the current value of an object (at least I think it's an object) in MS Project VBA.
Here's what I'm trying to do, save the value of my current header text.
dim a As String (or Variant/Object?)
a = Application.FilePageSetupHeader.Text.Value
This obviously doesn't work, but gets the point across.
The syntax for this is:
expression. FilePageSetupHeader( ** Name, ** Alignment, ** Text** )
with Name and Text being passed as a string.
If I get this to work, I'll also be interested in extracting the NormalType value from this object. Notice I'll have to specify the item:
GridlinesEditEx Item:=4, NormalType:=1
Any help would be appreciated!
FilePageSetupHeader is a method of the application object, not a property (see MSDN). The MS Project object model does not have a header object to interrogate, so unfortunately the headers (and footers) are write-only.

Select lists with specific numbering style

When using the following condition
Set r = ActiveDocument.Paragraphs(i).range
If r.ListFormat.ListTemplate.ListLevels(1).NumberStyle = wdListNumberStyleLowercaseRoman Then
I get
Run-Time error '91': Object variable or With block variable not set
I managed to create a similar macro for lists with specific bullets using AscW check, but here already wasted a whole day and still no working result.
What an I doing wrong and how to do it right?
Is it possible to use values from this table instead of wd-names? For wdListNumberStyleLowercaseRoman that would be "2" according to the table.
Using class ListParagraphs instead of Paragraphs will make it work both with wd-names and their values.

Add a row using linq or sql in VB.NET

I am developing an application, and as one of the steps, I need to have a set of information backed up to an SQL database.
I don't have any experience using SQL, and was hoping I could find a simple command that would just add a row with the information i'd needed... But I don't understand much of the information I've found for it. I've even had issues just using the snippet provided within Visual Studio.
I tried using the snippet for adding a row, and here's the code that's produced.
The reference to RDataSet.ArchivedIncidentsDataTable keeps giving me an error message telling me "object reference to non-shared object requires an object reference", which to me, is one of the least helpful error messages you could possibly receive. I don't understand what it means, or what I need to do in order to fix it.
Here is the code that I have, which is basically just the snippet that's provided:
Private Sub Save_SQLBackup_Info()
Dim newRow = CType(RDataSet.ArchivedIncidentsDataTable.NewRow(), RDataSet.ArchivedIncidentsRow)
newRow.CustomerID = "A124"
newRow.CompanyName = "Acme"
RDataSet.ArchivedIncidentsDataTable.Rows.Add(newRow)
End Sub
I'm not sure how filling in the fields for this small amount of code produced an error.
Also, after this segment of code is run, don't I have to run a command to update the database? The guides I'd been following online all referenced something like that, but I didn't understand the other parts of them.
I'm sure the problem i'm having is a matter of "You didn't declare X.", however I don't know much about SQL... If that's even what I'm using.
I would appreciate some guidance.
All I need to do is add a row to the database using a few fields from the form.
I have an online SQL server linked as a data object, and the dataset is titled RDataSet , the data table is titled ArchivedIncidentsDataTable.
The few guides I've found online reference different parts, like data adapters, that I don't have, and / or don't reference how they got to that part... I'm completely lost.
Is there any sort of one-line command I can run that will just shoot the information I'd like added to a row into this database? I don't understand why it has to be this complicated...
EDIT:
One of the answers was to drag an instance of the dataset onto the form.
After doing this, and trying to reference it, I'm now getting an error message telling me :
"Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
This message appears under the autocorrect options, and gives me the option to replace the dataset I added to the form, RDataSet1, with RDataset.
When hovering over the code "RDataSet1.ArchivedIncidentsDataTable.NewRow()", I still see the error message "Object Reference requires an object reference".
The message
object reference to non-shared object requires an object reference
means you're trying to access a member of a class like it's a shared member, but it's not, so you need an actually instance of that class (an object reference).
If you're using WinForms, you can just drag a RDataSet onto your Form, and it should generate a field in your Form called RDataSet1 or something like that (I asumme you used the DataSet Designer of Visual Studio).
Then use RDataSet1 (the object reference) instead of RDataSet (the type).