Click here to Skip to main content
15,915,042 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all,

when i fill all textboxes and then click on save button data inserted successfully. but when i leave one or more textbox blank then "Syntax error in Insert Into Statement". how can i solve?.
Table (Ms-Access).

Code

SQL
c.Exec("insert into donor(id,name,address,con1,con2,dob,fdd,amt)values(" & TextBox1.Text & ",'" & ComboBox1.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & DateTimePicker1.Value.Date & "','" & TextBox6.Text & "'," & TextBox2.Text & ")")
        c.StdMsg("Record Saved successfully.")


Posted
Updated 24-Mar-11 18:40pm
v6
Comments
Sergey Alexandrovich Kryukov 25-Mar-11 0:12am    
Makes no sense unless you show your code.
--SA

If you would had debugged, it would had taken hardly 5 min.

Example:
Assume I do not fill Textbox1, Textbox5 & Textbox2.

My Insert statement formed is:
SQL
Insert into donor(id,name,address,con1,con2,dob,fdd,amt)values(,TextBox3_Text,TextBox4_Text,, DateTimePicker1_Value_Date,,)


Saw anything? Just run the above query directly on database and see for yourself.
 
Share this answer
 
Comments
Prerak Patel 25-Mar-11 1:25am    
perfect. 5
I believe you will find parameters very useful. Few examples:
- http://support.microsoft.com/kb/181782[^]
- http://support.microsoft.com/kb/142938[^]
 
Share this answer
 
The simplest way to get around this problem would be to put a validation block before the Insert into statement where you initialise a value to the text items if they are not provided and then continue with the insert into statement.

Secondly i would suggest you make use of Parameters option as Mika has mentioned.
Take a look at the below link

MS Access Parameters[^]
 
Share this answer
 
Try to debug your code in such circumstances. Put a debug point in your code, copy the query and run it in Sql server. More often than not, you will be able to find a solution to such errors.
 
Share this answer
 

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