Sign in to follow this  
Clotty

VB Function Question

Recommended Posts

This is just a question I have for a game I'm making. Basically, I just wondered how you tell the program what to do if user clicks Cancel, Abort, Retry or Okay is response to a sudden MsgBox. Thanks in advance.

Share this post


Link to post
Share on other sites
Dim intResponse As Integer
intResponse = MsgBox("Whatever you want to ask", "Title", vbYesNoCancel)
If (intResponse = vbYes) Then
' Do whatever you want here for "Yes".
ElseIf (intResponse = vbNo) Then
' Do whatever you want here for "No".
ElseIf (intResponse = vbCancel) Then
' Do whatever you want here for "Cancel".
End If


You can also use other constants to test the return value like vbAbort, vbRetry, etc... actually, if you declare "intResponse" as type vbResponse (or something like that), it will allow you to use the CodeAssist or whatever it's called, so you can see all the possible values.

There are also other options besides vbYesNoCancel (e.g. vbYesNo, vbOkOnly, vbOkCancel, etc) that you can provide to the function to determine which buttons to display.

Share this post


Link to post
Share on other sites
Thanks a lot Mattman!

Oh, and one other thing I was having problems with was pressing command to take me to another form.


Hypothetically say I wanted vbOK to take them to the next screen (form3 lets say) or VBCancel to go back to the original form where they can say ... fill out an application form for something again (form 1 lets say).

How would I do that?

I've tried experimenting with GOTO form3 and GOTO (form3) and everything else I've come accross in program that I thought could work, but it hasn't worked with VB. The book I'm learning from is quite bad and I expect the answer to this question would probably be somewhere in chapter 19 when I'm in Chapter 8 now, and left in the dark.

Again, help is appreaciated.

Share this post


Link to post
Share on other sites
Try this...


If intResponse=vbOk Tthen
form3.show
ElseIf intResponse=vbCancel Then
form1.show
End If


The GoTo statement simply goes to a different line, not a form. Like in this while loop...


While True

MsgBox "This is normally an infinite loop"
GoTo theLine

Wend

theLine: MsgBox "Nice line!"


If you executed that, the goto statement would exit the loop and go directly to the aptly named line "theLine", which displays a message box. It can also be used for a block of code like:


While True

MsgBox "This is normally an infinite loop"
GoTo theBlockOfCode

Wend

theBlockOfCode:

MsgBox "Inside the block..."
MsgBox "Still inside the block..."


If you executed that you would see those two messsage boxes. Also keep in mind that those are just examples, it is not a very good idea to use alot of goto statements in loops, it makes people go insane...

Hope that helps :)

Share this post


Link to post
Share on other sites
Thanks guys.

I've tried to do what I just learned, but there's a hitch somewhere. Here is what I put down.

Clarify = MsgBox(strman_fname & " " & strman_lname & "are you sure you want proceed managing with " & List1 & "?", =vbYesNo + vbQuestion, "Confirm Decision")
If (Clarify = vbYes) Then
form2.Show
ElseIf (Clarify = vbNo) Then
manprofile.Show
End If

Basically strman_fname and strman_lname are just the manager's firstname and last name that get entered on the form. List1 is the club they select to manage. manprofile is the name of the form and form2 is a form a created simply for the testing to see if this works - it has no use ... yet.

The according to the debugging my problem is somewhere in the =vbYesNO + vbQuestion section of the MsgBox() function. I have no idea what exactly it is though.

Any of you know?

Help appreaciated.

Share this post


Link to post
Share on other sites
Quote:
Original post by Clotty
Clarify = MsgBox(strman_fname & " " & strman_lname & "are you sure you want proceed managing with " & List1 & "?", =vbYesNo + vbQuestion, "Confirm Decision")

Take out the '=' before vbYesNo so it reads:

Clarify = MsgBox(strman_fname & " " & strman_lname & "are you sure you want proceed managing with " & List1 & "?", vbYesNo & vbQuestion, "Confirm Decision")

Deg

Share this post


Link to post
Share on other sites
Quote:


If intResponse=vbOk Tthen
form3.show
ElseIf intResponse=vbCancel Then
form1.show
End If



You can also write "form3.show 1". "1" is a parameter.
Writing in this way can keep form3 activated until a code ends it.

Share this post


Link to post
Share on other sites
So, I have a new problem for you then :)

Private Sub cmdproceed_Click()

Clarify = MsgBox(strman_fname & " " & strman_lname & "are you sure you want proceed managing with " & List1 & "?", vbYesNo + vbQuestion, "Confirm Decision")

If (Clarify = vbYes) Then

Form1.Show AND manprofile.Hide

ElseIf (Clarify = vbNo) Then

manprofile.Show

End If

End Sub

There is an error here and I know it's in this line

"Form1.Show AND manprofile.Hide"

I'm guessing it's because I've used the AND in an illegal context for VB6. How else can I do the same thing?

Thanks in advance. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Argoran
You can also write "form3.show 1". "1" is a parameter.
Writing in this way can keep form3 activated until a code ends it.

Even better, use the constant names that represent the values desired.

Call Form3.Show(vbModal)
Call Form4.Show(vbModeless)

Share this post


Link to post
Share on other sites
Quote:
Original post by FlyingSpork
Try this...

If intResponse=vbOk Tthen
form3.show
ElseIf intResponse=vbCancel Then
form1.show
End If

There's actually a known bug in VB regarding the Show() and Hide() methods of a Form. Instead, it is suggested to use the Visible boolean property, as shown below.

If intResponse = vbOk Then
Form1.Visible = True
ElseIf intResponse = vbCancel Then
Form3.Visible = True
End IF

Share this post


Link to post
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

Sign in to follow this