Results 1 to 6 of 6
  1. #1
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110

    VBA to insert values in one table from another table.


    I have a Member Details form and when I add a new member I want to be able to add a record to a second table for that member with some default information.

    I have the following VBA code:

    Private Sub btnNewRegistration_Click()
    If Me.Dirty Then Me.Dirty = False
    CurrentDb.Execute "INSERT INTO Registration(ID, PrID, ProgramFee) SELECT " & Me.ID & " AS ID, PrID, ProgramFee FROM Programs WHERE PrID = " & GetDefaultProgram()
    End Sub

    GetDefaultProgram() is a function defined in a module in the database that defines the record to use from the Programs table.

    When I run it I get a compile error: sub or function not defined. When I debug it highlights the .ID portion of Me.ID.

    Any suggestions to resolve this are appreciated.

  2. #2
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    I may not understand your Schema,but you should not have to add Duplicate data under any circumstances. Mainform/Subform is the general way to add data and details.
    tblRegistration
    tblRegistrationDetails
    Is this not what you are trying to do?

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    ID and PrID are number type fields?

    Set a breakpoint on the Execute line. Step debug. Does Me.ID show a value?

    I don't see problem with that code. Maybe the issue is actually with the GetDefaultProgram procedure.
    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.

  4. #4
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110
    There are 3 tables. Members, Programs, and Registration. Members has contact information for the members. Programs has information for the programs members can register for. Registrations contains the information on programs the member has registered for.

    What I want to do is when I add a new member I also want to register them for the default program and add the ProgramFee to the Registration table (Registration Fees may be altered for each record and therefore need to be stored in the registration table. The default program is identified by the GetDefaultProgram() function. ID is a number field identifying the member. PrID is a number field identifying the program.

    I've done a similar task in another database. In that case it inserts each record from another table into the registration table for the member. So basically if there 6 programs then 6 records are inserted into the registration table for the member. What I'm trying to accomplish now is insert just the selected default record into the registration table for the member.

    The button itself only appears when creating a new member.

  5. #5
    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 comments are still applicable.
    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.

  6. #6
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110
    Proof that spelling counts. My original had GetDefaultProgram() when it should have been Get DefProgram()

    Here's the final statement:

    CurrentDb.Execute "INSERT INTO Registration(ID, [PrID], [ProgramFee]) SELECT " & Me.ID & " AS ID, [PrID], [ProgramFee] FROM Programs WHERE [PrID] = GetDefProgram()"

    Thanks for the help.

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

Similar Threads

  1. Replies: 1
    Last Post: 03-17-2014, 02:13 AM
  2. Insert Into Query With Table data and values
    By WickidWe in forum Queries
    Replies: 1
    Last Post: 01-05-2014, 06:41 PM
  3. Replies: 2
    Last Post: 02-04-2012, 01:48 PM
  4. Replies: 2
    Last Post: 12-20-2011, 07:33 AM
  5. Replies: 0
    Last Post: 03-08-2011, 05:56 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