Solved

runtime error 424 object required

Posted on 2004-10-14
14
464 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 49

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
 
LVL 49

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 49

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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 49

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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Auto Filter in Combo Box 7 33
format date field on certain entries 8 29
How autonumber field on a form 16 18
Access database form in matrix view 14 15
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now