+ Reply to Thread
Results 1 to 20 of 20

Retrieve and amend records from database

  1. #1
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Retrieve and amend records from database

    I am a building a database where each row represents a record. Each record has a unique identifier number.

    In worksheet "Record Search and Amend", I have recorded a macro to look up the data.

    The problem is once the record is retrieved I need to be able to amend the record and save it back into the database in the right row. This could be another button to initiate this action.

    Could anyone help me with the VBA code? Spreadsheet attached.

    Thanks,

    Dave32
    Attached Files Attached Files

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You don't need a macro to get the data, just place the formulas in the cells. there's no point running code for the sake of it.

    To amend the record use the VBA Find method to look for the ID number & then copy the amendments with code adapted from the code that posts new records.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Retreive and AMEND records from database

    Roy

    Thanks for your answer. I used a Macro here because when the user amends a record, new data is entered in the cell and then the formula disappears. The user goes to look up a new record, it does not work as one of the Vlookup statements will be missing! I am trying to keep formulae out of the forms.

    I don't know how to use the FIND function in VBA or amend the code so that when a record is found the record is saved again in the right place. I am new to VBA and even with the help of a book can't work out how to do everything.. Can you help?

    Thanks,

    Dave32
    Last edited by Dave32; 04-14-2008 at 06:34 AM.

  4. #4
    Forum Contributor
    Join Date
    03-25-2008
    MS-Off Ver
    Excel, Outlook, Word 2007/2003
    Posts
    245
    On the tab 'Record Search & Amend' I would add the keyword 'False' to your vlookup formula. Because when no value is found, there will be an error.

    Instead of those vlookup formula, use the macrobutton to get the number you fill in and display the values of the tables.

    When changing those retrieved values, you can use the worksheet_change event to update the values, based on the number.

    Charlize

  5. #5
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Retrieve and amend records from a database

    Charlize,

    Thanks for your reply. Yes, I can add FALSE to the Vlookup statement, I have just amended the formula

    =IFERROR(VLOOKUP(B3,Database!A2:Z20,2,FALSE),"Record Not found")

    I am sorry I could follow the rest of your advice. I am new to VBA and don't understand all the functions yet. Could you please amend my code in the spreadsheet.

    The spreadsheet is attached.

    Thanks,

    Dave32
    Attached Files Attached Files
    Last edited by Dave32; 04-14-2008 at 06:18 AM.

  6. #6
    Forum Contributor
    Join Date
    03-25-2008
    MS-Off Ver
    Excel, Outlook, Word 2007/2003
    Posts
    245
    Changed coding for the macrobutton. Will do it for a couple of items. Now you can add a worksheet_change event on this sheet and look for the number in B3 and change the value in database that you've just changed in a cell. First check if number is valid before changing.
    Please Login or Register  to view this content.
    Charlize

  7. #7
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Amend records retrieved from a database

    Charlize,

    The code works well. However, I still need help on the code for the worksheet change event. Roy suggested amend the VBA code that I am using in the tab "DataEntry1". I don't know how to do this though.

    Help!

    Dave32
    Attached Files Attached Files
    Last edited by Dave32; 04-14-2008 at 07:31 AM. Reason: Forgot attachment

  8. #8
    Forum Contributor
    Join Date
    03-25-2008
    MS-Off Ver
    Excel, Outlook, Word 2007/2003
    Posts
    245
    This is a worksheet_change event of the tab 'Record Search & Amend'.
    Please Login or Register  to view this content.
    The code for fetching a record should be replaced with this one. It disables the application event on fetching an reenables it after the fetching. This is to avoid unnecessary changes to your database.
    Please Login or Register  to view this content.
    And there is a problem with the find part. XLValues will not work. You fill in 13 but 137 is needed. So you'll have to alter the XLValues part to something else. Found it, define the lookat part.

    Charlize
    Last edited by Charlize; 04-14-2008 at 09:26 AM. Reason: Update coding regarding the lookup for whole ID no.

  9. #9
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Retrieve and amend records from database

    Charlize,

    OK, I can now retrieve a record. But, I want the user to be able to change the record an save it into the same place in the database.

    Lets take record 134, the user looks up the record and then changes the country field to DE.

    I have created another button "Save amended Record"

    Could you tell how I can save an amended record to the right place in the database.

    New file attached.

    Thanks,

    Dave32
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    03-25-2008
    MS-Off Ver
    Excel, Outlook, Word 2007/2003
    Posts
    245
    Quote Originally Posted by Dave32
    Charlize,

    OK, I can now retrieve a record. But, I want the user to be able to change the record an save it into the same place in the database.

    Lets take record 134, the user looks up the record and then changes the country field to DE.

    I have created another button "Save amended Record"

    Could you tell how I can save an amended record to the right place in the database.

    New file attached.

    Thanks,

    Dave32
    See my previous post.

  11. #11
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Retrieve and amend records from database

    Roy,

    Thanks for your new code. However, I got an error:

    Compile Error "End with without with"

    I just changed 1 line: Sorry I don't know how to post code correctly, I am looking at the guideline and rule now

    Please Login or Register  to view this content.
    Last edited by royUK; 04-14-2008 at 10:34 AM.

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Ignore the previous code. Add this to one module

    Please Login or Register  to view this content.
    You need to go through your code and tidy it up before you do any thing else.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Retrieve and amend records from database

    Roy,

    Thanks for the new code. It works fine, but I could not assign the new entry code to the "Save New Record to database" button on the dataentry1 tab.

    This is probably now because my code is messy.

    On the dataentry1 tab there should be 3 Macros:

    Clear Form code
    Save new record code
    Save to autofillfrom2 code

    On the Record & Search tab there should be 3 Macros or code

    Record search code
    Save amended record code
    Clear form code.

    Can you help me clear this mess up, I guess I have tried code from two different people now and it just got messy.

    Thanks for your help.

    Dave32 - new sheet attached
    Attached Files Attached Files

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I will have a look later, if I can tonight but I have a long drive home now.

  15. #15
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I've tidied this up, all the code is now in mMain and assigned to the buttons. i suggest that you stick to either Forms buttons or Control ToolBox buttons. I have also removed empty modules & userform.

    I suggest that you take some time to understand what your code is doing.
    Last edited by royUK; 04-15-2008 at 03:22 AM.

  16. #16
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Retrieve and amend records from database

    Roy,

    Thanks very much for your help. It is greatly appreciated.

    Just checked the file. sorry you attached the wrong one.



    Dave32
    Last edited by Dave32; 04-15-2008 at 03:20 AM.

  17. #17
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Retrieve and amend records from database

    Roy,

    Sorry you attached the wrong file. Please re-attach

    Dave32

  18. #18
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Sorry about that, it was a long day yesterday
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Smile Retrieve and amend records from database

    Roy,

    Thanks for the new file.

    Have a good day.

    Dave32

  20. #20
    Registered User
    Join Date
    03-12-2008
    Posts
    76

    Retrieve and amend records from database

    http://www.excelforum.com/showthread...02#post1908102

    Roy,

    Now I have the basics right in my spreadsheet.

    I have just discovered a big issue. I have too much data per record to store in Excel's 255 columns! Posted above.

    Each records refers to money spent on a marketing campaign, the problems is that each marketing can have several activities accross several countries.

    I need to store data about each activity in each country and the amount spent per month per country and per activity.

    Dave32

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1