Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

runtime error 424 object required

Posted on 2004-10-14
14
Medium Priority
?
484 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

604 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