• Forms – No data message

    Author
    Topic
    #464559

    Is there a way you can generate a no data message on a form if you pull up a student’s name and there is no data to show. That option exists in reports but I’m not sure of the best way to approach it in a form.

    I am guessing that you take one field on the form and if the field is empty then you have a message appear.

    What approach would you suggest?

    Thanks.

    Paul

    Viewing 3 reply threads
    Author
    Replies
    • #1189519

      In the On Open event of the form, put the following code:

      Code:
      If Me.Recordset.RecordCount = 0 Then
          MsgBox "There is no data"
          Cancel = True
      End If
    • #1189520

      You could use code like this in the On Open event of the form:

      Code:
      Private Sub Form_Open(Cancel As Integer)
        If Me.RecordsetClone.RecordCount = 0 Then
          MsgBox "No data", vbInformation
          Cancel = True
        End If
      End Sub
    • #1189534

      You could use code like this in the On Open event of the form:

      Code:
      Private Sub Form_Open(Cancel As Integer)
        If Me.RecordsetClone.RecordCount = 0 Then
          MsgBox "No data", vbInformation
          Cancel = True
        End If
      End Sub

      Thanks to both of you for your replies. Your solutions work, but I guess I need to go one step further.

      I have been using another form to select the student and then have the main form open. I now get the appropriate “No Data” message, but I also get an error message about canceling the Open Event.

      I assume I need to put something in my select student form to deal with this issue. Maybe move your code to my select student form?

      Here is the code I now have in there.

      Dim stDocName As String
      Dim stLinkCriteria As String

      stDocName = “FrmVisionScreeningMaster”

      stLinkCriteria = “[LNFN]=” & “‘” & Me![liststudents] & “‘”
      DoCmd.Close acForm, “FrmVisionScreeningMaster”
      DoCmd.OpenForm stDocName, , , stLinkCriteria
      DoCmd.Close acForm, “frmViewSelectedStudentVisionScreening”
      DoCmd.Close acForm, “Switchboard”

      • #1189537

        Create an error handler that traps error 2501 (=action canceled by user):

        Code:
        Sub cmdSomething_Click()
            Dim stDocName As String
            Dim stLinkCriteria As String
         
            On Error GoTo ErrHandler
         
            stDocName = "FrmVisionScreeningMaster"
            
            stLinkCriteria = "[LNFN]=" & "'" & Me![liststudents] & "'"
            DoCmd.Close acForm, stDocName
            DoCmd.OpenForm stDocName, , , stLinkCriteria
            DoCmd.Close acForm, "frmViewSelectedStudentVisionScreening"
            DoCmd.Close acForm, "Switchboard"
            Exit Sub
         
        ErrHandler:
            If Err = 2501 Then ' Canceled
                ' No need to do anything
            Else
                MsgBox Err.Description, vbExclamation
            End If
        End Sub
    • #1189561

      Create an error handler that traps error 2501 (=action canceled by user):

      Code:
      Sub cmdSomething_Click()
          Dim stDocName As String
          Dim stLinkCriteria As String
       
          On Error GoTo ErrHandler
       
          stDocName = "FrmVisionScreeningMaster"
          
          stLinkCriteria = "[LNFN]=" & "'" & Me![liststudents] & "'"
          DoCmd.Close acForm, stDocName
          DoCmd.OpenForm stDocName, , , stLinkCriteria
          DoCmd.Close acForm, "frmViewSelectedStudentVisionScreening"
          DoCmd.Close acForm, "Switchboard"
          Exit Sub
       
      ErrHandler:
          If Err = 2501 Then ' Canceled
              ' No need to do anything
          Else
              MsgBox Err.Description, vbExclamation
          End If
      End Sub

      Thanks – That did it.

      Paul

    Viewing 3 reply threads
    Reply To: Forms – No data message

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: