Jump to content

VB help needed


Thugwitglock

Recommended Posts

Hey there, i am currently working on a Visual Basic 6.0 project for school, and i have run into a little problem here that i need assistance on.

 

 

 

 

 

 

 

I won't go into details on the assignment, because its irrelivent, i'm simply having a problem with my error handling on it. I need to provide a message box popup that will display if none of the 3 options buttons in a frame are selected.

 

 

 

 

 

 

 

I was thinking about how i was going to do this for quite some time, and the best idea i could come up with was to do this...

 

 

 

 

 

 

 

Dim int1, int2, int3, intTotal As Integer







If optOverNight.Value = False Then



 int1 = 1



ElseIf optDays.Value = False Then



 int2 = 2



ElseIf optWeeks.Value = False Then



 int3 = 3



End If







intTotal = int1 + int2 + int3







If intTotal = 6 Then



 MsgBox "You must select a delivery option, vbOkOnly, "Error"



End If



 

 

 

 

 

 

 

But this code obviously did not work, so i was wondering if anybody had another idea i could try. I feel like i am making this harder than it actually is, so someone hitting me with some common sense would be much appreciated.

Link to comment
Share on other sites

Hey there, i am currently working on a Visual Basic 6.0 project for school, and i have run into a little problem here that i need assistance on.

 

 

 

 

 

 

 

I won't go into details on the assignment, because its irrelivent, i'm simply having a problem with my error handling on it. I need to provide a message box popup that will display if none of the 3 options buttons in a frame are selected.

 

 

 

 

 

 

 

I was thinking about how i was going to do this for quite some time, and the best idea i could come up with was to do this...

 

 

 

 

 

 

 

Dim int1, int2, int3, intTotal As Integer







If optOverNight.Value = False Then



 int1 = 1



ElseIf optDays.Value = False Then



 int2 = 2



ElseIf optWeeks.Value = False Then



 int3 = 3



End If







intTotal = int1 + int2 + int3







If intTotal = 6 Then



 MsgBox "You must select a delivery option, vbOkOnly, "Error"



End If



 

 

 

 

 

 

 

But this code obviously did not work, so i was wondering if anybody had another idea i could try. I feel like i am making this harder than it actually is, so someone hitting me with some common sense would be much appreciated.

 

 

 

 

 

 

 




Dim selectSomething



selectSomething = (optOverNight.Value Or optDays.Value Or optWeeks.Value)







If Not selectSomething Then



 MsgBox "Please select a delivery option first", vbOkOnly, "Error"



End If



 

 

 

 

 

 

 

Should work. Using Or makes sure that if any of them is true, the selectSomething value is true too. I may be off a bit with braces and such, it's been some time since I wrote VB :).

Link to comment
Share on other sites

I'm curious though, how where you able to declare the "selectSomething" Variable without giving it a "type". What i mean is using

Dim selectSomething As Integer

for example?

 

 

 

 

 

 

 

We have not used this sort of code in our class yet, so i was just wondering, incase the teacher wants me to go into detail on what code i used and how it works(she tends to do that).

Link to comment
Share on other sites

Ah right. Declaring a variable without a type gives you a variable of type Variant, which can basically become anything. I forgot the typedeclaration, to be honest, because I'm more used to write stuff in JavaScript or php now, both of which either don't have or hardly ever use typed declarations.

 

 

 

 

 

 

 

In this case, you could also use:

 

 

 

Dim selectedSomething As Boolean

 

 

 

A boolean datatype can only be true or false, in case you didn't use that stuff yet.

 

 

 

 

 

 

 

I should also note that your own example didn't work because you used:

 

 

 

If

 

 

 

ElseIf

 

 

 

ElseIf

 

 

 

End If

 

 

 

 

 

 

 

It would have worked if you used

 

 

 

If

 

 

 

End If

 

 

 

If

 

 

 

End If

 

 

 

If

 

 

 

End If

 

 

 

 

 

 

 

This is because elseif blocks only get executed if the if-condition fails. So in your own code, the code after the first elseif only got executed if the first option was true, and the code after the second one only if both the first option and the second were true. Which is impossible.

 

 

 

Moral of the story: don't use elseif just because you have another if just before it which is very similar ;).

 

 

 

 

 

 

 

Using Booleans is neater in this case, as you may have noticed.

 

 

 

 

 

 

 

Something else you should note is that if you give your variables decent names, code makes more sense and becomes easier to read if you do:

 

 

 

If Not somethingWasSelected Then ...

 

 

 

Instead of:

 

 

 

If somethingWasSelected = False Then ...

 

 

 

 

 

 

 

Both give the same result when handling the boolean datatype. Using the Not operator (it's an operator) on an integer may give funny results. I wouldn't know exactly what would happen (maybe the code wouldn't even run, can't recall that).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.