Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

For...Next Syntax Question VBA

Posted on 2013-05-29
6
Medium Priority
?
669 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
[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
6 Comments
 
LVL 12

Accepted Solution

by:
pdebaets earned 1000 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 1000 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
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…
Suggested Courses

722 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