Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-04-18
6
Medium Priority
?
880 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 1200 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 39

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 39

Assisted Solution

by:PatHartman
PatHartman earned 800 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Suggested Courses

636 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