+ Reply to Thread
Results 1 to 11 of 11

Userforms - Unable to get UPDATE button to work

  1. #1
    Registered User
    Join Date
    03-27-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Question Userforms - Unable to get UPDATE button to work

    Hi All,

    I have been searching all day to try and work out how to get my Update Command Button to update a record in my workbook.

    I have ADD, FIND, Next Recrod, LastRecord, Close and Clear cmdButtons and they all work fine.

    I am able locate my field using a comboBox. and then select find and it populated all of my other txtBoxes and cboBoxes but as soon as i make a change and select update it creates a new row at the bottom of by workbook.

    See code below. and help would be a massive help

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Userforms - Unable to get UPDATE button to work

    Hi,

    You are probably incrementing 'Currentrow' in a place where it should not be updating or getting the value from the wrong place.

    The value should come from the row number where you get the data that loads in to the UserForm.

    Another possibility is that you may have a conflict between local and module scope or global scope variables.

    If you are still having problems, you will get the best answer by uploading a sample workbook.
    Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

    Lewis

  3. #3
    Registered User
    Join Date
    03-27-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Userforms - Unable to get UPDATE button to work

    Hi Thanks for your reply,

    How do you get Currentrow to pick up the rownumber for the data that was loaded into the useform ?

    Unfortunatly due to ristrictions on my PC i am unable to upload anything

    Also i have no idea what you mean by local and module scope or global scope variables. < is this where i have 'Dim Currentrow As Long' above all of my code and then again in my code as in my original post?

    Thanks again for any help

  4. #4
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Userforms - Unable to get UPDATE button to work

    In your code you don't specifically have a sheet associated with Cells so your data will go to the sheet that is active when you click the button. So the first question is, is that the way you want it to work?


    If you go into the code and put the cursor anywhere in the title line.
    "Private Sub cmdUpdate_Click()" then press F9. The line should highlight in a reddish brown color with a large dot in the left margin of the same color. This is called a break point.

    Now run the form as you normally would. After clicking the button the code will stop at the break point. The first thing you can do is hover your mouse over the word Currentrow and you will get a popup telling you the value of Currentrow, you can do the same with things like
    "cboReleaseNumber.Text" to see what their values are also.


    "Cells" takes 2 arguments RowNumber,ColumnNumber, so whatever your value is for Currentrow will be the row the data is going to go into on the active sheet and the hard coded numbers you have in your code is the column number the data will go into.

    Is the value for currentrow what you expected, probably not since that seems to be your complaint. So the question we would have is what does your code look like that assigns a value to current row. Some where in your code is code that's assigning a value to current row, or you are entering it somewhere manually.
    Last edited by skywriter; 07-07-2015 at 11:29 AM.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Userforms - Unable to get UPDATE button to work

    Good advice from Sky. Here are a few more items that may help you.

    i have no idea what you mean by local and module scope or global scope variables. < is this where i have 'Dim Currentrow As Long' above all of my code and then again in my code?
    Yes. See the bottom of the following link for scope examples: http://www.functionx.com/vbaexcel/Lesson03.htm

    I am able locate my field using a comboBox. and then select find and it populated all of my other txtBoxes and cboBoxes but as soon as i make a change and select update it creates a new row at the bottom of by workbook.
    This is probably a clue to the problem, which may be caused by local vs global scope. The row after the last row got assigned to CurrentRow somewhere (probably when you did an Add). My speculation is that you have a local CurrentRow when you loaded the data to be edited. The dim CurrentRow statement in that Sub should probably be deleted. The CurrentRow should have been global with module scope (i.e. Private), defined at the top of the UserForm Module:
    Please Login or Register  to view this content.
    It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Please Login or Register  to view this content.
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    --------------------
    Additional debugging tips:
    a. Press 'F8' to single step (goes into subroutines and functions).
    b. Press SHIFT 'F8' to single step OVER subroutines and functions.
    c. Press CTRL 'F8' to stop at the line where the cursor is.
    d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
    e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
    output to the IMMEDIATE WINDOW.
    f. Select View > Locals to see all variables while debugging.
    g. To automatically set a BREAKPOINT at a certain location put in the line:
    'Debug.Assert False'
    h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
    if i >= 20 and xTV20 > 99.56 then
    Debug.Assert False
    endif
    i. A variable value will be displayed by putting the cursor over the variable name.

    Lewis

  6. #6
    Registered User
    Join Date
    03-27-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Userforms - Unable to get UPDATE button to work

    Thanks for all of your help, i still have not been able to work out what is wrong.

    Skywriter - i did the F9 and had alook to see what currentrow value is - and it currently is at row 84 (where as i want it to be the row where the record was located) i have narrowed it down to this being my problem.

    I have provided my entire code below - can someone tell me what i shoudl be defining my 'currentrow' as if not 'as long'

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Userforms - Unable to get UPDATE button to work

    Hi,

    Your issues were as I suspected. See the attached file, which contains updated code for almost everything. It should help you get started in the right direction.

    UserForm module:
    Please Login or Register  to view this content.
    Lewis
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    03-27-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Userforms - Unable to get UPDATE button to work

    Hi

    Thanks for helping with this,

    I have opened, tested and tried your code which works perfectly

    the issue that i have is that once i have amended it to fit my Userform (by adding all other txt & cbo boxes)

    I get the error 'variable not defined' for the below code - i have looked at your original and have copied and pasted the exact code but still get the error - not quite sure why it works in your document and not mine? (is it saying 'LabelRowNumber.Caption' is not defined)


    Please Login or Register  to view this content.
    Thanks

  9. #9
    Registered User
    Join Date
    03-27-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Userforms - Unable to get UPDATE button to work

    Hi - Just to add to the above - i have tried the rest of the code (in my userform) and the LabelRowNumber throws up an error all over.

    any help with this please?

    thanks

  10. #10
    Registered User
    Join Date
    03-27-2014
    Location
    Northampton, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Userforms - Unable to get UPDATE button to work

    another update - i have worked out the above error (i fogot to create the label and name it :|)

    i now have a different error. i cannot get my combo box to pick up the entries that have already been added to the spreadsheet

    the code has not been altered in anyway for the UserForm_Initialize (code that was kindly provided by Lewis)

    Thanks again people -

  11. #11
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Userforms - Unable to get UPDATE button to work

    Hi,

    I am guessing that somehow either UserForm_Initialize() is not being called properly or the variable iGblLastRow has an incorrect value.

    Try adding the line in red below in UserForm_Initialize().
    Please Login or Register  to view this content.
    The code will stop at that line. Since the ComboBox loads correctly initially:
    a. Press f5 when the debugger stops at that line the first time.
    b. Add a new item in the UserForm and select the Add button.
    c. The debugger should stop again inside UserForm Initialize().
    d. Verify that the value of iGblLastRow is correct (the value of the last row in Sheet1.
    e. Single step (f8) to verify that the ComboBox gets populated correctly.

    If everything works OK there is no problem. Otherwise, you have to tell me exactly which keystrokes you press in your file before the debug.assert change that causes the ComboBox to not load correctly.

    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Help with a Macro in VBA, Can't get the update button to work
    By AldoM in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-14-2013, 10:08 AM
  2. [SOLVED] Can I use userforms to find and update?
    By ricku87 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2013, 10:21 AM
  3. Replies: 2
    Last Post: 03-12-2008, 06:52 PM
  4. Replies: 0
    Last Post: 07-13-2006, 07:45 PM
  5. Unable to pass variable between Userforms
    By PGM in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-18-2006, 05:10 PM

Tags for this Thread

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