Solved

runtime error 424 object required

Posted on 2004-10-14
14
468 Views
Last Modified: 2012-06-21
Hi, Im trying to retrieve an autonumber generated by the insert query. I then select it into a recordset, taht goes ok but I get an error massage:

runtime error 424 object required

at the first line of the if statement, any help appreciated.


DoCmd.RunSQL "INSERT INTO clientservices2 ( serviceid, orgid) VALUES ( " & serviceid & ", " & orgid & ")"
     
     DoCmd.RunSQL "SELECT  Max(clientservices2.clientid) into rst from clientservices2"
     
     If (rst.RecordCount > 0) Then
        rst.MoveFirst
        clientid2 = rst(0)
        MsgBox (clientid)
        Else
        MsgBox "no record in rst"
    End If

WD
0
Comment
Question by:BobNZ2
  • 6
  • 5
  • 3
14 Comments
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 125 total points
ID: 12315145
is you define the object rst in your code? i guess you try to output the Max client ID?, try like:

...

DoCmd.RunSQL "INSERT INTO clientservices2 ( serviceid, orgid) VALUES ( " & serviceid & ", " & orgid & ")"

Dim rst As DAO.Recordset
SQLStr = SELECT  Max(clientservices2.clientid) from clientservices2
     
     DoCmd.RunSQL "SELECT  Max(clientservices2.clientid) into rst from clientservices2"

Set rst = CurrentDb.OpenRecordset(SQLStr)
     
     If (rst.RecordCount > 0) Then
        rst.MoveFirst
        clientid2 = rst(0)
        MsgBox (clientid)
        Else
        MsgBox "no record in rst"
    End I

...

for the code above, you need to add M$ DAO Object Library from Reference, hope this helps
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 12315155
Hi there

Your SELECT statement is actually creating a table called rst with the results of the SELECT

try this instead

Set rst = CurrentDb.OpenRecordset("SELECT  Max(clientservices2.clientid) from clientservices2")

HTH

Scott
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 12315158
:ryancys

sorry for duplicate there :)

Scott
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 50

Expert Comment

by:Ryan Chong
ID: 12315317
it's ok Scott, btw, i forget to put a couple of " there.. so it should be as:

...
SQLStr = "SELECT  Max(clientservices2.clientid) from clientservices2"
...

cheers 8-)
0
 

Author Comment

by:BobNZ2
ID: 12315432
Thanks for the replies people
This is what I now have...

DoCmd.RunSQL "INSERT INTO clientservices2 ( serviceid, orgid) VALUES ( " & serviceid & ", " & orgid & ")"

    Dim rst As DAO.Recordset
    SQLStr = "SELECT  Max(clientid) from clientservices2"    
    Set rst = CurrentDb.OpenRecordset(SQLStr)
     
    If (rst.RecordCount > 0) Then
        rst.MoveFirst
        clientid2 = rst(0)
        MsgBox (clientid)  //this outputs empty message box
        Else
        MsgBox "no record in rst"
    End If
       
    'DoCmd.SetWarnings (False) ' Hides the msgbox's
    DoCmd.RunSQL "insert into invoice (clientid) values (" & clientid2 & ")"  //and this puts out key violation message maybe
    because clientid is empty

WD
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 12315517
try use rst.eof = false instead:

...

DoCmd.RunSQL "INSERT INTO clientservices2 ( serviceid, orgid) VALUES ( " & serviceid & ", " & orgid & ")"

    Dim rst As DAO.Recordset
    SQLStr = "SELECT  Max(clientid) from clientservices2"    
    Set rst = CurrentDb.OpenRecordset(SQLStr)
     
    If (rst.eof = false) Then
        clientid2 = rst(0)
        MsgBox (clientid)
    Else
        MsgBox "no record in rst"
   
'     exit sub

    End If
       
    'DoCmd.SetWarnings (False) ' Hides the msgbox's
    DoCmd.RunSQL "insert into invoice (clientid) values (" & clientid2 & ")"

...
0
 

Author Comment

by:BobNZ2
ID: 12315673
Hmm no luck but when I held the cursor over "rst.recirdcount>0" the popup says "rst.recordcount=1" and

with "rst.eof=false"  it says "rst.eof=false"
holding the cursor over

clientid2=rst(0) the popup shows 106 which is the max value of clientid2

somewhat hinky

WD
0
 

Author Comment

by:BobNZ2
ID: 12315755
I have 2 tables 'clientservices' and 'clientservices2' that both insert into the clientid field in table 'invoice'
both clientid and clientid2 are autonumbers  is this what is causing the key violations?
 shame I can't upload schema.

WD
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 12315829
Is the column client in table invoice allow duplicate values? Do you setup any Rules, Referential Integrity for your tables?
0
 

Author Comment

by:BobNZ2
ID: 12315923
clientid field in table invoice is yes duplicates allowed
and yes referential integrity is enforced
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 12316029
Here is my suggestion:

Try backup your access file, then remove the referential integrity, see if your codes can be executed successfully or not. If yes then that means it's the referential integrity problem..

regards
0
 

Author Comment

by:BobNZ2
ID: 12316125
hmmm yea its the referential integrity once thats removed everything works ok.
but doesn't having the ref integrity off puts the database at risk?

WD
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 12316128
Hi what is the datatype of the clientid field in the invoice table?

Scott
0
 

Author Comment

by:BobNZ2
ID: 12316161
Thanks Ryanc points are yours

bummer Colosseo I had already assigned points.

" Hi what is the datatype of the clientid field in the invoice table?"

number

WD
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question