Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Apr 2008
    Posts
    188
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Francois

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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

  4. #4
    2 Star Lounger
    Join Date
    Apr 2008
    Posts
    188
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    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"

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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

  6. #6
    2 Star Lounger
    Join Date
    Apr 2008
    Posts
    188
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •