?
Solved

runtime error 424 object required

Posted on 2004-10-14
14
Medium Priority
?
475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 500 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 53

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 53

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 53

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 53

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 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