Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130

    Command Button Usage Only

    Hello, I have a userform that simply adds records to the database. My concern is for a user that is not familiar with Access. They may not realize that 'Tab' is how it is entered. I could display a msgbox confirming the adding of the record, but I would rather just have a command button that adds the record to the table. I was wondering how I could make the commandbutton the ONLY way to insert a record.

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Here is one way.

    Have your userform bound to a temporary table (which is just a blank copy of your actual table). So as they are entering data, they are writing to this temp table.
    Then, have your command button do an Append Query, copying the record over from the temp table to the final table.
    You would want to include code clear out the temp table after every add or whenever they click new add (in case they start a record, stop, and then start another new record before clicking your command button).

  3. #3
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    That is a really good idea, thank you. Any idea how to write the code to copy it over? I have hardly any experience in VBA on Access so I am pretty poor at writing the code at the moment.

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You don't really need to know any code to copy it over.
    Just create an Append Query, that writes the records from your temporary table to your permanent table. Then, just have the command button run that Append Query.
    If you use the Wizard to create your Command Button, there is an option under Miscellaneous for "Run Query".

    The only VBA code you really need to know is to delete records out of your temporary table after processing. That code will look something like this:
    Code:
    DoCmd.SetWarnings False
    DoCmd.RunSQL "Delete [TempTableName].* From [TempTableName];"
    DoCmd.SetWarnings True
    The first line just suppresses the warning message that you are about to delete records, and the last line turns your warnings back on.

  5. #5
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    Ok thank you very much. I put that on the event AfterUpdate of the form. Will that suffice?
    I was going to put it on the commandbutton as well, but I do not know how to add code when the query is already being executed.

  6. #6
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Ok thank you very much. I put that on the event AfterUpdate of the form. Will that suffice?
    I am not sure the timing of that will be right. I think you want to put it in the command button.

    You can can create a macro to do all the steps you want. Create a new Macro, add the following 4 actions:
    - OpenQuery: select the Append Query name
    - SetWarnings: set to No
    - RunSQL: enter the SQL code from my last post above (starting with "SELECT..." but with no quotes).
    - SetWarnings: set to Yes

    Save the Macro.
    Now just have your Command Button run this Macro. You could also convert the Macro to Visual Basic (their is a utility on the Macro menu that does that), and attach the VBA code to your button.

    As a precaution, you may also want to add the "Delete" code to the On Load and the On Close events.

  7. #7
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    Where is this SELECT statement you are talking about?

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Sorry, I meant to say "DELETE", not "SELECT", i.e.
    Code:
    Delete [TempTableName].* From [TempTableName];

  9. #9
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    Hmmm it is not updating the information to the permanent table. Any idea why this is not working?
    Last edited by dylcon; 06-05-2013 at 07:43 AM. Reason: Fixed Error

  10. #10
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Temporarily comment out the DELETE line, and then try again.
    If it does not add the record, trying running your Append Query ("TempCustQuery") manually.
    Does it work or does that give you some sort of error?
    You may have something like a key field violation (or some other error) that is not allowing you to Append the record.

  11. #11
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    It is working fine when I run the Query manually, but when I click the command button, it displays the msgbox, but still does not update the tblCustomer for some reason.

    Code:
    Private Sub Command18_Click()    DoCmd.SetWarnings False
        DoCmd.OpenQuery "TempCustQuery", acViewNormal, acEdit
        'DoCmd.RunSQL "Delete [tblTemporaryCustomer].* From [tblTemporaryCustomer];"
        DoCmd.SetWarnings True
        MsgBox (Me.TempCustName & " has been entered as a new company.")
    
    
    End Sub

  12. #12
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I think we just need to refresh the Form so that the record is added to the Temp table.

    Try this:
    Code:
    Private Sub Command18_Click()    
        Me.Refresh
        DoCmd.SetWarnings False
        DoCmd.OpenQuery "TempCustQuery", acViewNormal, acEdit
        'DoCmd.RunSQL "Delete [tblTemporaryCustomer].* From [tblTemporaryCustomer];"
        DoCmd.SetWarnings True
        MsgBox (Me.TempCustName & " has been entered as a new company.")
    End Sub

  13. #13
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    So I am guessing that the .OpenQuery is just opening and not running the query so I copied that SQL from the query and now am intending to use:

    Code:
        DoCmd.RunSQL "INSERT INTO tblCustomer ( CustomerName, CustomerNotes )" SELECT tblTemporaryCustomer.TempCustName, tblTemporaryCustomer.TempCustNotes 
    FROM tblTemporaryCustomer;"
    but that is coming up as incorrect syntax. How do I enter this correctly as I am pretty sure this will solve the problem.

  14. #14
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    That is not necessary. Opening any Action query will run it.
    If you make the change I listed in my previous post, it should work.
    I recreated your scenario and tested it out myself and confirmed that it works when that new line is added.

  15. #15
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    Joe, my previous post was done before I saw your refreshing statement. That cleared up the problem. Thank you.

    Any chance you know how to make the textboxes of the form blank once the command button is entered as well?

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

Similar Threads

  1. Command button
    By jiawen in forum Access
    Replies: 3
    Last Post: 01-12-2013, 12:07 PM
  2. Button Command
    By JayX in forum Access
    Replies: 2
    Last Post: 12-15-2011, 12:33 PM
  3. Command Button
    By tia in forum Access
    Replies: 1
    Last Post: 11-20-2011, 11:47 AM
  4. Replies: 1
    Last Post: 07-27-2010, 02:27 PM
  5. Command Button Help!
    By arthura in forum Programming
    Replies: 3
    Last Post: 06-30-2009, 12:55 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