Solved

runtime error 424 object required

Posted on 2004-10-14
14
462 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

705 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

15 Experts available now in Live!

Get 1:1 Help Now