Results 1 to 10 of 10
  1. #1
    DenShin is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    8

    How to get data from one table to another table using the vba code

    Click image for larger version. 

Name:	test.JPG 
Views:	33 
Size:	14.5 KB 
ID:	51143Click image for larger version. 

Name:	test1.JPG 
Views:	34 
Size:	64.5 KB 
ID:	51142

    image in the right is the criteria on when the process of copying will trigger. First, based from the value of the "Select Here" which is the "CODE" in the fields what ever the code matches on the table will be copied. Two, the checkbox is a trigger also to what table will be the source and destination table will the data will insert into. By the way all tables are link to sql server. can anyone help what codes will be used here?

    Noted: Source tablename is definite with the "@" sign of the name.

    used codes:
    CurrentDb.Execute "INSERT INTO [Temp_promo] SELECT fields1,fields1,fields3 FROM [@Promo] WHERE [code] = '" & Me.cmbcode & "';" - doesn't work
    Run-time error '3622' - You must use the dbseechanges opetion with openrecordset when accessing a sql server table that has an identity column.

    CurrentDb.Execute "INSERT INTO [Temp_promo] SELECT fields1,fields1,fields3 FROM [Promo] WHERE [code] = '" & Me.cmbcode & "';" - This work but i need to changed the "@PROMO" table to "PROMO" table, which as i said must definite to "@PROMO" only.


    Private Sub Copy_Click()
    Dim db As Database
    Dim rsSource As Recordset
    Dim rsDestination As Recordset

    If Shw1 = True Then
    'Open a recordset for the source table'
    Set db = CurrentDb
    Set rsSource = db.OpenRecordset("SELECT Code,Name FROM [@Promo] WHERE [code] = '" & Me.cmbcode & "';", dbOpenDynaset, dbSeeChanges)



    'Open a recordset for the destination table'
    Set rsDestination = db.OpenRecordset("TEMP_PROMO", dbOpenDynaset, dbSeeChanges)

    'Check if there are records in the source Recordset '
    If Not rsSource.EOF Then
    rsSource.MoveFirst 'Move to the first record in the source Recordset'

    'Loop through records in the source Recordset'
    Do Until rsSource.EOF
    'Add a new record to the destination Recordset'
    rsDestination.AddNew


    'Set values in the destination Recordset based on the source Recordset'
    rsDestination!Code = rsSource!Code
    rsDestination!Name = rsSource!Name
    'Repeat for other fields...'
    'Update the destination Recordset'
    rsDestination.Update

    'Move to the next record in the source Recordset'
    rsSource.MoveNext
    Loop

    End If
    rsSource.Close
    rsDestination.Close
    Set rsSource = Nothing
    Set rsDestination = Nothing
    Else
    'Do nothing'
    End If
    - this work but for only one table only to another only. adding more will not work.
    annyone can help me about this? very much appriciated

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Not sure this is any better explanation than cross-posted thread https://stackoverflow.com/questions/...75238_77582392
    Last comment of that thread:
    "I just tested running an INSERT SELECT on my linked SQLServer table and also get that error when usingCurrentDb.Execute.
    DoCmd.RunSQLgenerates different error related to timestamp field. Does your table have timestamp field? How many fields are involved?
    Suggest you explicitly reference fields in the INSERT and SELECT parts - worked for me."

    Can concatenate table name into SQL statement.


    Should post code between CODE tags to retain indentation and readability.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    DenShin is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    8
    Yeah its the same question from the cross-posted. But I can't explain well to the other thread, Cause I can't show the picture of what i'm trying to do. Yes, I do have a timestamps on the fields 3 dates but from the @PROMO only, other tables has no timestamp included.

    fields varies, but max of 16 fields and min of 6 fields per table:

    (Checked box: Headers (if True)) = From (@Promo - 16 fields - 3 Time Stamps - 1 Auto Increment) insert into (TEMP_PROMO) where Code = '" & me.txtcode &"';"

    (Checked box: Area/s (if True)) = (@Promo_Area - 6 fields - 0 Time Stamps - 0 Auto Increment) insert into (TEMP_PROMO_AREA) where Code = '" & me.txtcode &"';"

    (Checked box: Business Partner/s (if True)) = (@Promo_BP - 6 fields - 0 Time Stamps - 0 Auto Increment) insert into (TEMP_PROMO_BP) where Code = '" & me.txtcode &"';"

    (Checked box: Main Items/s (if True)) = (@Promo_Details - 13 fields - 0 Time Stamps - 0 Auto Increment) insert into (TEMP_PROMO_BP) where Code = '" & me.txtcode &"';"

    (Checked box: Exempted Prom (if True)) = (@Promo_Exempted - 5 fields - 0 Time Stamps - 0 Auto Increment) insert into (TEMP_PROMO_EXEMPTED) where Code = '" & me.txtcode &"';"

    (Checked box: Free Item/s (if True)) = (@Promo_Free - 13 fields - 0 Time Stamps - 0 Auto Increment) insert into (TEMP_PROMO_FREE) where Code = '" & me.txtcode &"';"

    that is how the needed codes to work out.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What do you mean by "timestamp"? I mean a timestamp (aka rowversion) type field in SQLServer. Actually has nothing to do with a date/time data type. AFAIK, table can have only one of these fields.

    You need to build SQL statement(s) based on selections on form? If user checks any or all of checkboxes, run SQL to insert records from/to associated tables - up to 6 table pairs? So need some sort of looping code structure. Consider a multi-select listbox instead of multiple checkboxes. Looping through selected listbox items would be simpler.

    I don't really understand this statement "This work but i need to changed the "@PROMO" table to "PROMO" table, which as i said must definite to "@PROMO" only."
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Possible crosspost here ? https://www.access-programmers.co.uk...by-vba.329563/

    If not, new advice.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Have you tried

    CurrentDb.Execute "INSERT INTO [Temp_promo] SELECT fields1,fields1,fields3 FROM [@Promo] WHERE [code] = '" & Me.cmbcode & "';", dbSeeChanges
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    DenShin is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    8
    Yes @pbaldy, this works just fine for the first checkbox which is the Header. But using that code for the next check box nothing seems to work, adding the dbSeeChanges or removing it in the end of the statement.. it doesn't work anymore. Why is that?
    sample of my code upon clicking the copy button.

    If chckbox1 = true then
    CurrentDb.Execute "INSERT INTO [Temp_promo] SELECT fields1,fields1,fields3 FROM [@Promo] WHERE [code] = '" & Me.cmbcode & "';", dbSeeChanges
    else
    'do nothing
    end if


    If chckbox2 = true then
    CurrentDb.Execute "INSERT INTO [Temp_promo_details] SELECT fields1,fields1,fields3 FROM [@Promo_details] WHERE [code] = '" & Me.cmbcode & "';", dbSeeChanges "with or without"
    else
    'do nothing
    end if


    If chckbox3 = true then
    CurrentDb.Execute "INSERT INTO [Temp_promo_Free] SELECT fields1,fields1,fields3 FROM [@Promo_Free] WHERE [code] = '" & Me.cmbcode & "';", dbSeeChanges "with or without"
    else
    'do nothing
    end if


    and so on.....

    i tried also puting some msgbox "" to check if the code runs correctly. and the process is correctly executed by why is that nothing copied from one table to another once the checkbox is checked from main item/s until exempted.

    i also tried checking the main item/s but executed well but nothing copied from the @promo_details table to temp_promo_details.

  8. #8
    DenShin is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    8
    Im sorry June7 I don't have any slightest idea on what timestamp is used.

    And Yes, i need to build vba codes or sql statement as long it runs if the checkbox being checked and table to table will be copied from source to destination table. this can be 1 checkbox selected or all the checkbox selected.

    Just ignore the last statement.. heheh

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Have you tried to see if the Select statements for the other checkboxes return any records? Debug.Print just the Select part of the Insert statements, then copy it from the intermediate window into a new query and run it.
    Can you please show us a screenshot of the navigation pane of your Access front-end with the two sets of linked tables? Is this Access file used for anything else other than this copying task? You can very easily loop through the tabledefs in code and rename the "@xxx" as "s_xxx" (for "source"):
    Code:
    Public Sub vcRenameLinks()
    Dim tdf As DAO.TableDef
    Dim db As DAO.Database          
        
    Set db = CurrentDb                      
       
    For Each tdf In db.TableDefs
    	If Len(tdf.Connect) > 0 Then
    		If Left(tdf.Name, 1) = "@" Then tdf.Name = Replace(tdf.Name, "@", "s_")
    		tdf.RefreshLink		  
    	End If
    Next tdf
    
    
    End Sub
    Maybe post the exact code (the entire sub) you are trying to run and not the "Field1,Field2,..." pseudo code.
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    For chuckles, try this:

    , dbSeeChanges + dbFailOnError

    It may throw an error that tells you what's wrong.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Code to input data from form into table
    By Rgaming in forum Access
    Replies: 8
    Last Post: 10-11-2017, 02:09 PM
  2. SQL code to create table of external data
    By mcunkelman in forum Queries
    Replies: 3
    Last Post: 09-30-2015, 11:28 AM
  3. Trying to pull data from table into VBA code
    By kcrty in forum Programming
    Replies: 8
    Last Post: 04-08-2015, 06:37 PM
  4. Replies: 6
    Last Post: 02-09-2015, 12:17 PM
  5. Code to move data from table to another
    By zachlunch in forum Programming
    Replies: 1
    Last Post: 08-30-2013, 12:12 PM

Tags for this Thread

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