Results 1 to 15 of 15
  1. #1
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104

    Append Query (Maybe VBA?)

    I've searched through a lot of places, and found a few that are close, but I can't seem to find the correct solution for my problem (probably because I'm almost a complete VBA newbie, and even then not so well-versed in Access).

    Anyways, I'm trying to make a button on a form in Access 2007 (which serves at the input form - called Input_Screen - for a table called List) to append the data from List to a larger database called CHARGES.

    The fields in List are:


    SITE
    PDATE
    PAMOUNT
    DESC
    DESC1
    ACHGROUP
    Deduct/Add
    Initial

    The fields in CHARGES are:
    SITE
    PDATE
    PAMOUNT
    DESC
    DESC1
    ACHGROUP

    I only want to append the data for the fields that are currently in CHARGES from List (disregarding the Deduct/Add and Initial fields). I'm also trying to figure out a second button to clear the List table completely, so I can start over with a clean database (I'm generating reports and such off of it that will change, but I want to keep the DB CHARGES for sort of an archive, as well as a few more uses I have in mind.

    Any help is much appreciated... again, sorry that I'm so entirely helpless in VBA.

  2. #2
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You should not have two tables with such similar structures, it violates good database design practices. It sounds like you should be using a query to just show the fields and records you want from the charges table. Other than that, I do not quite understand what you are doing and what the tables actually represent in your data model. Could you explain the purpose of your application and how the charge table fits into that application?

  3. #3
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    The "CHARGES" database is actually imported from a dBase III File. I work for a company that uses an old, old, old customized program that utilizes that particular file for one big archive. I mimicked the database format on the new (List) database in Access, imported the CHARGES db, and I'm currently making something where they input on a form, it saves to a temp database (List), and they generate a report of the currently entered (in this case weekly) data. When the okay is given (previously this was split into 3 different reports and painstaking procedures entered in multiple places), I want to be able to dump the List database into the CHARGES main database (adding to it so the custom program can later read the data it needs). Then, I want to be able to hit another button and clear the List database (after the information has been moved over to the CHARGES db, of course), so it will be ready for the next week.

    Hope that helps.

  4. #4
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    To be honest, I still do not see the need for the list table. You can accomplish the same thing with just your charges table. If you want to go that route, I would add a simple yes/no field to the charges table and set its default property to yes/true. This way when anyone enters data, the record is flagged. You would then create a query that only selects records where the yes/no field=true and base your entry form on that. If no records are flagged as yes, the form opens up ready to enter new records.

    You can then base your report on a query that pulls only those flagged records. Once the OK is given, run an update query to change the yes/no field from yes to no. Your form will be "cleaned" of any records and ready for the next week.

    If you still want to pursue the two table approach, you would need to run an append query to select the records from the list table and add them to the charges table. The query would look like this:

    INSERT INTO CHARGES ( SITE, PDATE, PAMOUNT, [DESC], DESC1, ACHGROUP )
    SELECT List.SITE, List.PDATE, List.PAMOUNT, List.DESC, List.DESC1, List.ACHGROUP
    FROM List;


    Then you would need to run a delete query on the list table to remove the records; that query would look like this:

    DELETE *
    FROM List;


    I would be hesitant to put the button to run the queries on the same for where your users are entering data since they might click the button at the wrong time and mess up your process. I would run the queries directly or from a separate form that only you have access to.


    If you wanted to run the two queries automatically with the click of a button, the code would look something like this:


    Code:
    Private Sub Command0_Click()
    Dim myAppendQuery As String
    Dim myDeleteQuery As String
    myAppendQuery = "INSERT INTO CHARGES ( SITE, PDATE, PAMOUNT, [DESC], DESC1, ACHGROUP) "
    myAppendQuery = myAppendQuery & " SELECT List.SITE, List.PDATE, List.PAMOUNT, List.DESC, List.DESC1, List.ACHGROUP FROM List"
    myDeleteQuery = "DELETE *FROM List"
    
    CurrentDb.Execute myAppendQuery, dbFailOnError
    CurrentDb.Execute myDeleteQuery, dbFailOnError
    
    End Sub

  5. #5
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    I am inclined to agree that I'm not a fan of using the two seperate DB's, but I can't be sure what adding additional fields to the master DB will have on the older program that will later (after I export back to dBase III) read from it. Compatibility issues are going to be a mess themselves, without having to add extra fields to it. Again, I could be totally wrong (since I'm not experienced with databases), but it makes sense from a logical standpoint.

    I would be hesitant to put the button to run the queries on the same for where your users are entering data since they might click the button at the wrong time and mess up your process. I would run the queries directly or from a separate form that only you have access to.
    When would running the queries mess things up? I see the point of a different form, and I'll likely put that in, but is there any time I should be aware of that doing those particular operations might cause things to go haywire?

  6. #6
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Since you are just importing rather than linking to your dBaseIII table, adding a field in the Access table is not an issue. When you are ready to send the data back into dBaseIII then I would just use a query that does not include the additional field.

    Out of curiosity, how do you plan to get just the new data back into your dBaseIII system? If you try to append all records from your charges table then you will end up with duplicates in your dBaseIII table.

    I believe you might be able to use ODBC to connect Access directly to your dBaseIII tables (as linked tables). Access would then serve as a front end interface. Unfortunately, I do not have experience with connecting Access to a dBaseIII database. You might pose that question as a new thread on the forum. If you were able to use Access to directly connect to your dBaseIII tables, then perhaps I would reconsider the list table approach since you do not want to add any records to the dBaseIII backend before they are ok'd.

  7. #7
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    The code that you put in worked like a charm! Thank you very much!

    I was actually thinking about linking the table, since otherwise I'd have to add an import and export button, not to mention the hassle of moving the files over and overwriting them, etc.

    It's a dBase III file (at least I think it is; it's a FoxPro 2.6 database, and when I use the dBase III option on importing it works fine). The actual database is linking fine, I just want to be able to test it and make sure that I'm not going to mess anything up .

    Quote Originally Posted by jzwp11 View Post
    I believe you might be able to use ODBC to connect Access directly to your dBaseIII tables (as linked tables). Access would then serve as a front end interface. Unfortunately, I do not have experience with connecting Access to a dBaseIII database. You might pose that question as a new thread on the forum. If you were able to use Access to directly connect to your dBaseIII tables, then perhaps I would reconsider the list table approach since you do not want to add any records to the dBaseIII backend before they are ok'd.
    I just used access to link to the .dbf file (using import wizard) and it seems to be working perfectly fine at the moment.

  8. #8
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I just used access to link to the .dbf file (using import wizard) and it seems to be working perfectly fine at the moment.
    Just to clarify, importing and linking are two separate things. Importing brings the data into Access as a table. Any changes made to the data in that Access table are not made in the source table (table from which the information was imported). A linked table actually makes a connection between Access and the table in the other database. Therefore, when you make a change to the data via Access, you are actually making the change in the other database's table directly. Hopefully I have explained that clearly. Please let me know if you have questions.

  9. #9
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    You did, I was just a bit vague and confused myself. Would there be any problems you could think of offhand at linking the table (I went to the External Data tab, More, then dBase file and linked it) and using the append query that way?

    My main concern, though you might not know (I certainly don't until I test it), is that FoxPro 2.6 will go haywire at the data added from Access 2007 and not work with the master database (I've created copies to test on, but I figured I'd run that by you in the off chance you knew).

    I was working on it making a backup copy of the database each time as well, just in case. I wanted it to be structured like:

    CHARGES_7-21-2010.dbf

    I have the following code:

    Code:
    Dim fs As Object
    Dim oldPath As String, newPath As String
    oldPath = "C:\My Documents\dbf"
        ' Path for the original database
    newPath = "C\My Documents\dbf\backup"
        ' Path for backup of database
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.Copyfile oldPath & "\" & "CHARGES.dbf", newPath & "\" & "CHARGES_" & Date & ".dbf"
    ' Copying the database and adding _Date at the end of it
    Set fs = Nothing
    But its saying the
    Code:
    fs.Copyfile oldPath & "\" & "CHARGES.dbf", newPath & "\" & "CHARGES_" & Date & ".dbf"
    part isn't found.


    Another part that I feel compelled to add is that it (the master database that the FoxPro 2.6 program reads from, CHARGES) not only includes a .dbf file, but it also has a .idx file (which I assume has something to do with an index, but I have no idea). I suppose, now that I think on it, that it might be prudent to back that up as well.
    Last edited by justinwright; 07-21-2010 at 09:49 AM. Reason: Additional thoughts

  10. #10
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    Sorry for double post, but I changed the copying button around to the following:

    Code:
    Dim fs As Object
    Dim oldPath As String, newPath As String
    oldPath = "C:\My Documents\dbf"
        ' Path for the original database
    newPath = "C:\My Documents\dbf\backup"
        ' Path for backup of database
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.Copyfile oldPath & "\" & "CHARGES.DBF", newPath & "\" & "CHARGES.DBF"
    ' Copying the database and adding _Date at the end of it
    Set fs = Nothing
    It works just fine, but I wanted the _<date> at the end of the backup. Any ideas?

  11. #11
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    From what I briefly read about FoxPro here, it is mainly a language that has a database management system. I'm not sure what you are dealing with. Do you have a front end that was created in FoxPro and FoxPro is communication with a dBaseIII database backend or is everything contained within FoxPro? If you find that FoxPro is just a front end, I assume that you should be able to connect (link) to the dBaseIII back end directly without going through FoxPro. Your Access database will be your new front end.

    As to the error you received, it might not like the period shown in red in the folder name and you need a & not a comma

    fs.Copyfile oldPath & "\" & "CHARGES.DBF" & newPath & "\" & "CHARGES.DBF"

    adding the date should not be a problem

    fs.Copyfile oldPath & "\" & "CHARGES.DBF" & newPath & "\" & "CHARGES.DBF" & Format(Date, "mm-dd-yyyy")

  12. #12
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    Well, the actual code
    Code:
    fs.CopyFile oldPath & "\" & "CHARGES.DBF", newPath & "\" & "CHARGES_" & ".DBF"
    Works well, wouldn't putting the "&" instead of a "," there just make the source file that much longer? The newPath is where the backup will be going.

    I was mainly concerned with adding the current date at the end of the copied file. The
    Code:
    & Date &
    addition in between "CHARGES_" and ".DBF" makes it error out, though.

    And FoxPro is just the front end, from what I can tell. I'll still have to use FoxPro (since it does a lot more than what this Access database/form is for), but I was streamlining an exterior process that is tedious, to say the least.

  13. #13
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Oops, sorry I didn't realize that the comma was separating the current (old) location of the file, to the new location to which it will be copied. Is this what you were after?:

    fs.Copyfile oldPath & "\" & "CHARGES.DBF",newPath & "\" & "CHARGES_" & Format(Date, "mm-dd-yyyy") & ".DBF"

  14. #14
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    Wonderful! Worked perfectly, changed & & Format to & Format (took out the extra &). Thank you!

  15. #15
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I saw the extra & after I posted, went back and changed it, but apparently you saw the post before my edit came through.

    Glad it worked for you.

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

Similar Threads

  1. Append Query Error
    By lupis in forum Queries
    Replies: 1
    Last Post: 06-18-2010, 02:10 AM
  2. Append query won't append
    By yelkenli in forum Queries
    Replies: 5
    Last Post: 02-12-2010, 11:19 AM
  3. Append Query
    By aabh in forum Queries
    Replies: 6
    Last Post: 02-02-2010, 04:26 PM
  4. Append Query
    By cotri in forum Queries
    Replies: 1
    Last Post: 01-28-2010, 08:52 PM
  5. append query error
    By shashigk in forum Queries
    Replies: 2
    Last Post: 09-22-2009, 07:17 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