Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Behedwin is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    61

    Question How to add data to my tables?

    Hi

    I have a access project that i hope i can finish.
    Im trying to make a staff profile with a log of data for each staff member.

    I have created the forms and so far i have a delete button for the data in the log table.
    but now i want to add a "add" button.

    when the user click on the add button i want a new form to open where they fill out data to the form and then save it to that userprofile
    also i want to add an edit button so i can edit... the same way, a new window is opened and the user can edit and save.

    i guess this is done in VBA code and i have no clue how to do this.
    any advice would be greate


    i have uploaded my file here, please take a look at it.
    Last edited by Behedwin; 11-02-2017 at 03:57 PM.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Simplest approach is a form/subform arrangement. Otherwise, code to open the log form for a new record will have to pass the ProfileID value and populate the foreign key field. Common topic. Review https://www.accessforums.net/showthr...light=OpenArgs

    Regardless, one form can serve for log.
    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
    Behedwin is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    61
    Well i know the simplest way is to use a subform arrangement....
    but id like to learn how to make it work how i hope to have it

    this code generates an error... and i dont know why
    Code:
    Private Sub AddLogRow_Button_Click()
    DoCmd.OpenForm "AddLog_Form", , , "Profile_ID = " & Me.txtProfile_ID, acFormAdd, , Me.txtProfile_ID
    End Sub
    The code is placed on the add log row button when someone clicks on it.

    I get this error message
    http://prntscr.com/h5ghcn


    Any idea why i get this error and how to fix it.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Well, if you are adding a new record then there is no reason to use filter criteria.

    What is the error message?
    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
    Behedwin is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    61
    Click on the link to see a screenshot of the error message: https://prnt.sc/h5ghcn

    I am so used to excel and the VBA in there... im so confused over what im looking at in Access VBA.

    Im having trouble formulating what i want to do...
    click on button > open new form based on the current opened record in the first form.

    Table 1: Profile_table
    Primary Key = profile_ID

    Table 2: Log_table
    Primary Key = Log_ID
    Secondary Key = Profile_ID_SK (relationship to primary key in profile_table)

    So if user clicks on button "add new log row" in profile_form.
    Id like to open a window displaying the log_form so the user can add data to the users profile based on log_id and profile_id

    Hope this makes any kind of sense lol

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Did you follow the examples in the referenced thread?
    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
    Behedwin is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    61
    Yes.
    I have that file
    I see that it works.

    but i can not recreate it with the code from that file.
    I just get the same errors...

    im missing something.
    but dont know what

    cant find any problems really in my file.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Your code does not follow the referenced example. However, that example could use an adjustment.

    Need a record in Profile_Table first. If is a new record on Profile_Form, must be committed to table before opening the Log form.

    If Me.Dirty Then Me.Dirty = False
    If Not IsNull(Me.txtProfile_ID) Then DoCmd.OpenForm "AddLog_Form", , , , acFormAdd, , Me.txtProfile_ID
    End If

    Also need code behind the Log form.
    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
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Because the two tables are linked and RI is enforced, you MUST have a value from Profile_Table.Profile_ID in Log_Table.Profile_ID_SK.
    You are trying to create a record in Log_Table.Profile_ID_SK that has a value of 0, but there is no record with a value of 0 in Profile_Table.Profile_ID


    You have the Profile_ID (PK field) from the "Profile_Table" for a person in the openargs argument but you NEVER USE IT.

    So on the form "AddLog_Form", you need to have a hidden text box control bound to the "Profile_ID_SK" field.
    When the "AddLog_Form" opens, code in the on load event code takes the value in the openargs argument and puts it in the "Profile_ID_SK" text box.

    THEN you can save the record.



    PS... it looks like you might have problems with the delete log row code also. Are you wanting to delete ONE specific log record or ALL log records for a specific person??

  10. #10
    Behedwin is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    61
    Quote Originally Posted by ssanfu View Post
    Because the two tables are linked and RI is enforced, you MUST have a value from Profile_Table.Profile_ID in Log_Table.Profile_ID_SK.
    You are trying to create a record in Log_Table.Profile_ID_SK that has a value of 0, but there is no record with a value of 0 in Profile_Table.Profile_ID


    You have the Profile_ID (PK field) from the "Profile_Table" for a person in the openargs argument but you NEVER USE IT.

    So on the form "AddLog_Form", you need to have a hidden text box control bound to the "Profile_ID_SK" field.
    When the "AddLog_Form" opens, code in the on load event code takes the value in the openargs argument and puts it in the "Profile_ID_SK" text box.

    THEN you can save the record.



    PS... it looks like you might have problems with the delete log row code also. Are you wanting to delete ONE specific log record or ALL log records for a specific person??
    Thanks for your answer
    What do you mean i have a problem with the delete log row button?
    The idea is that it should delete the row that i select in the listbox named Log_Listbox
    And as far as i can tell it does that... or what am i missing (i bet i am missing stuff all the time)


    On the other matter.... I think i got it to work!

    I did this:
    In the form Profile_Form in the button "add log row" event on-click i added this code
    Code:
    DoCmd.OpenForm "AddLog_Form", , , "Profile_ID_SK = " & Me.txtProfile_ID, acFormAdd, , Me.txtProfile_ID
    On the startup event of the form called AddLog_Form i added this code
    Code:
        
    If Not IsNull(Me.OpenArgs) Then
            Me.txtProfile_ID_SK = Me.OpenArgs
    End If
    And now i think it works as i want it to work.
    However a follow-up question.
    How to eliminate the error messages that appear when someone click on the button and nothing is selected in the Profile listebox... i understand that i need something to tell that "if the txtprofile_id is empty or null then dont run the code".... is this right?



    Uploaded a new file with this working ad described.

  11. #11
    Behedwin is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    61
    Quote Originally Posted by June7 View Post
    Your code does not follow the referenced example. However, that example could use an adjustment.

    Need a record in Profile_Table first. If is a new record on Profile_Form, must be committed to table before opening the Log form.

    If Me.Dirty Then Me.Dirty = False
    If Not IsNull(Me.txtProfile_ID) Then DoCmd.OpenForm "AddLog_Form", , , , acFormAdd, , Me.txtProfile_ID
    End If

    Also need code behind the Log form.
    Thank you very much.
    You gave me the answer to my follow-up question that i wrote on my answer to ssanfu

    Now i dont get any error messages when randomly clicking the button

  12. #12
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    These two lines should be at the top of EVERY module
    Code:
    Option Compare Database
    Option Explicit

    You are pretty close - you should check to see if Me.txtProfile_ID has a value BEFORE "AddLog_Form" opens.... see the code.
    I modified the "AddLogRow_Button_Click" code as per June's suggestion in Post #8.
    Attached Files Attached Files

  13. #13
    Behedwin is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    61
    Quote Originally Posted by ssanfu View Post
    These two lines should be at the top of EVERY module
    Code:
    Option Compare Database
    Option Explicit

    You are pretty close - you should check to see if Me.txtProfile_ID has a value BEFORE "AddLog_Form" opens.... see the code.
    I modified the "AddLogRow_Button_Click" code as per June's suggestion in Post #8.

    Cool, ill be away today so i dont have much time. But im like a little child right now seeing things starting to work

    Could you explain why that code should be at the start of each module?

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    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
    Behedwin is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    61
    Yes, but i dont understand why... It says what the statement means... But things work without... So why is these statements good to use?

    What are the benefits.

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

Similar Threads

  1. Replies: 7
    Last Post: 07-24-2017, 11:47 PM
  2. Replies: 5
    Last Post: 11-26-2013, 11:11 PM
  3. Replies: 5
    Last Post: 08-12-2013, 12:53 AM
  4. Replies: 27
    Last Post: 08-14-2012, 09:05 AM
  5. Replies: 5
    Last Post: 05-14-2012, 02:01 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