Results 1 to 5 of 5
  1. #1
    monardasprite is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    3

    Object required error in If statement

    Hello,

    I have a form that when a box is checked (a selection made), I'd like the after-update code to run an append query to a table. Just running the append query works fine, but I'm trying to add an If statement afterwards that is bringing up an "object required" error. I know my table name and column name are typed correctly, where else am I going wrong?

    Here's my code:

    Option Compare Database
    Private Sub Check24_AfterUpdate()
    Set db = CurrentDb
    DoCmd.RefreshRecord
    DoCmd.OpenQuery "qryCalculateIngredientsToAddAppendTable"


    If Tables![tblMediaIngredientstoAdd]![TotalLiters] > 31 Then DoCmd.OpenQuery "qryCalculateIngredientsToAddAppendTable" Else
    DoCmd.OpenReport "RptCheckListChooseFromMediaList"
    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
    Option Compare Database
    Option Explicit  ' <<-- should be at the top of EVERY code module
    
    Private Sub Check24_AfterUpdate()
      Set db = CurrentDb
      DoCmd.RefreshRecord
      DoCmd.OpenQuery "qryCalculateIngredientsToAddAppendTable"
      If Tables![tblMediaIngredientstoAdd]![TotalLiters] > 31 Then
         DoCmd.OpenQuery "qryCalculateIngredientsToAddAppendTable" 
      Else
         DoCmd.OpenReport "RptCheckListChooseFromMediaList"
      End if
    End Sub
    AFAIK, you can't refer to a field in a table like that. The table could have many records; which record should be checked to see if it is > 31?

  3. #3
    monardasprite is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    3
    Of course, that makes sense. Thanks Steve. In this particular case, this is a temporary table and the value for [TotalLiters] would be the same, so I wasn't thinking. I think I'll try to solve this by either using a query or setting a variable.

  4. #4
    monardasprite is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    3
    I'm still having trouble with this. I get a runtime error 13 Type Mismatch message

    Here's my code. Any thoughts?
    Option Compare Database
    Option Explicit
    Private Sub Check24_AfterUpdate()
    Dim TotalLiters As String
    TotalLiters = "Select LitersNeeded From [tblMediaList] WHERE [Select Media] = true"
    'There would only be one line in the table where Select Media =True'
    DoCmd.RefreshRecord
    DoCmd.OpenQuery "qryCalculateIngredientsToAddAppendTable"
    If TotalLiters > 31 Then DoCmd.OpenQuery "qryCalculateIngredientsToAddAppendTable" Else
    DoCmd.OpenReport "RptCheckListforDaveChooseFromMediaList", acViewReport
    End Sub

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
      If TotalLiters > 31 Then
    You have delcared "TotalLiters" as a STRING, but the statement above is comparing a string to a number.
    "TotalLiters" is a string , 31 is a number.
    31 (no quotes - a number) is different than "31" (with quotes - a string)


    Code:
    TotalLiters = "Select LitersNeeded From [tblMediaList] WHERE [Select Media] = true"
    You cannot set a variable equal to a SQL statement and have a value returned.
    You can open a recordset
    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub Check24_AfterUpdate()
       Dim TotalLiters As String
       Dim rs as DAO.Recordset
    
       Set rs = Currentdb.Openrecordset("Select LitersNeeded From [tblMediaList] WHERE [Select Media] = true")
       'There would only be one line in the table where Select Media =True'
       If not rs.BOF and not rs.EOF Then
          TotalLiters = rs!LitersNeeded 
       End If
    
       'DoCmd.RefreshRecord
       DoCmd.OpenQuery "qryCalculateIngredientsToAddAppendTable"
       If TotalLiters > 31 Then 
           DoCmd.OpenQuery "qryCalculateIngredientsToAddAppendTable" 
       Else
           DoCmd.OpenReport "RptCheckListforDaveChooseFromMediaList", acViewReport
       End If
    End Sub
    or you could use DLookup()
    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub Check24_AfterUpdate()
      Dim TotalLiters As String
    
      TotalLiters = DLookup("LitersNeeded","tblMediaList", "[Select Media] = true")
      'There would only be one line in the table where Select Media =True'
      DoCmd.RefreshRecord
      DoCmd.OpenQuery "qryCalculateIngredientsToAddAppendTable"
      If TotalLiters > 31 Then 
         DoCmd.OpenQuery "qryCalculateIngredientsToAddAppendTable" 
      Else
         DoCmd.OpenReport "RptCheckListforDaveChooseFromMediaList", acViewReport
      End If
    End Sub

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

Similar Threads

  1. Runtime 424 error... Object required.
    By sanderson in forum Programming
    Replies: 8
    Last Post: 08-09-2015, 08:10 PM
  2. Replies: 5
    Last Post: 01-31-2013, 01:39 PM
  3. Object Required Error
    By sgp667 in forum Programming
    Replies: 1
    Last Post: 11-06-2012, 03:15 AM
  4. Error: Object Required
    By compooper in forum Programming
    Replies: 6
    Last Post: 06-22-2011, 07:52 AM
  5. Object Required Error.
    By Robeen in forum Forms
    Replies: 1
    Last Post: 03-28-2011, 10:30 AM

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