Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day ! i'm here again ;) i will just ask , how will i disable saving on my database if the textboxes and the comboboxes are empty . it's just that everytime i click the button 'save' it saves even the fields are empty . i already put msgbox on the code saying that 'pls fill out fields' but after that it'll save. all i want to do is, if i click save and the fields are empty it'll not save and pops up a msgbox saying 'plss fill out fields' and if i put data on fields it'll save now :)

here is the code:


VB
Dim command As New OleDbCommand("insert into confiscation(num,REGION,PENRO,CENRO,MONTHDATE,YEARDATE,CONVEYANCE,CONV_UNITOFMEASURE,CONV_EST_VALUE,CHAINSAW,CHAIN_UNITOFMEASURE,CHAIN_EST_VALUE,LOGS,LOGS_UNITOFMEASURE,LOGS_EST_VALUE,FLITCHES,FLITCH_UNITOFMEASURE,FLITCH_EST_VALUE,LUMBER,LUMB_UNITOFMEASURE,LUMB_EST_VALUE) values (@num,@REGION,@PENRO,@CENRO,@MONTHDATE,@YEARDATE,@CONVEYANCE,@CONV_UNITOFMEASURE,@CONV_EST_VALUE,@CHAINSAW,@CHAIN_UNITOFMEASURE,@CHAIN_EST_VALUE,@LOGS,@LOGS_UNITOFMEASURE,@LOGS_EST_VALUE,@FLITCHES,@FLITCH_UNITOFMEASURE,@FLITCH_EST_VALUE,@LUMBER,@LUMB_UNITOFMEASURE,@LUMB_EST_VALUE)")
      command.CommandType = CommandType.Text
      con.Open()

      If TextBox1.Text = "" And TextBox3.Text = "" _
           And TextBox5.Text = "" And TextBox7.Text = "" _
           And TextBox9.Text = "" Then
          MsgBox("cannot be saved, please fill the confiscated product")
      Else
          With command.Parameters
              .AddWithValue("@num,", a)
              .AddWithValue("@REGION,", ComboBox1.Text)
              .AddWithValue("@PENRO,", ComboBox2.Text)
              .AddWithValue("@CENRO,", ComboBox3.Text)
              .AddWithValue("@MONTHDATE,", ComboBox4.Text)
              .AddWithValue("@YEARDATE,", TextBox11.Text)
              .AddWithValue("@CONVEYANCE,", TextBox1.Text)
              .AddWithValue("@CONV_UNITOFMEASURE,", ComboBox5.Text)
              .AddWithValue("@CONV_EST_VALUE,", TextBox2.Text)
              .AddWithValue("@CHAINSAW,", TextBox3.Text)
              .AddWithValue("@CHAIN_UNITOFMEASURE,", ComboBox6.Text)
              .AddWithValue("@CHAIN_EST_VALUE,", TextBox4.Text)
              .AddWithValue("@LOGS,", TextBox5.Text)
              .AddWithValue("@LOGS_UNITOFMEASURE,", ComboBox7.Text)
              .AddWithValue("@LOGS_EST_VALUE,", TextBox6.Text)
              .AddWithValue("@FLITCHES,", TextBox7.Text)
              .AddWithValue("@FLITCH_UNITOFMEASURE,", ComboBox8.Text)
              .AddWithValue("@FLITCH_EST_VALUE,", TextBox8.Text)
              .AddWithValue("@LUMBER,", TextBox9.Text)
              .AddWithValue("@LUMB_UNITOFMEASURE,", ComboBox9.Text)
              .AddWithValue("@LUMB_EST_VALUE,", TextBox10.Text)

          End With

          command.Connection = con
          command.ExecuteNonQuery()
          con.Close()
          MsgBox("success")

          ComboBox1.SelectedIndex = -1
          ComboBox2.SelectedIndex = -1
          ComboBox3.SelectedIndex = -1
          ComboBox4.SelectedIndex = -1
          ComboBox5.SelectedIndex = -1
          ComboBox6.SelectedIndex = -1
          ComboBox7.SelectedIndex = -1
          ComboBox8.SelectedIndex = -1
          ComboBox9.SelectedIndex = -1
          TextBox1.Text = ""
          TextBox2.Text = ""
          TextBox3.Text = ""
          TextBox4.Text = ""
          TextBox5.Text = ""
          TextBox6.Text = ""
          TextBox7.Text = ""
          TextBox8.Text = ""
          TextBox9.Text = ""
          TextBox10.Text = ""
          TextBox11.Text = ""
          datagrid_add()
          count()
      End If
  End Sub




any answer may help so much :)
Thanks in advance,
Posted
Updated 23-Jun-13 16:16pm
v3
Comments
[no name] 23-Jun-13 20:52pm    
The problem is in your If statement and using And. All of the textboxes have to be empty before it will not save. If even one of your textboxes there have some text in it, the If statement is going to evaluate to false and the Else condition will be executed. Rewrite your If statement.
Johnny J. 24-Jun-13 5:34am    
If I can just make a suggestion: You shouldn't put answers that are actually solutions as a comment. That way, the OP cannot accept the solution, and the question cannot be marked as resolved (unless of course somebody else posts the correct answer as a solution)
gwiyomin15 23-Jun-13 22:15pm    
i already did :'( i put it on the top of the Dim but it doesn't work
*hopeless*

1 solution

Change your If statement as

VB
If TextBox1.Text = "" OR TextBox3.Text = "" _
             OR TextBox5.Text = "" OR TextBox7.Text = "" _
             OR TextBox9.Text = "" Then
            MsgBox("cannot be saved, please fill the confiscated product")


Best Regards.
 
Share this answer
 
Comments
gwiyomin15 24-Jun-13 21:09pm    
i've got the code :) and its working thanks . hmm but still there's a problem . i want to post the screen shot of my form so you could see what's wrong, but how will i do that ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900