can anyone help me w/ this one?


dim msg as boolean
msg = msgbox ("Save this one?", vbyesno)
if msg = true then
'code goes here
'code goes here
end if

when i run this one my code jumps to else though i choose yes to save the file.

Recommended Answers

All 7 Replies

try replacing true with vbyes.

i already tried it and it gave me the same result? i dnt know whats wrong

what code is there between the if blocks (where it says 'code goes here), what's actually there?

Ok, the problem is that you declared the variable (msg) as a boolean value.... while the msgbox function returns more values than just true and false. This code works wonderfully when you change the type declaration of your variable:

Dim msg As VbMsgBoxResult

msg = MsgBox("Save this one?", vbYesNo)
If msg = vbYes Then
    MsgBox "true"
    MsgBox "false"
End If

yep got it i just deleted the declaration andleave it as a variant variable. but i think its a good practice to declare a variable right?
thanks for your help.

Declaring variables isn't required in VB, but it's a great practice. Part of the reason is, when you get into other languages, that are more powerful, you'll be forced to declare any variable before you can use it.... also, declaring variables provides the program with a reserve of memory for that variable. This makes your program smaller, and makes it run faster. On a simple test application, like the one we have here, it won't make a noticable difference, but in larger applications, you'll be asking yourself what the hell happened. Provided is a link to a page that will help you to optimize your VB programming, so that you can increase the speed significantly of yours apps.

Also, When you don't declare a variable, it's default type is a variant. If you read the page linked, you'll see why variants are a no, no. Basically, they are the largest, most bulky variable type in existance, and should really be treated like the Leper Variable. In fact, every time you use a variant type variable, I want you to think about how you are giving your code leprosy.

well said. thank u thank u. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.