Solved

For...Next Syntax Question VBA

Posted on 2013-05-29
6
636 Views
Last Modified: 2013-05-29
I've taken over some systems at a college and some of the Access databases contain some code which has not been touched for years because "it works" and everyone is too afraid to alter it! Basically, the code takes a student's selected courses and allocates them to classes which is a lot more complex than it initially sounds. Anyway, I can figure out a lot of it, but I am a little perplexed by the code shown below which crops up in similar format throughout:

    For i = 1 To 4: choices(i) = "": pos(i) = 0: groups(i) = 0: Next i

Open in new window


It looks to me like the For...Next loop is being compressed into a single line. Would I be correct? In theory I should be able to expand the code out to a more standard four lines. I just want to make sure that my assumption is correct before messing with code which is nearly two decades old!
0
Comment
Question by:Karl_mark
6 Comments
 
LVL 12

Accepted Solution

by:
pdebaets earned 250 total points
ID: 39203781
Yes. The colon is a statement separator, allowing you to put more than one statement on a line. You could write the same as

For i = 1 To 4
    choices(i) = ""
    pos(i) = 0
    groups(i) = 0
Next i

Open in new window


Looks like some arrays are being initialized.
0
 

Assisted Solution

by:dtelfer
dtelfer earned 250 total points
ID: 39203782
Hi Karl,

you are completely corect.  the colon (":") acts as a command seperator in many dialects of BASIC, allowing multiple lines of code to be specified on a single source file line.  

You can separate the code into five lines by replacing the colons with carriage returns, which will substantially improve readability for you and the rest of your team.

For i = 1 to 4
   choices(i) = ""
   pos(i) = 0
   groups(i) = 0
Next i

Open in new window

0
 
LVL 77

Expert Comment

by:peter57r
ID: 39203783
Yes -it's exactly the same as a vertically presented set of 5 lines .
0
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.

 
LVL 15

Expert Comment

by:Simon Ball
ID: 39203795
I assumed the : was a terminator but did a little accdb investigation to see if i could test the loop and output results using a "normal" for/next to get it to run in access 2010, and I get "subscript out of range" on . : pos(i) = 0

Not used arrays in Access for a long time though so it could be my error... SO you might be wise to test loops with ":" in in newer versions of access if you are using an older version and intending to upgrade.
0
 

Author Closing Comment

by:Karl_mark
ID: 39203797
Thanks all. I'll set about making the code more legible. It was written by a mathematician back in around 1992, and I'm trying to fathom how it actually does the allocation; there are lots of number variables thrown in with no commenting, so I'm adding some logfiles to see how it processes the records.
0
 
LVL 15

Expert Comment

by:Simon Ball
ID: 39203830
Sounds fun! Good luck :)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
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.
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…

786 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