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
Solved

For...Next Syntax Question VBA

Posted on 2013-05-29
6
646 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

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.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

861 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