Using VBA to fill table w/field names in an Array

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
I am trying to fill a table with VBA/DAO and have many field names. I can enter each field using the following, as examples...
Code:
rs![ItemNo] = 1
rs![ItemAge] = 1

However, I would like to put "ItemNo" and "ItemAge" and all the others into an array, like
Code:
strTemp = Array("ItemNo", "ItemAge"....)

Then I'd be able to use a loop to fill the data, rather then many individually written lines.

I believe this is possible, but don't recall how.

Thanks!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi,

Your question seems incomplete. If you want to make all fields equal to 1 then you can do so easily:

Code:
[COLOR="Navy"]Sub[/COLOR] Test()
    
    [COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
    [COLOR="Navy"]Dim[/COLOR] rs [COLOR="Navy"]As[/COLOR] DAO.Recordset
    
   
    [COLOR="SeaGreen"]'//Open table[/COLOR]
    [COLOR="Navy"]Set[/COLOR] rs = CurrentDb.OpenRecordset("Table3", dbOpenTable)
    
    [COLOR="SeaGreen"]'//Create record with all fields to 1[/COLOR]
    [COLOR="Navy"]With[/COLOR] rs
        .AddNew
        [COLOR="Navy"]For[/COLOR] i = 0 [COLOR="Navy"]To[/COLOR] .Fields.Count - 1
            .Fields(i) = 1
        [COLOR="Navy"]Next[/COLOR]
        .Update
    [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
    
    rs.Close

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

But I can't imagine that's what you really want. In any case, you could do the same without VBA by just setting the default values for the fields in the DB. Maybe that's what you really need?
 
Upvote 0
Not all values will be 1, they'll vary.

i revised my code to save the data in an excel spreadsheet...and then TransferSpreadsheet, as the fields won't always be in sequential order.

Thanks!
 
Upvote 0
Hi,
Good deal! Post back if any other problems.
ξ
 
Upvote 0
TIP:

You can use also reference a field in a recordset like this:

Code:
rs("ItemNo") = 1


or

Code:
Dim strFieldName as String

strFieldName = ItemNo

rs(strFieldName) = 1
 
Upvote 0
It may be possible on some type of loop. It will depend on how you will get the values for each field.

Would you please provide for details.
 
Upvote 0

Forum statistics

Threads
1,221,900
Messages
6,162,691
Members
451,782
Latest member
LizN

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top