Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    lockstock is offline Novice
    Windows 8 Access 2013
    Join Date
    Sep 2015
    Posts
    19

    auto create records

    hi,

    this is a part of my db:

    tblTask
    *TaskID (primary key)
    Task
    ....

    tblEvaluation
    *Tasks (primary key)
    **Pupil (primary key)
    Evaluation
    ...



    tblPupil
    **PupilID (primary key)
    Name
    ...

    When a record in tblTask is created, i want to create automatically a record in tblEvaluation for each pupil (evaluation left blank)
    Has anyone an idea how this can be done? Can this be done through use of macros or will it need VBA?

    Thanks!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    An INSERT SELECT action is one method.

    A macro would have to run a query object. VBA could run the SQL statement, like:

    CurrentDb.Execute "INSERT INTO tblEvaluation (Tasks, Pupil) SELECT " & Me.tbxTask & " AS TaskID, PupilID FROM tblPupil"

    Do you need to restrict which students are captured?

    Other methods involve VBA and recordset objects and looping code structure.
    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
    lockstock is offline Novice
    Windows 8 Access 2013
    Join Date
    Sep 2015
    Posts
    19
    thanks,
    no just every pupil in the table should do the trick

    i will try to insert the statement somewhere but i'm kind of a newbie if it comes to programming

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Yes, the real trick is figuring out what event(s) to put code into.

    Perhaps the AfterUpdate event of the task control - (is it a combobox?) or a button Click.
    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
    lockstock is offline Novice
    Windows 8 Access 2013
    Join Date
    Sep 2015
    Posts
    19
    the task records are created in a datasheet (sub)form

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    What do you mean by 'task' records - records in tblEvaluation? What is this form/subform arrangement? I envisioned main form bound to tblTask and subform bound to tblEvaluation. Create task record on main form and code will create associated records for tblEvaluation. Refresh/requery the subform to view new records.
    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.

  7. #7
    lockstock is offline Novice
    Windows 8 Access 2013
    Join Date
    Sep 2015
    Posts
    19
    Click image for larger version. 

Name:	Capture.PNG 
Views:	10 
Size:	24.0 KB 
ID:	22114


    maybe this pic will clarify.
    I create the taskIDs in a subform of tblTaskGroups
    every time a TaskID is created i want a record in the tblEvaluation for every PupilID

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You mean main form bound to tblTaskgoups and subform bound to tblTask? Okay, then code behind the tblTasks subform. You just don't need to do requery/refresh because the tblEvaluation records are not displayed.
    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.

  9. #9
    lockstock is offline Novice
    Windows 8 Access 2013
    Join Date
    Sep 2015
    Posts
    19
    i entered
    CurrentDb.Execute "INSERT INTO tblEvaluation (Tasks, Pupil) SELECT" & Me.tblTask & "AS TaskID, PupilID FROM tblPupil"
    in the afterupdate of the subform but i get an error '.tblTask' (from Me.tblTask) is not found?

    Click image for larger version. 

Name:	Capture1.PNG 
Views:	10 
Size:	12.5 KB 
ID:	22115

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    My example is Me.tbxTask. This is an example of referencing a form's textbox control.

    Also, don't drop the spaces I show in the example SQL statement - one is after SELECT and another before AS - within the quote marks.
    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.

  11. #11
    lockstock is offline Novice
    Windows 8 Access 2013
    Join Date
    Sep 2015
    Posts
    19
    so i named the textbox where i type the tasks 'tbxTask' (is that correct?)
    and put this line in the afterupdate of that textbox:
    CurrentDb.Execute "INSERT INTO tblEvaluation (Tasks, Pupil) SELECT " & Me.tbxTask & " AS TaskID, PupilID FROM tblPupil"
    now i get:
    Click image for larger version. 

Name:	Capture1.PNG 
Views:	10 
Size:	6.2 KB 
ID:	22116

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You might have to first commit the task record to table. Record is committed when move to another record, close form, or run code.

    One method:

    DoCmd.RunCommand acCmdSaveRecord

    But tbxTask should be bound to TaskID field. Is this an autonumber PK field? If it is, user should not be able to input a value and therefore the AfterUpdate event should not trigger.

    If you want to provide db for analysis, follow instructions at bottom of my post.
    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.

  13. #13
    lockstock is offline Novice
    Windows 8 Access 2013
    Join Date
    Sep 2015
    Posts
    19
    no its not a autonumber pk field

    the form i'm referring to is the NieuweEvaluatie form

    auto test - Copy.zip

    oh sorry TaskID is indeed auto PK, i bound tbxTask to 'Task' not 'TaskID'

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The only field in the subform is Task which is a descriptive text field. You need the TaskID field for the INSERT action. TaskID is an autonumber field and record must be committed before the TaskID is available and before records can be committed to tblEvaluation (for relational data integrity).

    Private Sub tbxTask_AfterUpdate()
    CurrentDb.Execute "INSERT INTO tblEvaluation (Tasks, Pupil) SELECT " & Me!TaskID & " AS TaskID, PupilID FROM tblPupil"
    End Sub

    I am not sure textbox AfterUpdate will work because focus shifts immediately to a new record since there is nowhere else to go as there is only the one control on the form.

    What is nieuw in NieuweEvaluatie load event? Why does existing record not show on main form?

    You have lookups with alias built in tables - I NEVER do this. I prefer to see actual values in fields when viewing tables.
    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.

  15. #15
    lockstock is offline Novice
    Windows 8 Access 2013
    Join Date
    Sep 2015
    Posts
    19
    i added a taskID field to the subform, named it tbxTask and placed the code in it.
    now when i create a record in tblTasks I don't get any errors but nothing happend either.

    'nieuw' was a macro, I renamed it MacroNieuw

    auto test.zip

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Auto create usernames based on name.
    By Welshhobo in forum Access
    Replies: 8
    Last Post: 10-21-2013, 09:04 AM
  2. Auto create records
    By RamiMohaisen in forum Programming
    Replies: 4
    Last Post: 05-16-2012, 07:52 AM
  3. Replies: 12
    Last Post: 08-30-2011, 03:36 PM
  4. Auto Record create across tables
    By b123 in forum Database Design
    Replies: 15
    Last Post: 10-02-2010, 01:36 AM
  5. Auto-Create a Table
    By Mxcsquared in forum Forms
    Replies: 3
    Last Post: 01-28-2006, 11:36 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