Actuarial Outpost
 
Go Back   Actuarial Outpost > Actuarial Discussion Forum > Software & Technology
FlashChat Actuarial Discussion Preliminary Exams CAS/SOA Exams Cyberchat Around the World Suggestions

DW Simpson International Actuarial Jobs
Canada  Asia  Australia  Bermuda  Latin America  Europe


Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-26-2008, 01:13 PM
Guest
Guest
 
Posts: n/a
Default Cleaning Up Code: Access VBA

I have several small snipets of code that are to be run if a box is checked.

Right now it simply goes down and says:

If Checkmark A is checked then
And a whole bunch of code
End If

I'd prefer it to look like this:

If CheckMark A is checked then Goto the area where the code is for checkmark A
If Checkmark B is checked then goto the area where the code is for checkmark B
etc..

It's just getting messay to read.
Reply With Quote
  #2  
Old 06-26-2008, 01:21 PM
Abused Student's Avatar
Abused Student Abused Student is offline
Member
SOA
 
Join Date: Feb 2007
Location: The Eighth Circle of Hell
Favorite beer: Cold and lots of it
Posts: 42,202
Default

So do it. Create a sub for each, put them in a module or something, inside the if statement, run the sub.
__________________
GAME ON!!!!!!! Let your ness show. Join the D&D fun. Started but applications still accepted


Officially assigned the role of Dictator, 9/30/09. Bow to my whims.
Reply With Quote
  #3  
Old 06-26-2008, 01:27 PM
actuarialvoodoo's Avatar
actuarialvoodoo actuarialvoodoo is offline
Member
CAS AAA
 
Join Date: Jun 2007
Studying for fun
College: Texas A&M University
Favorite beer: warsteiner dunkel; bourbon
Posts: 618
Default

How is a goto statement easier to read than a series of if statements, nested if statements or a select case statement? I'm not saying I can't be convinced to see it your way, but my guess is your real problem is you need to organize and pretty up your current code rather than using the goto statement. Ever heard of speghetti code?
__________________
I don't believe in a lot of things, but I do believe in duct tape.
Reply With Quote
  #4  
Old 06-26-2008, 01:32 PM
Abused Student's Avatar
Abused Student Abused Student is offline
Member
SOA
 
Join Date: Feb 2007
Location: The Eighth Circle of Hell
Favorite beer: Cold and lots of it
Posts: 42,202
Default

Quote:
Originally Posted by actuarialvoodoo View Post
How is a goto statement easier to read than a series of if statements, nested if statements or a select case statement? I'm not saying I can't be convinced to see it your way, but my guess is your real problem is you need to organize and pretty up your current code rather than using the goto statement. Ever heard of speghetti code?
With a whole bunch of if statements, code can become quickly very messy and hard to follow. If you break it up into chunks and easy to read pieces, it makes it a lot easier to follow, especially if you have the opportunity to reuse a lot of code.
__________________
GAME ON!!!!!!! Let your ness show. Join the D&D fun. Started but applications still accepted


Officially assigned the role of Dictator, 9/30/09. Bow to my whims.
Reply With Quote
  #5  
Old 06-26-2008, 01:38 PM
Phil's Avatar
Phil Phil is offline
Site Supporter
Site Supporter
 
Join Date: Sep 2001
Location: not on board the Enterprise
Posts: 5,667
Default

I think you might need... could it really be... is there actually a use here for... Gosub?
Reply With Quote
  #6  
Old 06-26-2008, 01:41 PM
Guest
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by Abused Student View Post
So do it. Create a sub for each, put them in a module or something, inside the if statement, run the sub.
I keep running into undefined variables when I do that. Or the variables don't hold their values.

Right now everything is in one subroutine all in the same module.
Reply With Quote
  #7  
Old 06-26-2008, 01:42 PM
GadgetGeek's Avatar
GadgetGeek GadgetGeek is offline
Note Contributor
SOA AAA
 
Join Date: Sep 2001
Location: I'm here, where are you?
Studying for a way to escape
College: Illinois College ('87)
Favorite beer: Stag
Posts: 11,154
Default

Here is how I handle a similar situation.
Code:
if me.ckboxA = vbchecked then
  process_checkboxB
endif
if me.ckboxB = vbChecked then
  process_checkboxB
endif

sub process_checkboxA()
  do stuff
end sub

sub process_checkboxB()
  do different stuff
end sub
.
__________________
Some people are like slinkies. Mostly useless, but provide amusement when pushed down the stairs.
Reply With Quote
  #8  
Old 06-26-2008, 01:43 PM
GadgetGeek's Avatar
GadgetGeek GadgetGeek is offline
Note Contributor
SOA AAA
 
Join Date: Sep 2001
Location: I'm here, where are you?
Studying for a way to escape
College: Illinois College ('87)
Favorite beer: Stag
Posts: 11,154
Default

Quote:
Originally Posted by notreallyme View Post
I keep running into undefined variables when I do that. Or the variables don't hold their values.

Right now everything is in one subroutine all in the same module.
ugly solution is to declare your variables globally.
elegant (but sometimes cumbersome) solution is to pass your variables to the subroutine(s)
__________________
Some people are like slinkies. Mostly useless, but provide amusement when pushed down the stairs.
Reply With Quote
  #9  
Old 06-26-2008, 01:44 PM
Guest
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by Phil View Post
I think you might need... could it really be... is there actually a use here for... Gosub?
Not sure if you are kidding or not.

I am not a programmer by any means. Everything works now I'm trying to figure out what is best.

I was reading up on Goto statements and there was a recommendation to not use many of them.

-----------------------------

Part of the reason I like have all the stuff up top is that I can quickly tell what all I have written (The A might be something like TIVreport) and organize everything.

And yes the code does look similar in all cases:
- Write a SQL statement
- Run the query
- Put it to a recordset
- Export it to Excel
Reply With Quote
  #10  
Old 06-26-2008, 01:46 PM
Guest
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by GadgetGeek View Post
ugly solution is to declare your variables globally.
elegant (but sometimes cumbersome) solution is to pass your variables to the subroutine(s)
I currently have everything declared globally.

Do they hold their values when going to a new subroutine? I thought I had problems with this but can't remember.

PS thanks for the all the help so far everyone!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT -4. The time now is 02:18 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
*PLEASE NOTE: Posts are not checked for accuracy, and do not
represent the views of the Actuarial Outpost or its sponsors.
Page generated in 0.42901 seconds with 9 queries