Solved

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

Posted on 2014-04-18
6
868 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 85

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 37

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 85
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 37

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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