Wrapping the call to MessageBox()

The MessageBox() function lets you display a standard Windows dialog to get a Yes/No answer from the user. The dialog is reliable and familiar but the call to this function can be a bit verbose:

If MessageBox ("Are you sure",
  MB_ICONQUESTION + MB_YESNO,
  "Delete record") = IDYES ...

This call then gives you a return value of 6 or 7 so you need to write further code to determine whether the user said 'Yes' or 'No'. The task is not difficult but the task does become tedious after a while.

You can make life a lot easier by writing a function to wrap the MessageBox call. We use a function named "Confirm" which takes the text and title as parameters and converts the numeric return from MessageBox into the True or False values .T. or .F..

The body of the function is very simple:

Function Confirm
Lparameter tcText, tcTitle

lnResponse = Messagebox(tcText, ;
  MB_ICONQUESTION + MB_YESNO, ;
  tcTitle)

Return lnResponse = IDYES

EndFunc

The code to use this function is much easier to write than the call to the MessageBox function because you can embed it into an If statement:

If Confirm("Are you sure", "Delete record") ...

A more sophisticated function

Our basic Confirm() function has grown more sophisticated over the years. The second parameter is now optional and the function uses PCount() to count the number of parameters received. If there is only one parameter then it defaults to using the screen caption as the title for the MessageBox dialog. This makes calling the function even simpler.

An additional bonus in our work is that we have no longer have to remember the different calling conventions for the FoxPro and Access messageboxes.

Both languages use the standard Windows dialog and both require a parameter of 36 and will return 6 or 7 as the result. The difference is that Access calls it with the MsgBox() function rather than MessageBox() and has constants named vbYes and vbQuestion corresponding to IDYES and MB_ICONQUESTION in Visual FoxPro. We've now got a "Confirm()" function written in both languages so we don't have to worry about these annoying differences any more.

MS Access technical tips

Visual FoxPro technical tips

General Tips

 

More tips from Alvechurch Data

Alvechurch Data - Microsoft FoxPro and Access Database Development and Training

Alvechurch Data are based close to Birmingham and provide Microsoft Access and Visual FoxPro training, development and support.

Read More

Autocomplete in Visual Fox Pro

Autocomplete in Visual Fox Pro

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.

Read More

FoxPro Functions

FoxPro has always had functions like FREAD and FWRITE to read and write files at a low level. They can handle files which defeat the STRTOFILE and FILETOSTR functions.

Read More

Development Services

Alvechurch Data specialise in Microsoft Access and Visual FoxPro databases and have fifteen years experience developing databases for small business use.

Read More

Access and Visual FoxPro Training

Microsoft Access and Visual FoxPro training courses and workshops offered by Alvechurch Data in Birmingham.

Read More