Results 1 to 11 of 11
  1. #1
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53

    VBA - Select certain entries and paste contents into another table

    Hello, using VBA I would like to select entries in a table where a certain criteria is met. I would then like a count of those entries.

    Those selected entries would then be pasted into another table but not all of the columns, only a select few.



    How would I go about doing this? Would I need to store the entries as a list and then populate the other table using a for statement and DAO code similar to the one below?

    Code:
    Dim rstProjects As DAO.Recordset
    Dim dbTable As DAO.Database
    Dim pnumber As Integer
    pnumber = Text198.Value
            
    Set dbTable = CurrentDb
    Set rstChanges = dbTable.OpenRecordset("Active Projects")
    rstChanges.AddNew
    rstChanges("Project_Number").Value = pnumber
    rstChanges.Update

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Select the items on a form, the execute an append query.
    One way would be to allow users to click from a list. The action is an append query to the target table.

    Click image for larger version. 

Name:	pick3-states how2.png 
Views:	11 
Size:	29.2 KB 
ID:	17437

  3. #3
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53
    I don't want the user to be able to select which items, the items will always be the same.

  4. #4
    AnandKhinvasara is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Posts
    31
    Insert into table2
    select column1, column2
    where column1 = <criteria>

  5. #5
    AnandKhinvasara is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Posts
    31
    In VBA you can assign the SQL to a string, and then fire it using docmd.runsql function.
    Docmd.RunSQL strQuery

  6. #6
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53
    Quote Originally Posted by AnandKhinvasara View Post
    Insert into table2
    select column1, column2
    where column1 = <criteria>
    Ok thanks, I will give this a try.

  7. #7
    AnandKhinvasara is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Posts
    31
    To be very specific, heres the right syntax. The column names in Insert statement should match with select statement. Here you can only mention the columns you actually want to add data to. Hope this helps !

    INSERT INTO Employees ([First Name], [LAst Name], [Employee Code])
    SELECT FirstName, LastName, RefCode
    FROM Candidates WHERE InterviewPassed = TRUE;

  8. #8
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53
    Quote Originally Posted by AnandKhinvasara View Post
    To be very specific, heres the right syntax. The column names in Insert statement should match with select statement. Here you can only mention the columns you actually want to add data to. Hope this helps !

    INSERT INTO Employees ([First Name], [LAst Name], [Employee Code])
    SELECT FirstName, LastName, RefCode
    FROM Candidates WHERE InterviewPassed = TRUE;
    Ok thanks so much I kinda have this working.

    Now lets say the Employees have a project number assigned to them and I have the project number in my vba code that also needs to be inserted.

    I attempted at doing that but I don't think I've done it right

    SQL = "INSERT INTO Employees ([First Name], [Last Name], [Employee Code], [Project Number))" & _
    "SELECT FirstName, LastName, RefCode," & Project_Number & _
    "FROM Candidates WHERE InterviewPassed = TRUE;"

  9. #9
    AnandKhinvasara is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Posts
    31
    "SELECT FirstName, LastName, RefCode," & Project_Number & _

    above will work if [project number] of employees table is actually a number field.

    If not use,

    "SELECT FirstName, LastName, RefCode,'" & Project_Number & "'" & _

  10. #10
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53
    Thank you so much for your help AnandKhinvasara, I finally got this working.

  11. #11
    AnandKhinvasara is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Posts
    31
    Good to know )

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

Similar Threads

  1. Replies: 4
    Last Post: 01-20-2014, 11:50 AM
  2. Delete Contents of worksheet before export - xlWSh.Cells.Select
    By allenjasonbrown@gmail.com in forum Programming
    Replies: 1
    Last Post: 10-14-2012, 10:38 PM
  3. Replies: 5
    Last Post: 04-18-2012, 12:04 PM
  4. Replies: 3
    Last Post: 02-28-2012, 12:12 AM
  5. Select contents of a field on click?
    By anoob in forum Access
    Replies: 7
    Last Post: 03-01-2011, 07:59 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