Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Lost_focus and save button.

Lost_focus and save button.

Scheduled Pinned Locked Moved Visual Basic
databasedebuggingquestion
10 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lemarshall
    wrote on last edited by
    #1

    I have a total of 65 text boxes which the user can fill in. They are 13 down and 5 across. Each row is related to a particular material (which can change daily -see earlier posts) the columns are for quality of material. In Debug mode stepping through the code this is what happens: If I pull up the form and enter data into one textbox and then click on the save button the program goes through the save routine, but there is nothing to save and then it hits the Lost_focus event of the text box and runs through and saves the data to the collection class but doesn't save the information. If I enter one text box and then tab to another the Lost_focus of the first textbox saves the data to the collection class and then click on the Save button the program saves the info to the tables properly and then runs the Lost_focus event of the second text box. I don't remember this happening in VB6 so I am sure it has to do with the new form collection class, etc. I am confused and wonder now what is the proper way to proceed. I need to be able to save for each form anywhere from 1 to 65 types of material on each form. The text_changed proeprty won't work as it fires on each digit being entered- 123 causes the event to fire 3 times. Here is the code for the SaveMaterial function from the save button, the StoreMaterial2COL function and the first couple of lost_focus Subs:

    Private Function SaveMaterial() As Boolean
    
        Try
            ' The index of a zero-based collection is Count property minus 1.
            Dim iCnt As Integer
            iCnt = myMaterialsCollection.Count
            iCnt = iCnt - 1
    
            With myMaterialsCollection
                For Each aItem In myMaterialsCollection
                    If iCnt = -1 Then
                        Exit For
                    End If
                    \_gblString = CreateGUIDString()
    
                    gblConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\JWI\\Data\\JWI.mdb;Jet OLEDB:Database Password=jWi;")
    
                    strData = "INSERT INTO LOLItems (LOLItemsID, LOLIFormID, Type, TextBox, MaterialsID, Units, DateCreated )" \_
                    & " VALUES ('" & \_gblString & "',  " \_
                    & "'" & \_frmGuid & "', '" \_
                    & 1 & "',' " \_
                    & aItem.txtBoxName & "',' " \_
                    & aItem.MaterialID & "',' " \_
                    & aItem.jnts & "',' " \_
    
    D L H 3 Replies Last reply
    0
    • L lemarshall

      I have a total of 65 text boxes which the user can fill in. They are 13 down and 5 across. Each row is related to a particular material (which can change daily -see earlier posts) the columns are for quality of material. In Debug mode stepping through the code this is what happens: If I pull up the form and enter data into one textbox and then click on the save button the program goes through the save routine, but there is nothing to save and then it hits the Lost_focus event of the text box and runs through and saves the data to the collection class but doesn't save the information. If I enter one text box and then tab to another the Lost_focus of the first textbox saves the data to the collection class and then click on the Save button the program saves the info to the tables properly and then runs the Lost_focus event of the second text box. I don't remember this happening in VB6 so I am sure it has to do with the new form collection class, etc. I am confused and wonder now what is the proper way to proceed. I need to be able to save for each form anywhere from 1 to 65 types of material on each form. The text_changed proeprty won't work as it fires on each digit being entered- 123 causes the event to fire 3 times. Here is the code for the SaveMaterial function from the save button, the StoreMaterial2COL function and the first couple of lost_focus Subs:

      Private Function SaveMaterial() As Boolean
      
          Try
              ' The index of a zero-based collection is Count property minus 1.
              Dim iCnt As Integer
              iCnt = myMaterialsCollection.Count
              iCnt = iCnt - 1
      
              With myMaterialsCollection
                  For Each aItem In myMaterialsCollection
                      If iCnt = -1 Then
                          Exit For
                      End If
                      \_gblString = CreateGUIDString()
      
                      gblConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\JWI\\Data\\JWI.mdb;Jet OLEDB:Database Password=jWi;")
      
                      strData = "INSERT INTO LOLItems (LOLItemsID, LOLIFormID, Type, TextBox, MaterialsID, Units, DateCreated )" \_
                      & " VALUES ('" & \_gblString & "',  " \_
                      & "'" & \_frmGuid & "', '" \_
                      & 1 & "',' " \_
                      & aItem.txtBoxName & "',' " \_
                      & aItem.MaterialID & "',' " \_
                      & aItem.jnts & "',' " \_
      
      D Offline
      D Offline
      Dr Walt Fair PE
      wrote on last edited by
      #2

      Have you tried using the control's Validating or Validated events?

      CQ de W5ALT

      Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

      L 1 Reply Last reply
      0
      • L lemarshall

        I have a total of 65 text boxes which the user can fill in. They are 13 down and 5 across. Each row is related to a particular material (which can change daily -see earlier posts) the columns are for quality of material. In Debug mode stepping through the code this is what happens: If I pull up the form and enter data into one textbox and then click on the save button the program goes through the save routine, but there is nothing to save and then it hits the Lost_focus event of the text box and runs through and saves the data to the collection class but doesn't save the information. If I enter one text box and then tab to another the Lost_focus of the first textbox saves the data to the collection class and then click on the Save button the program saves the info to the tables properly and then runs the Lost_focus event of the second text box. I don't remember this happening in VB6 so I am sure it has to do with the new form collection class, etc. I am confused and wonder now what is the proper way to proceed. I need to be able to save for each form anywhere from 1 to 65 types of material on each form. The text_changed proeprty won't work as it fires on each digit being entered- 123 causes the event to fire 3 times. Here is the code for the SaveMaterial function from the save button, the StoreMaterial2COL function and the first couple of lost_focus Subs:

        Private Function SaveMaterial() As Boolean
        
            Try
                ' The index of a zero-based collection is Count property minus 1.
                Dim iCnt As Integer
                iCnt = myMaterialsCollection.Count
                iCnt = iCnt - 1
        
                With myMaterialsCollection
                    For Each aItem In myMaterialsCollection
                        If iCnt = -1 Then
                            Exit For
                        End If
                        \_gblString = CreateGUIDString()
        
                        gblConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\JWI\\Data\\JWI.mdb;Jet OLEDB:Database Password=jWi;")
        
                        strData = "INSERT INTO LOLItems (LOLItemsID, LOLIFormID, Type, TextBox, MaterialsID, Units, DateCreated )" \_
                        & " VALUES ('" & \_gblString & "',  " \_
                        & "'" & \_frmGuid & "', '" \_
                        & 1 & "',' " \_
                        & aItem.txtBoxName & "',' " \_
                        & aItem.MaterialID & "',' " \_
                        & aItem.jnts & "',' " \_
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I find it's not a good idea to bug the user with a save every time a control looses focus. In fact, editing of the user's inputs in all controls should not happen until a save is triggered. In my opinion, a save should happen when one of two events happens ... (1) a Save button is clicked by the user (hence they expect it to happen), or (2) the FormClosing event fires. And the latter should trigger a save only if the user made at least one change in any of the controls which should cause a message box to display to ask the user if the changes are to be saved or not. In other words, the user should be alerted that they are about to loose changes; but, should have the option to discard or save the changes. Hope all that makes sense. There's an article on this topic that you may find interesting. Click here to read it.

        L 1 Reply Last reply
        0
        • D Dr Walt Fair PE

          Have you tried using the control's Validating or Validated events?

          CQ de W5ALT

          Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

          L Offline
          L Offline
          lemarshall
          wrote on last edited by
          #4

          Not sure about those events but will look into them. Thanks Lary

          1 Reply Last reply
          0
          • L Lost User

            I find it's not a good idea to bug the user with a save every time a control looses focus. In fact, editing of the user's inputs in all controls should not happen until a save is triggered. In my opinion, a save should happen when one of two events happens ... (1) a Save button is clicked by the user (hence they expect it to happen), or (2) the FormClosing event fires. And the latter should trigger a save only if the user made at least one change in any of the controls which should cause a message box to display to ask the user if the changes are to be saved or not. In other words, the user should be alerted that they are about to loose changes; but, should have the option to discard or save the changes. Hope all that makes sense. There's an article on this topic that you may find interesting. Click here to read it.

            L Offline
            L Offline
            lemarshall
            wrote on last edited by
            #5

            The Save works only when they click on the Save button to cycle through the Material Collection class. When the control loses focus then it STORES the necessary info into the Material collection class. I figured this would be easier than cycling through each text box when the Save button was clicked and then doing something like : if len(textbox.text)> 0 then Store necessary info to table end if and then to the next textbox and so on. My problems seems to be that the events don't fire in the sequence that I'm expecting. I can enter data into 2 or 15 text boxes and when I click on Save it always seems to handle the number of items in the collection and THEN go to at least one more textbox even though there is no data stored in it. If I enter data into just one text box then the Save routine runs before the STORE routine for the textbox and then that store routine runs and then the next textbox lost focus runs also. Not sure why? Thanks, Larry

            L 1 Reply Last reply
            0
            • L lemarshall

              The Save works only when they click on the Save button to cycle through the Material Collection class. When the control loses focus then it STORES the necessary info into the Material collection class. I figured this would be easier than cycling through each text box when the Save button was clicked and then doing something like : if len(textbox.text)> 0 then Store necessary info to table end if and then to the next textbox and so on. My problems seems to be that the events don't fire in the sequence that I'm expecting. I can enter data into 2 or 15 text boxes and when I click on Save it always seems to handle the number of items in the collection and THEN go to at least one more textbox even though there is no data stored in it. If I enter data into just one text box then the Save routine runs before the STORE routine for the textbox and then that store routine runs and then the next textbox lost focus runs also. Not sure why? Thanks, Larry

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I'm not following for some reason. That sounds awfully convoluted. Guess I'd have to look at the code.

              L 1 Reply Last reply
              0
              • L Lost User

                I'm not following for some reason. That sounds awfully convoluted. Guess I'd have to look at the code.

                L Offline
                L Offline
                lemarshall
                wrote on last edited by
                #7

                Your right it is convoluted. Let me lay out part of the design specs. We are dealing with scrap material commodity selling. There will be 30-40 buyers/sellers working with the form and can make 20-30 buys/sells per hour. What makes it difficult is they want to top selling items from yesterday to show as the first item on the form today. In other words, if 3/8 inch rod traded more yesterday then today it needs to be at the top of the list. If today 5/8 inch rods sold the most then tomorow they need to be at the top of the list. So each day, the order in which the items show can change. I can calculate total number of sales of each item and then fill the items into a label which has the name of the item and then in the labels .tag property I store the GUID for the item. Then I name each of the textboxes acrosss the screen based upon the item name. Each item has 5 grades from Junk to New and it is possible that on one order the user can sell 20 joints of 3/8 inch rod Junk, 15 joints of 3/8 inch rod Good (B2) and 10 rods of 3/8 inch rod New. That is why I thought the best reason is to store each entry into a text box on the Lost_focus event into a Material collection class and then save the items to the tables from that class when they click on the Save button which will also close the form and allow them to go on to the next order. Anything anyone can come up with to make it easier will be greatly appreciated. Larry

                L 1 Reply Last reply
                0
                • L lemarshall

                  I have a total of 65 text boxes which the user can fill in. They are 13 down and 5 across. Each row is related to a particular material (which can change daily -see earlier posts) the columns are for quality of material. In Debug mode stepping through the code this is what happens: If I pull up the form and enter data into one textbox and then click on the save button the program goes through the save routine, but there is nothing to save and then it hits the Lost_focus event of the text box and runs through and saves the data to the collection class but doesn't save the information. If I enter one text box and then tab to another the Lost_focus of the first textbox saves the data to the collection class and then click on the Save button the program saves the info to the tables properly and then runs the Lost_focus event of the second text box. I don't remember this happening in VB6 so I am sure it has to do with the new form collection class, etc. I am confused and wonder now what is the proper way to proceed. I need to be able to save for each form anywhere from 1 to 65 types of material on each form. The text_changed proeprty won't work as it fires on each digit being entered- 123 causes the event to fire 3 times. Here is the code for the SaveMaterial function from the save button, the StoreMaterial2COL function and the first couple of lost_focus Subs:

                  Private Function SaveMaterial() As Boolean
                  
                      Try
                          ' The index of a zero-based collection is Count property minus 1.
                          Dim iCnt As Integer
                          iCnt = myMaterialsCollection.Count
                          iCnt = iCnt - 1
                  
                          With myMaterialsCollection
                              For Each aItem In myMaterialsCollection
                                  If iCnt = -1 Then
                                      Exit For
                                  End If
                                  \_gblString = CreateGUIDString()
                  
                                  gblConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\JWI\\Data\\JWI.mdb;Jet OLEDB:Database Password=jWi;")
                  
                                  strData = "INSERT INTO LOLItems (LOLItemsID, LOLIFormID, Type, TextBox, MaterialsID, Units, DateCreated )" \_
                                  & " VALUES ('" & \_gblString & "',  " \_
                                  & "'" & \_frmGuid & "', '" \_
                                  & 1 & "',' " \_
                                  & aItem.txtBoxName & "',' " \_
                                  & aItem.MaterialID & "',' " \_
                                  & aItem.jnts & "',' " \_
                  
                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #8

                  From your description of your form it seems to me that you might be better served by designing a user control that contains the 5 text boxes, laid out as you need them, together with any labels, if any. Then you only need to add 13 of these user controls to something like a scroll box on your form. If nothing else it would make your code much less complex and therefore easier to read/maintain. Fortunately those nice folks at M$ must have been thinking of you and your problem they have released Microsoft Visual Basic Power Packs 3.0[^] which contains a new control, for VB.NET anyway (I believe a similar control was available for VB 6), called the DataRepeater. The download from the above link contains full source code and Help files with some samples. For a quick 'HowTo' take a look at DataRepeater Control For Windows Forms[^]. I have just noticed from that link that if you have installed VS2008 SP1, you should already have this control. If you have the time, I really recommend that you, at least, take a look at the 'HowTo' as I really believe that it will help with your current project. Good luck anyway. :)

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

                  1 Reply Last reply
                  0
                  • L lemarshall

                    Your right it is convoluted. Let me lay out part of the design specs. We are dealing with scrap material commodity selling. There will be 30-40 buyers/sellers working with the form and can make 20-30 buys/sells per hour. What makes it difficult is they want to top selling items from yesterday to show as the first item on the form today. In other words, if 3/8 inch rod traded more yesterday then today it needs to be at the top of the list. If today 5/8 inch rods sold the most then tomorow they need to be at the top of the list. So each day, the order in which the items show can change. I can calculate total number of sales of each item and then fill the items into a label which has the name of the item and then in the labels .tag property I store the GUID for the item. Then I name each of the textboxes acrosss the screen based upon the item name. Each item has 5 grades from Junk to New and it is possible that on one order the user can sell 20 joints of 3/8 inch rod Junk, 15 joints of 3/8 inch rod Good (B2) and 10 rods of 3/8 inch rod New. That is why I thought the best reason is to store each entry into a text box on the Lost_focus event into a Material collection class and then save the items to the tables from that class when they click on the Save button which will also close the form and allow them to go on to the next order. Anything anyone can come up with to make it easier will be greatly appreciated. Larry

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    I'm not seeing the need to store the data in both a collection and in the textboxes. Why can't the data reside just in the text boxes until the Save button is clicked? Then there would be no background overhead trying to duplicate the data from the textboxes in a collection class. It sounds to me like the simpler, and possibly more robust approach would be to remove the LostFocus events entirely. When the Save button is clicked, instantiate the collection, transfer the data from the textboxes to the collection, and then store the data in the database.

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      I'm not seeing the need to store the data in both a collection and in the textboxes. Why can't the data reside just in the text boxes until the Save button is clicked? Then there would be no background overhead trying to duplicate the data from the textboxes in a collection class. It sounds to me like the simpler, and possibly more robust approach would be to remove the LostFocus events entirely. When the Save button is clicked, instantiate the collection, transfer the data from the textboxes to the collection, and then store the data in the database.

                      L Offline
                      L Offline
                      lemarshall
                      wrote on last edited by
                      #10

                      Right, but I initially thought that I would have to cycle through EVERY textbox and if there was a value in it then store the data directly to the table then. I would be able to pick up the MaterialID from the GUID stored in the label for the row. I just thought that would be more time consuming and convoluted than this way. But I am open for suggestions. There are a lot more textboxes on the form than just the 65 for the materials so I have to be able to prevent any others from being searched. I beleive that can be done by cycling through the textboxes on tabLOLfrm.TabPage1 and tabLOLfrm.TabPage2 and tabLOLfrm.TabPage3. I beleive that the textboxes on the container are not recognized by the form so that might be one way. I remember that VB6 had the control array feature which was much easier to utilize. I have seen a number of attempts to create a control array in VB.Net but don't know exactly how to make a simple easy way to work. I've tried about 3 diferent ways from several different sources. Again, I appreciate your input and insight so far, any further assistance is greatly apreciated. I'm going to start working and this suggestion tonight and see what I can accomplish. Larry

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups