Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using MsgBox to validate data

Status
Not open for further replies.

Garyeag

Technical User
Mar 23, 2001
32
GB
I am using MsgBox to try and validate data on a form.
If the textbox is empty MsgBox displays an error message and beeps. This bit works OK, whether I invoke it on the lost focus or exit event.
However, I can not get the focus to return the the text box which needs validating after the msgbox.
What am I doing wrong?
I've tried putting a setfocus both before and after the msgbox, but this fails. I've tried using VBA and just in macro form (which I then converted to VBA).
It would help if I could use the debugger to trace what is happening, as it's underlying causes I want to cure not just symtoms like this.
Would it have anything to do with the fact that the tab order means that the next box after the textbox I'm validating is the first box in a subform? Is the focus getting stuck in the subform?
Insights appreciated.
 
Here's an example of code that I use to display the MsgBox, clear out the text box, and set the focus to it:
Code:
If IsNull(strX = DLookup("[medhistid]", "dbo_prhdemog", "[medhistid] like '*" _
    & Forms!frmMain!txtMedhist & "*'")) Then
  MsgBox (strMsg)
  Forms!frmMain!txtMedhist = ""
  Forms!frmMain!txtMedhist.SetFocus
  Forms!frmMain.Refresh
End If

HTH
 
How are you using a msgbox to validate data? From what I understand, you evaluate whether there is an appropriate value in the textbox before determining whether or not a msgbox is used. I assume that you have some kind of structure like this

If Isnull(me.MyTextbox) or me.myTextbox = "" then
msgbox "Please put a value into this field", vbexclamation
me.mytextbox.setfocus
end if


Make sure that you have not disabled the textbox before you set the focus to it.

Hope that helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top