UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Do Loop Until.....is Their A Better Way?, Any Version    
 
   
jimbofoxman
post Jun 30 2020, 04:33 PM
Post#1



Posts: 428
Joined: 4-April 08



The code below is just seeing if I could get it to work. Right now it does work, but I am wondering if their is a better way. Just seems like I did it the long way. Before the salesmen can get their job scheduled the jobs need a few criteria met. This example is checking for a Labor figure. If their is nothing currently in Labor it prompts for some input.

Obviously their are some checks. What to do if they press Cancel? What if they enter text? What if they enter 0 again? I may or may not leave the 0-100 part. Sometimes they like to enter 1 to get around things. Probably should change the initial, so it's not between 0 and 100.

We allow negative labor as sometimes we end up doing warranty work. So we need the labor figure to help scheduling but it tells us we are eating the labor.

Here is my code. It works, but wondering if their is a better more efficient way. Also, is their a better way to do the Loop? Right now I have an Until blnLabor = True

CODE
    If Me.ContractVerNo < 900 And Me.Labor70 = 0 Then
        Do
            strLabor = InputBox("What is the crew labor for this job?", "Crew Labor...")
            If StrPtr(strLabor) = 0 Then
                MsgBox "Cancelled"
                Exit Sub
            ElseIf Not IsNumeric(strLabor) Then
                MsgBox "Not numeric"
            ElseIf CInt(strLabor) = 0 Then
                MsgBox "Cannot be 0"
            Else
                If CInt(strLabor) < 0 Or CInt(strLabor) >= 100 Then
                    MsgBox "You actually entered something useful"
                    'run some other code too
                    blnLabor = True
                Else
                    MsgBox "Come on we need a real labor"
                End If
            End If
        Loop Until blnLabor = True
    End If


Any thoughts?
Go to the top of the page
 
theDBguy
post Jun 30 2020, 04:41 PM
Post#2


UA Moderator
Posts: 78,463
Joined: 19-June 07
From: SunnySandyEggo


Hi. Sometimes, "better" is just a matter of perspective. If the code is working as you intended, then may be it's good enough or better than nothing (or doesn't work). There's always more than one way to do the same thing in Access (and coding, in general), but I wouldn't worry too much about it until you find a reason why you should. Just my 2 cents...

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
projecttoday
post Jun 30 2020, 04:59 PM
Post#3


UtterAccess VIP
Posts: 12,381
Joined: 10-February 04
From: South Charleston, WV


Well, I commend you for leaving the door open to a better way. Just because it works doesn't mean it necessarily couldn't be better. However, nothing jumps out at me with this approach overall.

Can't you just exit upon success and do away with blnLabor? In that case you could change the loop to be infinite or just to give a certain number of tries.

--------------------
Robert Crouser
Go to the top of the page
 
jimbofoxman
post Jun 30 2020, 06:02 PM
Post#4



Posts: 428
Joined: 4-April 08



QUOTE
Can't you just exit upon success and do away with blnLabor? In that case you could change the loop to be infinite or just to give a certain number of tries.


Their are going to be several other checks after that. My initial though was to fill all the data collected at the end, but now that I think about it that's probably not the best. So what if they already filled it in and then hit cancel on something else. They won't have to do it next time. So, yeah I think I could just Exit the Do Loop.
Go to the top of the page
 
projecttoday
post Jun 30 2020, 09:25 PM
Post#5


UtterAccess VIP
Posts: 12,381
Joined: 10-February 04
From: South Charleston, WV


Try using the Continue statement to pass control back up to the top of the loop and try again.

--------------------
Robert Crouser
Go to the top of the page
 
missinglinq
post Jul 1 2020, 05:27 AM
Post#6



Posts: 4,702
Joined: 11-November 02



I tend to agree with theDBguy. In the days when a 20 MB hard drive was state-of-the-art, we used to have contests seeing who could accomplish a given task with the fewest keystrokes...but those days are way in the past!

More important, to me, today, is making code that is well documented, so that those working on your code in the future (including yourself!) can easily follow what you did.

As they say (don't you always wonder who they are?) if it ain't broke don't fix it!

Linq ;0)>

--------------------
BTW...The Monkey is laughing at me...not at any other poster!

The problem with making anything foolproof...is that fools are so darn ingenious!

All posts/responses based on Access 2003/2007
Go to the top of the page
 
Jeff B.
post Jul 1 2020, 07:18 AM
Post#7


UtterAccess VIP
Posts: 10,489
Joined: 30-April 10
From: Pacific NorthWet


<linq>

For those with too much time on their hands, that saying is: "If it ain't broke, break it!"

(actually, not a bad idea … if I can figure out a way to break it, I can figure out a way to prevent/fix …)

--------------------
Regards

Jeff Boyce
Microsoft Access MVP (2002-2015)

Mention of hardware or software is, in no way, an endorsement thereof. The FTC of the USA made this disclaimer necessary/possible.
Go to the top of the page
 
penfold098
post Jul 1 2020, 08:22 AM
Post#8



Posts: 155
Joined: 5-March 14



I agree with the statement: "If it ain't broke, don't fix it," especially if you beta tested it, and it works for the users. (I have too many examples of a seemingly small code change causing errors downstream). doh.gif

I applaud your questions nonetheless. Seeking to write better and more efficient code is a career-long journey -- which is why we all visit UtterAccess often! uarulez2.gif

Personally I only change old code if it is an abomination. Otherwise I put a comment in the old code that notes the better algorithm, so I don't use it in another project.

HTH
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    7th July 2020 - 11:08 AM