Friday, December 25, 2009

Messagebox in vb 6 - msgbox

msgbox in vb 6

How to diplay a message box in Visual Basic?

Message Box is used to display a message to the user. It can prompt the user to do something. It can received inputs like 'Yes' or 'No', 'Ok' or 'Cancel'. It can give warning messages, Exclaimatory messages,

The msgbox function has 5 arguments as follows:

Msgbox(Prompt,[Buttons AS VbMsgBoxStyle=vbOkOnl],[Title],[HelpFile],[Context]) AS VbMsgBoxResult

The first argument (parameter) is the prompt, which is clearly the the prompt text or caption.

The second argument, button constants are VbAbortRetryIgnore, vbApplicationModal, vbCritical, vbDefaultButton1, vbDefaultButton2, vbDefaultButton3, vbDefaultButton4, vbExclamation, vbInformation, vbMsgBoxHelpButton, vbMsgBoxRight, vbMsgBoxRtlReading, vbMsgBoxSetForeground, vbOkCancel, vbQuestion, vbRetryCancel, vbSystemModal, vbYesNo, vbYesNoCancel

The third argument 'Title' refers to the Title bar caption of the Messagebox window. You can type any string here.

Help file refers the location of HTML or windows help file. This argument is applicable if you give vbMsgBoxHelpButton for the second argument (button constant). This will display a 'help' button in the messagebox and if the user click this button the help file will open.

The messagebox result constants are vbAbort, vbCancel, vbIgnore, vbNo, vbOk, vb Retry, vbYes

For example: If the user click yes button on the messagebox the result is vbYes.

Here is an example of typical Msgbox (Messagebox) function with sample source code:

Private Sub cmdDelete_Click()
If MsgBox("Are you sure to delete this record?", vbYesNo, "Confirm Delete") = vbYes Then
'Write code for deletion
End If
End Sub

When the user clicks the Delete button a messagebox is shown as in the picture. When the user click the yes button the code inside the If - then - endif Control structure will be executed. (Write your code for deletion of the record)

For the second argument you can combine visual elements like warning icons to provide the user visual warning for deletion. Here the example code in vb 6.

Private Sub cmdDelete_Click()
If MsgBox("Are you sure to delete this record?", vbExclamation + vbYesNo, "Confirm Delete") = vbYes Then
'Write code for deletion
End If
End Sub