Results 1 to 7 of 7
  1. #1
    ortizimo is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jun 2017
    Location
    FL
    Posts
    88

    If no records then stop, if records then open but not sure why!

    the code below to me--its not supposed to be working. basically what i am looking for is:
    1. if no records flagged then do not open form and show msgbox stating no records found.
    2. if any records are flagged then msgbox stating how many and then open the form.

    i know this is wrong but somehow it works and still i dont like that i dont understand it...
    ------------------------------------------------------------------------
    Private Sub cmdFlagged_Click()

    Dim fRec As Integer
    'if no records flagged then do not open frmFlagged and show msgbox
    If fRec = DCount("[ID]", "qryFlagged") Then
    DoCmd.Close acNormal, "frmFlagged"
    MsgBox "You Have NO Records Flagged.", vbInformation
    Else
    'if flagged records found then show msgbox and open frmFlagged
    If fRec = DCount("[ID]", "qryFlagged") = 0 Then
    MsgBox "You Have " & fRec & " Records Flagged for Review!", vbInformation
    DoCmd.OpenForm "frmFlagged", acNormal
    End If
    End If
    End Sub
    ------------------------------------------------------------------------
    The red lines to me should be:
    1. = 0 then
    2. > 0 then



    thnx!

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    frec is not set

    you define fRec as an integer but if you don't populate it with a value it's going to be null (I think) and will automatically fail)

    if you are trying to determine if the recordset is null or not

    Code:
    if dcount("[ID]", "qryFlagged") = 0
       'fail part
    else
       'succeeed part
    endif

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    ifRec is zero at start so it ends up as
    If 0 = 0 when there are no records

    in 2nd line translation is
    if 0 equals (true or false) where parens are the result of dcount = 0. In that case, if record count (Dcount = 0) is false, it becomes "if 0 equals false" - which is true

    Not very well written, thus not very intuitive I guess.
    Last edited by Micron; 06-03-2019 at 01:12 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    you don't populate it with a value it's going to be null
    integer and long variables default to 0

    One can easily determine the values of variables and results of expressions by stepping through code and examining them.
    Ortizimo, I suggest you adopt that practice for trouble shooting.

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    2 potential approaches
    Code:
    Private Sub cmdFlagged_Click()
    Dim fRec As Integer
    
    fRec = DCount("[ID]", "qryFlagged")
    'if no records flagged then do not open frmFlagged and show msgbox
    If fRec = 0 Then
     MsgBox "You Have NO Records Flagged.", vbInformation
    Else
     MsgBox "You Have " & fRec & " Records Flagged for Review!", vbInformation
    DoCmd.OpenForm "frmFlagged", acNormal
    End If
    
    End Sub
    or how to exit on one condition and default to another
    Code:
    Private Sub cmdFlagged_Click()
    Dim fRec As Integer
    
    fRec = DCount("[ID]", "qryFlagged")
    'if no records flagged then do not open frmFlagged and show msgbox
    If  fRec = 0 Then
      MsgBox "You Have NO Records Flagged.", vbInformation
      Exit Sub
    End If
    
    MsgBox "You Have " & fRec & " Records Flagged for Review!", vbInformation
    DoCmd.OpenForm "frmFlagged", acNormal
    
    End Sub
    They have the same line count I'd say. Either are a bit less cryptic and don't rely on what seem to be happy accidents in your code.

  6. #6
    ortizimo is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jun 2017
    Location
    FL
    Posts
    88
    Quote Originally Posted by Micron View Post
    2 potential approaches
    Code:
    Private Sub cmdFlagged_Click()
    Dim fRec As Integer
    
    fRec = DCount("[ID]", "qryFlagged")
    'if no records flagged then do not open frmFlagged and show msgbox
    If fRec = 0 Then
     MsgBox "You Have NO Records Flagged.", vbInformation
    Else
     MsgBox "You Have " & fRec & " Records Flagged for Review!", vbInformation
    DoCmd.OpenForm "frmFlagged", acNormal
    End If
    
    End Sub
    or how to exit on one condition and default to another
    Code:
    Private Sub cmdFlagged_Click()
    Dim fRec As Integer
    
    fRec = DCount("[ID]", "qryFlagged")
    'if no records flagged then do not open frmFlagged and show msgbox
    If  fRec = 0 Then
      MsgBox "You Have NO Records Flagged.", vbInformation
      Exit Sub
    End If
    
    MsgBox "You Have " & fRec & " Records Flagged for Review!", vbInformation
    DoCmd.OpenForm "frmFlagged", acNormal
    
    End Sub
    They have the same line count I'd say. Either are a bit less cryptic and don't rely on what seem to be happy accidents in your code.
    thanks...that worked.
    Also, i know is poorly written as i'm learning and trying to make sense of it.
    Thanks to all.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    trying to make sense of it.
    Thanks to all.
    did my explanation make any sense?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Shall Stop Orphan Records in SubForm!
    By cap.zadi in forum Forms
    Replies: 4
    Last Post: 12-04-2018, 07:21 AM
  2. Replies: 7
    Last Post: 12-03-2015, 08:37 PM
  3. How to stop form automatically adding records
    By linuxson in forum Access
    Replies: 33
    Last Post: 07-01-2014, 06:33 AM
  4. Stop the introduction of records
    By azhar2006 in forum Forms
    Replies: 27
    Last Post: 02-14-2014, 03:42 PM
  5. stop access from auto adding records
    By svcghost in forum Forms
    Replies: 2
    Last Post: 10-21-2010, 05:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums