Results 1 to 5 of 5
  1. #1
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94

    SQL Commands in VBA

    Hello all - I am attempting to insert some SQL commands which I have tested on the server into VBA code, but I am getting stuck on the temp table creation.

    I am attempting to duplicate a large record into a table with a PK and another unique field. My approach is to create a temp table, remove/change the data that needs to be unique and then insert it back into the original table.

    I am getting stuck on the first line when it's embedded in VBA where I get a run-time error 2467 - object closed...

    So the real question is how do I create a temporary table to do what I want to do and then on the back end, how do I remove it? Thanks...



    Code:
    DoCmd.RunSQL ("CREATE TEMPORARY TABLE drc.tmp AS SELECT * FROM tbl_FTSO WHERE TaskSR='" & Me.SourceSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET id = NULL WHERE TaskSR='" & Me.SourceSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET TaskSR='" & Me.TargetSR & " WHERE TaskSR=" & Me.SourceSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET PHIHIPPA = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET SOX = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET PCI = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET CertificateSummary = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET DateCertificateIssued = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET Environment = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET Facility = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET CEDate = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET RIDCert = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET CertNumber = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET CertType = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET PreceedingCertNumber = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("UPDATE tmp SET RecertificationSR = NULL WHERE TaskSR=" & Me.TargetSR & "';")
    DoCmd.RunSQL ("INSERT INTO tbl_FTSO SELECT * FROM tmp;")
    DoCmd.RunSQL ("DROP TABLE tmp;")


  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    My guess that you are trying to set an autonumber (Identity) field to Null.
    Try commenting out this line: (and any other identity field)

    Code:
    DoCmd.RunSQL ("UPDATE tmp SET id = NULL WHERE TaskSR='" & Me.SourceSR & "';")

    PS is this one record or multiple records?

  3. #3
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94

    SQL Commands in VBA

    The issue is that the temp db is not being created. It stops on each line in the code with the same error.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Allen Browne has some examples of using DDL code to create a table. (plus other DDL code)
    See : http://allenbrowne.com/func-DDL.html#CreateTableDDL

    Not knowing how many fields are in a "large record", I would open a filtered recordset and create the insert string on-the-fly, if you are (adding) copying one record. Maybe a little more code, but better than creating and dropping a tmp table every time you want to dup a record.

    Another way would be to create the tmp table - don't drop it. Every time you want to copy a record, delete all records in the tmp table, insert the record(s) you want to copy, change the field data, then append the record(s)to the original table. simpler if you don't have to create & delete a table.

    My$0.02

  5. #5
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    In your Create temporary table statement - what is drc? Could you not create tmp in the current database? If it is supposed to be in the current database, then the problem is drc.tmp - MS Access doesn't like periods in table names.

    John

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Enabling commands for different users
    By glen in forum Forms
    Replies: 28
    Last Post: 10-11-2012, 03:36 PM
  2. Help with SQL INSERT commands
    By chris.williams in forum Programming
    Replies: 6
    Last Post: 09-04-2012, 06:14 PM
  3. clarification with transfer commands
    By mike02 in forum Access
    Replies: 7
    Last Post: 08-08-2012, 09:37 AM
  4. Button Commands
    By JayX in forum Access
    Replies: 13
    Last Post: 12-09-2011, 05:37 PM
  5. If __ Then __ type commands in queries?
    By TUPJK in forum Access
    Replies: 7
    Last Post: 06-20-2011, 01:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums