Results 1 to 5 of 5
  1. #1
    Kirtap is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    22

    Can't get transaction in VBA working

    Hello,



    I'm trying to get the following transaction code working for importing data (all fields are text) from one table into another table (different as fields link to other tables in order to take care of spelling stuff by matching to correct names). I'll show the relevant code. If this is not enough to help me, please let me know.
    I am using MySQL ODBC 5.1 as the tables are on a server.

    Code:
     
    
    Public Function importEMCTable()
    
    'Vars for wksp for transactions
    Dim wksp As DAO.Workspace   'For the transaction
    Dim Assets As DAO.Recordset  'A join of two tables to put data into
    Dim import_table_rs 'Table I am importing data from, used for do loop
    
    'Open Assets JOIN GPCL_Asset_Details to put data into
    Set Assets = CurrentDb.OpenRecordset("SELECT Assets.Asset_ID, Assets.GPCL_Asset_ID, Assets.Asset_Type, Assets.Manufacturer, Assets.Model, Assets.Serial_Number,    Assets.Description, GPCL_Asset_Details.GAD_ID, GPCL_Asset_Details.GAD_Asset_Link, GPCL_Asset_Details.Status, GPCL_Asset_Details.Manual_Number, GPCL_Asset_Details.Location FROM Assets LEFT JOIN GPCL_Asset_Details ON Assets.Asset_ID = GPCL_Asset_Details.GAD_ID", dbOpenDynaset)
    
    'Open table to import data from
    Set import_table_rs = CurrentDb.OpenRecordset("SELECT Import_ID, GPCL_Asset_ID, Asset_Type, Manufacturer, Model, Serial_Number, Asset_Status, Location, Manual_Number FROM EMC_Import_Table") '
        
    Set wksp = DBEngine.Workspaces(0) 
    
    Do While Not import_table_rs.EOF
    'Code for setting variables is performed
    'Some of the fields in the MySQL can be NULL like GPCL_Asset_ID, Manufacturer, Model, Serial_Number, Description, some others
    'Note I am setting primary keys manually because I need to set the Link that joins the two tables
    'All fields are set, but in debug the null fields are given the value of Empty
    
            With Assets
                .AddNew
                    !Asset_ID = input_Asset_ID
                    !GPCL_Asset_ID = input_GPCL_Asset_ID
                    !Asset_Type = input_Asset_Type
                    !Manufacturer = input_Manufacturer
                    !Model = input_Model
                    !Serial_Number = input_Serial_Number
                    !Description = input_Description
                    !GAD_ID = input_GAD_ID
                    !GAD_Asset_Link = input_GAD_Asset_Link
                    !Status = input_Status
                    !Manual_Number = input_Manual_Number
                    !Serial_Number = input_Serial_Number
                    !Location = input_Location
                .Update
            End With
    
            'To increase the Primary Keys each iteration, I add DMax and a variable called iteration.  There is no conflict in primary keys.
            iteration = iteration + 1
    Loop
    
    'Then I commit
    wksp.CommitTrans (dbForceOSFlush)
    
    
    'I also have code for closing the connections and cleaning up vars.
    End Function
    It gets to Update and then I get RunTime Error 3146 ODBC--call failed.

    Do I have a complete misunderstanding of how transactions work or is there an incompatibility? All the datatypes do match up.

    Also, I don't need to worry about conflict because this a one time script I run. I was told this was a faster way then running multiple sql statements as I have to import around 1000 records. Occasionally I have to work over a VPN and SQL really makes all the difference I noticed when I ran an SQL statement that limited the data taken in from other tables to match against names in a table.

    Edit: Sorry if I put this in the wrong section.

    Thank you in advance,
    Kirtap
    Last edited by Kirtap; 10-02-2013 at 05:30 AM. Reason: Fixing formatting and added function name to top

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Maybe you didn't post the whole Function.... but here are my observations (FWIW) Some comments in line.

    ( I did make a couple of changes .... a variable "sSQL" - makes it easier for me to read)
    Code:
    Option Compare Database   'should be at the top of every module
    Option Explicit           'should be at the top of every module
    
    Public Function importEMCTable()
    
       'Vars for wksp for transactions
       Dim wksp As DAO.Workspace   'For the transaction
       Dim Assets As DAO.Recordset  'A join of two tables to put data into
       Dim import_table_rs   'Table I am importing data from, used for do loop
       ' Why is import_table_rs  not declared as DAO.Recordset?????
    
       Dim iteration As Long
       Dim sSQL As String
    
       iteration = 0
    
       'Open Assets JOIN GPCL_Asset_Details to put data into
       sSQL = "SELECT Assets.Asset_ID, Assets.GPCL_Asset_ID, Assets.Asset_Type,"
       sSQL = sSQL & " Assets.Manufacturer, Assets.Model, Assets.Serial_Number, Assets.Description,"
       sSQL = sSQL & " GPCL_Asset_Details.GAD_ID, GPCL_Asset_Details.GAD_Asset_Link, GPCL_Asset_Details.Status,"
       sSQL = sSQL & " GPCL_Asset_Details.Manual_Number, GPCL_Asset_Details.Location"
       sSQL = sSQL & " FROM Assets LEFT JOIN GPCL_Asset_Details ON Assets.Asset_ID = GPCL_Asset_Details.GAD_ID"
       '  Debug.Print sSQL
       Set Assets = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset)
    
       'Open table to import data from
       sSQL = "SELECT Import_ID, GPCL_Asset_ID, Asset_Type, Manufacturer, Model,"
       sSQL = sSQL & " Serial_Number, Asset_Status, Location, Manual_Number "
       sSQL = sSQL & " FROM EMC_Import_Table"
       '  Debug.Print sSQL
       Set import_table_rs = CurrentDb.OpenRecordset(sSQL)      '
    
       Set wksp = DBEngine.Workspaces(0)
    
       '
       '    Where is the "wksp.BeginTrans" command??? And maybe "wksp.Rollback" if there is an error??
       '
    
       'I would check to ensure there are records in the recordset or at least move to the first record in the record set
       import_table_rs.MoveFirst
    
       Do While Not import_table_rs.EOF
          'Code for setting variables is performed
          'Some of the fields in the MySQL can be NULL like GPCL_Asset_ID, Manufacturer, Model, Serial_Number, Description, some others
          'Note I am setting primary keys manually because I need to set the Link that joins the two tables
          'All fields are set, but in debug the null fields are given the value of Empty
    
          With Assets
             .AddNew
             !Asset_ID = input_Asset_ID
             !GPCL_Asset_ID = input_GPCL_Asset_ID
             !Asset_Type = input_Asset_Type
             !Manufacturer = input_Manufacturer
             !Model = input_Model
             !Serial_Number = input_Serial_Number
             !Description = input_Description
             !GAD_ID = input_GAD_ID
             !GAD_Asset_Link = input_GAD_Asset_Link
             !Status = input_Status
             !Manual_Number = input_Manual_Number
             !Serial_Number = input_Serial_Number
             !Location = input_Location
             .Update
          End With
    
          'To increase the Primary Keys each iteration, I add DMax and a variable called iteration.  There is no conflict in primary keys.
          iteration = iteration + 1
    
          'need to move to the next record or you are stuck in an endless loop!!
          import_table_rs.MoveNext
       Loop
    
       'Then I commit
       wksp.CommitTrans (dbForceOSFlush)
    
       'I also have code for closing the connections and cleaning up vars.
    End Function
    Out of curiosity, you opened two records sets: "Assets" and "import_table_rs". You are creating records in "Assets", but no data is being read from/written to "import_table_rs".
    You have lines like:
    Code:
             .AddNew
             !Asset_ID = input_Asset_ID
             !GPCL_Asset_ID = input_GPCL_Asset_ID
    It looks like the same data is being written to every new record written to "Assets".

    The last part of the code confuses me...

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    Further to Steve's comments, you might wish to review transaction processing with Access.
    see http://msdn.microsoft.com/en-us/libr...ice.12%29.aspx

    It may be helpful to readers if you told us WHAT you are trying to in plain English.
    Good luck.

  5. #5
    Kirtap is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    22
    Hey guys,
    Thank you very much for your responses. I appreciate it. Sorry for my late response.

    @Steve: Sorry, I screwed up in trying to paste relevant code. It ended up being messy and I apologize.
    Assets' record source I tried to set as a join of two tables (Assets and GPCL_Asset_Details) to reduce the number of sql statements.

    Although I didn't notice that I didn't set import_table_rs as DAO.RecordSet. Thanks! Also thank you for pointing out the naming scheme sSQL. This is a way better practice than what I had. I really should have named the DAO.Recordset Assets as rsAssets.

    The value of GPCL_Asset_ID is a field for engineers to identify their assets. I apologize for the confusion of that.
    GAD_ID is the primary key for GPCL_Asset_Details table, which has a one to one relationship with the Assets table.
    The relationship is determined by a field in the GPCL_Asset_Details table called GAD_Asset_Link, which corresponds to the Asset_ID field in the Assets table.

    @Orange: My bad for not explaining what my objective was.
    My objective is to import records of information on assets from a table (import_emc_table) into two tables (Assets and GPCL_Assets, which have a One-To-One relationship. However, its not just a simple import. In order to maintain consistency with other tables I have to import from, I have tables containing proper names of Asset Types and Manufacturers that have a unique id that the Asset_Types and Manufacturers link to. So basically I am iterating through each record in import_emc_table and breaking the data from import_emc_tables into variables, which I place into the two tables. Unfortunately I have to go through over a 1000 records. If a matching asset type or manufacturer is not found, a popup pops up asking them to select an available asset type/manufacturer. There is also the option to add a new Asset Type/Manufacturer if it is not found.


    However, I figured out the transaction wasn't the problem. The problem I was having was when I tried adding a record by the "With DAO.RecordSource" method
    Code:
    With Assets
            .AddNew 
             !Asset_ID = input_Asset_ID 
             !GPCL_Asset_ID = input_GPCL_Asset_ID          
             !Asset_Type = input_Asset_Type          
             !Manufacturer = input_Manufacturer          
             !Model = input_Model          
             !Serial_Number = input_Serial_Number          
             !Description = input_Description          
             !GAD_ID = input_GAD_ID          
             !GAD_Asset_Link = input_GAD_Asset_Link          
             !Status = input_Status          
             !Manual_Number = input_Manual_Number          
             !Serial_Number = input_Serial_Number          
             !Location = input_Location          
             .Update       
    End With
    I figured out the transaction wasn't the problem when I tried using several DoCmd.RunSQL statements.
    The problem I was having was when I tried adding a record by the "With DAO.RecordSource" method of adding things.

    Or perhaps this can't be done with the MySQL odbc?
    Thank you for your responses and I am sorry for any inconvenience.
    Last edited by Kirtap; 10-03-2013 at 06:31 AM. Reason: Edit: Attempting to fix formatting. I think something is wrong with my browser.

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

Similar Threads

  1. Transaction Log
    By gtimmies in forum Database Design
    Replies: 1
    Last Post: 05-27-2013, 03:27 PM
  2. How to Print current Transaction?
    By cap.zadi in forum Forms
    Replies: 1
    Last Post: 12-08-2011, 07:47 AM
  3. Replies: 7
    Last Post: 11-14-2011, 05:59 PM
  4. SQL Transaction question
    By Mazdaspeed6 in forum Programming
    Replies: 4
    Last Post: 12-16-2010, 12:51 PM
  5. Latest Transaction Report
    By Worm in forum Reports
    Replies: 3
    Last Post: 08-16-2010, 06:10 AM

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