Results 1 to 5 of 5

Thread: Save empty fields to access database.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Save empty fields to access database.

    Hi!

    I have a code that saves data to access database. But I can't figure out how to make it work when a field is left blank.

    Code:
    MainForm.RihmatabelTableAdapter.Insert(Me.ComboProfiil.Text, Me.PikkusTextBox.Text, _
                                                       Me.OriginaaltoodeCheckBox.Checked.ToString, Me.OriginaalkoodTextBox.Text, _
                                                       Me.TootekoodTextBox.Text, Me.SobivusTextBox.Text, Me.ForgardenCheckBox.Checked.ToString, _
                                                       Me.ReginettCheckBox.Checked.ToString, Me.VärvusTextBox.Text, Me.HindTextBox.Text, _
                                                       Me.MärkmedTextBox.Text)
    
                MainForm.RihmatabelTableAdapter.Fill(MainForm.RihmadDataSet.Rihmatabel)
                MsgBox("Uus toode lisatud!", MsgBoxStyle.Information, "Rihmade Andmebaas")

    How should I edit my code so that it allows to save even when some of the textboxes are left empty?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Save empty fields to access database.

    It shouldn't make any difference if all fields are text. "" is as much a valid value as any other text. What problems have you experienced?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Re: Save empty fields to access database.

    Quote Originally Posted by dunfiddlin View Post
    It shouldn't make any difference if all fields are text. "" is as much a valid value as any other text. What problems have you experienced?
    Few fields are numeric fields so it gives me an error: Conversion from string "" to type Double is not valid.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Save empty fields to access database.

    Quote Originally Posted by lkallas View Post
    Few fields are numeric fields so it gives me an error: Conversion from string "" to type Double is not valid.
    There you go... You have to test for empty strings. If a string is empty and that field allows null, you substitute it with DBNull.Value; if the string is empty and your field doesn't allow null then you need to use the default value for that field, probably zero; otherwise, convert the string to a Double.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Save empty fields to access database.

    Ah, right. Well in that case you should use variables to pass values or use Numeric Up/Down controls (with a default value of 0) instead of text boxes for those fields that require numbers. To convert textbox entries to variables use ...

    Dim r As Double
    Double.TryParse(TextBox1.Text, r)

    r = 0 when the conversion fails for any reason (including empty text).
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Posting Permissions

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



Click Here to Expand Forum to Full Width