Solved

For...Next Syntax Question VBA

Posted on 2013-05-29
6
615 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

747 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

15 Experts available now in Live!

Get 1:1 Help Now