Solved

Reading an array and outputting a record for each column in VBA

Posted on 2014-04-18
6
847 Views
Last Modified: 2016-02-13
Experts,

I have this code in SAS which read a record that has 15 msg columns
MSG0 - MSG15.  In SAS I can use the following code to generate an output line for each message,

Data Merge_Edits_Array;
      Set Edits;
      ARRAY EditsArray MSGNO1 - MSGNO15;
Do x = 1 to 15;
      Edit = EditsArray[x];
      If Edit ~= " " then OUTPUT;
END;
Drop x MSGNO1 - MSGNO15;
Run;      

Is there a way to do this in VBA.  I would like to use this in ACCESS if possible.
0
Comment
Question by:morinia
  • 2
  • 2
  • 2
6 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 300 total points
ID: 40008947
If you always have 15, you can do this:

Dim i as Integer

For i = 1 to 15
  Msgbox yourarray(i)
Next

I'm not sure what you mean by "output" line - can you explain a little further exactly what you want to do with each element of that array?

If your number of elements in the array is variable, you'd have to change that a bit:

Dim i As Integer

For i = 0 to UBound(YourArray)
  Msgbox YourArray(i)
Next i

Note too that this depends on how you're getting that array from SAS, and whether the object type is compatible with VBA.
0
 

Author Comment

by:morinia
ID: 40008969
Scott,

By output I mean to write a record.  I will then have a file with an ID number and multiple lines representative of each value in the array.

Input  
001   001  055 075

Output
ID      Array_value
001        001
001        055
001        075
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40009196
There is no concept of a column array because the tabledef/querydef collection is one and if you had sub arrays that included only some of the columns, that would violate first normal form and therefore would simply not be supported by relational database.

Take a look at the querydefs/tabledefs/ fields collection.  There are samples in help.  From your example it looks like the first column is the Foreign Key and the other columns are all instances of some value.  So you can reference the first column specifically by name and the others in a loop starting at 1 or 2 depending on whether your array is 0 or 1 based.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 84
ID: 40009318
You'd have to give us a little more information about the structure, both incoming and output. As Pat mentioned, it looks like your first value if a Foreign Key, but that's hard to say unless we know more about it.
0
 

Author Comment

by:morinia
ID: 40009377
Using the code Scott gave as a base, how would I write out the record in VB to table in ACCESS instead of displaying in in MSGBOX.  I think with that I can figure the rest out.

Dim i As Integer

For i = 0 to UBound(YourArray)
  Msgbox YourArray(i)
Next i
0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 200 total points
ID: 40009407
You need to open a recordset using DAO or ADO.  Then inside the loop, you would write the record to the recordset instead of displaying it.

Dim i As Integer
Dim db As DAO.Database
Dim td As DAO.TableDef
Dim rs As DAO.Recordset

Set db = CurrentDB()
set td = db.TableDefs!yourtablename
Set rs = td.OpenRecordset

For i = 0 to UBound(YourArray)
    rs.AddNew
        rs.fld1 = YourArray(i)
    rs.Update
Next i 

Open in new window

0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now