473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Update or Insert Table from txtbox

Hello,
I've been working on taking data from a form and inserting it to a table if the record doesn't exist and updating it if the record does exists. All the data is in textboxes and a comboboxes. I have button that when clicked runs an sql statement in visual basic. When I click the button error '3129' appears stating that "Invalid SQL statement; Expected INSERT, DELETE, UPDATE ..." Please help.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command93_Click()
  2.  
  3. Dim strSQL As String
  4.  
  5. strSQL = "IF EXISTS ( SELECT col1FROM Table1 WHERE col1= " & Me.txtcol1& ") BEGIN"
  6.  
  7. 'Update Statment
  8. strSQL = strSQL & " Update Table1 SET col2 = '" & Me.txtcol2 & "' "
  9. strSQL = strSQL & "col3 = '" & Me.txtcol3 & "', "
  10. strSQL = strSQL & "col4 = '" & Me.cbocol4 & "', "
  11. strSQL = strSQL & "col5 = '" & Me.txtcol5 & "', "
  12. strSQL = strSQL & "col6 = '" & Me.txtcol6 & "', "
  13. strSQL = strSQL & "col7 = '" & Me.txtcol7 & "' "
  14. strSQL = strSQL & "WHERE col1= " & Me.txtcol1& ""
  15.  
  16. strSQL = strSQL & " Else "
  17. 'Insert Statement
  18. strSQL = strSQL & "INSERT INTO Table1 ( col1, col2, col3, col4, col5, col6, col7) "
  19. strSQL = strSQL & "Values ('" & Me.txtcol1 & "', "
  20. strSQL = strSQL & "'" & Me.txtcol2 & "', "
  21. strSQL = strSQL & "'" & Me.txtcol3& "', "
  22. strSQL = strSQL & "'" & Me.cbocol4 & "', "
  23. strSQL = strSQL & "'" & Me.txtcol5 & "', "
  24. strSQL = strSQL & "'" & Me.txtcol6 & "', "
  25. strSQL = strSQL & "'" & Me.txtcol7 & "') END"
  26.  
  27. If txtcol1 = "" Then
  28. MsgBox "Please Enter col1 Number"
  29. Else
  30. 'CurrentDb.Execute strSQL, dbFailOnError
  31. DoCmd.RunSQL strSQL, dbFailOnError
  32. End If
  33. End Sub
Sep 4 '08 #1
2 1627
NeoPa
32,556 Expert Mod 16PB
There is no "IF EXISTS" construct in Jet SQL.

I suggest you do the checking in VBA then create the SQL accordingly.
Sep 5 '08 #2
NeoPa
32,556 Expert Mod 16PB
Let me know if you need any help with that.

Concepts or coding :)

Welcome to Bytes!
Sep 5 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
2
by: Manish | last post by:
Hey folks I am having a weird problem in ASP .Net. My page is in C#. I have a datagrid, which populates based on selection in drop down box on ASP page. This datagrid has template textbox colum in...
4
by: Shane | last post by:
I would like to update Access by using a txtBox. Is it possible to update Access this way? Example: If I selected the txtBox and input a line of text I would like that line of text to be sent...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.