none
Formatted data is not saved correctly

    Question

  • Using MS Access back end. The ID field is a text field of 4 characters. 4 characters are required, and so if a product ID has less than 4 characters, the ID field gets padded to make 4 characters:

    Private Sub IDTextBox_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles IDTextBox.Validating
        While Len(IDTextBox.Text) < 4
            IDTextBox.Text = CStr("0" + CStr(IDTextBox.Text))
        End While
    End Sub
    When the user clicks the Save button to save the data, the leading zeros are dropped.
    Private Sub SaveData()
        Me.Validate()
        Me.TblProductsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.StewardDataSet)
    End Sub
    Why does this happen? And how do I overcome this?
    Sunday, January 26, 2014 2:28 PM

Answers

  • Conceptually speaking if my code and your code show four characters but only three are placed into the field there are only a few things that will do this.

    1. Your field is less than four characters in length (yes I know you indicated it's 4 but this is one fact)
    2. There is a parameter that has the length property set, usually in the constructor see here.
    3. An event is raised that manipulates the data.

    Now you may believe none of the above are affecting your operation but one of them is.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Sunday, January 26, 2014 9:16 PM

All replies

  • Hello,

    Try using the following

    IDTextBox.Text = CInt(IDTextBox.Text).ToString("D4")


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Sunday, January 26, 2014 5:11 PM
  • Thanks, but that doesn't help. Both my code and yours causes the IDTextBox to display the ID as 4 characters; however, both my code and yours revert back to the 3 characters entered instead of keeping the leading 0 when the save button is pressed. How do I keep the 4 characters?
    Sunday, January 26, 2014 7:33 PM
  • Conceptually speaking if my code and your code show four characters but only three are placed into the field there are only a few things that will do this.

    1. Your field is less than four characters in length (yes I know you indicated it's 4 but this is one fact)
    2. There is a parameter that has the length property set, usually in the constructor see here.
    3. An event is raised that manipulates the data.

    Now you may believe none of the above are affecting your operation but one of them is.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Sunday, January 26, 2014 9:16 PM