How to code a MsgBox correctly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yousaf Shah
    New Member
    • Feb 2011
    • 25

    How to code a MsgBox correctly

    I am a surgeon and has made a database for my patient record keeping. I have got a form 'Basic History Examination Form' that should not be opened directly but through another form "Patient Biodata Form". For that, I put following code in on exit event of 1st control of form 'HistoryID'

    Code:
    Private Sub HistoryID_Exit(Cancel As Integer)
      If IsNull(Me.PatientIDPicker) Then
        MsgBox "Sorry...!" & vbCrLf & "You can not enter data directly into 'Basic History Examination Form'" & vbCrLf & "Please " & _
    "close form and attempt data entry through 'Patient Biodata Form'", vbInformation, "Wrong Data Entry Attempt"
        Form.DataEntry = False
    Exit Sub
    When I open the form directly and attempt to enter data, it responds with pre-defined msg correctly but when I close msgbox and try to close the form, msgbox appears again... when I close it for second time, it again re-appears and then on closing msgbox, it closes alongwith form....
    In this code "PatientIDPicke r is an unbound field on form that gets automatically with 'patientID' value when form is opened in a normal way. e.g through Patient Biodata form.
    I am unable to figure out that where there is a problem..

    Is there anybody to help me out
    Last edited by TheSmileyCoder; Jun 26 '13, 09:51 PM. Reason: Fixed Code tags.
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    I can't really see anything in your code that matches the scenario you describe. So maybe there is more code that we are not seeing here?


    Why not try this instead?
    In the OPEN event of your History form add the following:
    Code:
    Private Sub Form_Open(Cancel as Integer)
      IF Screen.ActiveForm.Name<>"Patient Biodata Form" Then
        Cancel=True
        MsgBox "This form should be opened from the 'Patient Biodata Form'
      End If
    End Sub

    Comment

    Working...