Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Into sql statement errors - combo box

Status
Not open for further replies.

vwalla

Technical User
Feb 25, 2004
22
US
I have a combo box connect to an acces db. If the user types something in the combo box that is not in the list, I wanted to have a msgbox popup & ask them if they want to add it.

Here is my code. I keep getting compile errors on the execute statement. I'm sure that I am doing this all wrong.
Code:
Private Sub cboCat_LostFocus()
  Dim db As New Connection
  Set db = New Connection
    If adoPrimaryRS.EOF Then
    'Value does exist in database, ask user if they would like to add value
    If MsgBox("Add Value", vbYesNo + vbQuestion, "Add Value") = vbYes Then
    adoPrimaryRS.Open "select * from cat WHERE category='" & Replace(cboCat.Text, "'", "''") & "'", db, adOpenStatic, adLockOptimistic
    db.Execute "INSERT INTO Cat VALUES('" & cboCat.Text & "'),,adCmdText"
   
    End If
End If

adoPrimaryRS.MoveNext

adoPrimaryRS.Close

End Sub]

Thanks,

Vwalla
 
The format of an INSERT statement is
Code:
INSERT INTO Cat [COLOR=red](FieldName1, FieldName2, ...)[/color]
                VALUES (Value1, Value2, ...)
You are missing the list of fields to be inserted.
 
I chnged it, but still get an error on ".execute" part. "Method or data member not found"

Thanks
 
Check to make sure that there are no typos in your field names. You can also try qualifying them with Square brackets.
Code:
INSERT INTO [Cat] ([FieldName1],[FieldName2],...)
                VALUES (Value1,Value2, ...)


zemp
 
Is my connection info correct? Am I going about this properly from a coding standpoint?

Thanks,

Vwalla
 
Here is something to try, quotes may be in the wrong place.
Code:
db.Execute "INSERT INTO Cat VALUES('" & cboCat.Text & "')[COLOR=red]"[/color],,adCmdText


zemp
 
That is what I have. (Please look at the top)
 
No you have this,
Code:
db.Execute "INSERT INTO Cat VALUES('" & cboCat.Text & "'),,adCmdText[COLOR=red]"[/color]
Note the location of the red quote mark.


zemp
 
Oh...I will change & let you know. Would this cause the ".exectue" stmt to error?

Thanks,

Vwalla
 
That was it. Thanks zemp & all others that helped me on this PITA.

Here is the full code that works:

Code:
Private Sub cboCat_LostFocus()
  Dim db As New Connection
  Set db = New Connection
  
  db.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Owner\My Documents\test.mdb;"

    Set addComboRS = New Recordset
    addComboRS.Open "select * from cat WHERE cat='" & Replace(cboCat.Text, "'", "''") & "'", db, adOpenStatic, adLockOptimistic


    If addComboRS.EOF Then
    db.CursorLocation = adUseClient
    db.Execute "INSERT INTO Cat VALUES('" & cboCat.Text & "')", , adCmdText
   
    End If

addComboRS.Close

End Sub
 
I just ran into something else. Letters & number work, but if I type "Wendy's" instead of "Wendys" I get the following error:

I get a syntax error - missing operator. Apparently it has something to do with the apostrophe.

Thanks
 

Use the same Replace function you have in your SELECT statement in the INSERT statement.

jj
 
Will this show as "Wendy''s" in my db or when I print out a list of records?

Thanks,

Vwalla
 

Actually, I don't think you should even have the REPLACE function in the SELECT statement but you do need it in the INSERT. No, it will not show the extra apostrophe in the database or your reports.
 
IMHO you should never use the Replace function in conjunction with saving SQL Server data. Use ADO parameters, it allows the data to remain as is and prevents SQL Injection attacks. faq709-1526

zemp
 
Ok. I will try later this evening & post results. Got busy here at my other job.

Thanks,

Vwalla
 
That worked, but I had to leave the replace stmt on the SQL also.

Zemp, this is for personal use at my house. Would SQL injection be a concern?

Thanks to all,

Vwalla
 
Would SQL injection be a concern?
Depends on who has access to the program/database and how sensitive/important the data is. That's your call.


zemp
 
Me & whoever tries to crack my computer :)!

The data is not sensitive, just tracking sales tax that we have paid throughout the year (We can write it off in Texas..wooHoo!)

I will use it on my company's price database program for my outside salesmen.

Thanks,

Vwalla
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top