Visual Basic - problemas con insert

Life is soft - evento anual de software empresarial
 
Vista:

problemas con insert

Publicado por Mari3 (38 intervenciones) el 17/06/2009 16:28:31
Hola... Necesito ayuda...
Tengo problema con el insert into me sale un error cuando intento guardar los datos que ingreso en los text... a lo mejor tengo un error en el código... espero que me puedan ayudar...

Este es mi codigo

rs.Open "INSERT INTO datos (pozo,estacion,ub,marca,modelo,fija,portatil,tipocorrea,protcorrea,stroker,celler,palanca,faja,bt,guaya,carrera,pintura,modelos,empaques,silla,cruceta,pines,marcame,serialme,potenciame,diametroeje,fijame,busingme,interno,externo,marcarc,serialrc,observacionrc,marcapc,serialpc,potenciapc,variadorpc,fechapc,observacionpc" _
&VALUES(" + Text1.Text + ",'"+ Text2.Text + "','" + Text3.Text + "','" + Text4.Text + "','" + Text5.Text + "','" + Text6.Text + "','" + Text7.Text + "','" + Text8.Text + "','" + Text9.Text + "','" + Text10.Text + "','" + Text11.Text + "','" + Text12.Text + "','" + Text13.Text + "','" + Text14.Text + "','" + Text15.Text + "','" + Text16.Text + "','" + Text17.Text + "','" + Text18.Text + "','" + Text19.Text + "','" + Text20.Text + "','" + Text21.Text + "','" + Text22.Text + "','" + Text23.Text + "','" + Text24.Text + "','" + Text25.Text + "','" + Text26.Text + "','" + Text27.Text + "','" + Text28.Text + "','" & Text29.Text + "','" + Text30.Text + "','" + Text31.Text + "','" + Text32.Text + "','" + Text33.Text + "','" + Text34.Text + "','" + Text35.Text + "','" + Text36.Text + "','" + Text37.Text + "','" + Text38.Text + "','" + Text39.Text + "'),
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:problemas con insert

Publicado por Quique (51 intervenciones) el 17/06/2009 16:54:49
No nos dices qué error te da, eso nos ayudaría bastante para tratar de ayudarte.
Pero, bueno, sí hay errores, al menos a simple vista, porque no revisé si tus campos coinciden con los valores.
Para empezar, después del campo "observacionpc"
y antes de VALUES jamás cerraste el paréntesis que debería encerrar los nombres de los campos. Y para terminar, el paréntesis que cierra los valores (VALUES) no lo cierras con comillas, sino con una coma.
Aquí traté de corregirlo, a ver si te funciona.

rs.Open "INSERT INTO datos (pozo,estacion,ub,marca,modelo,fija,portatil,tipocorrea,protcorrea,stroker,celler,palanca,faja,bt,guaya,carrera,pintura,modelos,empaques,silla,cruceta,pines,marcame,serialme,potenciame,diametroeje,fijame,busingme,interno,externo,marcarc,serialrc,observacionrc,marcapc,serialpc,potenciapc,variadorpc,fechapc,observacionpc)" _
&VALUES(" + Text1.Text + ",'"+ Text2.Text + "','" + Text3.Text + "','" + Text4.Text + "','" + Text5.Text + "','" + Text6.Text + "','" + Text7.Text + "','" + Text8.Text + "','" + Text9.Text + "','" + Text10.Text + "','" + Text11.Text + "','" + Text12.Text + "','" + Text13.Text + "','" + Text14.Text + "','" + Text15.Text + "','" + Text16.Text + "','" + Text17.Text + "','" + Text18.Text + "','" + Text19.Text + "','" + Text20.Text + "','" + Text21.Text + "','" + Text22.Text + "','" + Text23.Text + "','" + Text24.Text + "','" + Text25.Text + "','" + Text26.Text + "','" + Text27.Text + "','" + Text28.Text + "','" & Text29.Text + "','" + Text30.Text + "','" + Text31.Text + "','" + Text32.Text + "','" + Text33.Text + "','" + Text34.Text + "','" + Text35.Text + "','" + Text36.Text + "','" + Text37.Text + "','" + Text38.Text + "','" + Text39.Text + "')"
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:problemas con insert

Publicado por Mari3 (38 intervenciones) el 17/06/2009 19:31:39
OK, cuando mande la nota se me olvido colocar esos detalles…. Igual me sigue dando error
Si lo coloco así
rs.Open "INSERT INTO datos (pozo,estacion,ub,marca,modelo,fija,portatil,tipocorrea,protcorrea,stroker,celler,palanca,faja,bt,guaya,carrera,pintura,modelos,empaques,silla,cruceta,pines,marcame,serialme,potenciame,diametroeje,fijame,busingme,interno,externo,marcarc,serialrc,observacionrc,marcapc,serialpc,potenciapc,variadorpc,fechapc,observacionpc)" _
&VALUES(" + Text1.Text + ",'"+ Text2.Text + "','" + Text3.Text + "','" + Text4.Text + "','" + Text5.Text + "','" + Text6.Text + "','" + Text7.Text + "','" + Text8.Text + "','" + Text9.Text + "','" + Text10.Text + "','" + Text11.Text + "','" + Text12.Text + "','" + Text13.Text + "','" + Text14.Text + "','" + Text15.Text + "','" + Text16.Text + "','" + Text17.Text + "','" + Text18.Text + "','" + Text19.Text + "','" + Text20.Text + "','" + Text21.Text + "','" + Text22.Text + "','" + Text23.Text + "','" + Text24.Text + "','" + Text25.Text + "','" + Text26.Text + "','" + Text27.Text + "','" + Text28.Text + "','" & Text29.Text + "','" + Text30.Text + "','" + Text31.Text + "','" + Text32.Text + "','" + Text33.Text + "','" + Text34.Text + "','" + Text35.Text + "','" + Text36.Text + "','" + Text37.Text + "','" + Text38.Text + "','" + Text39.Text + "')",

ES DECIR CON COMILLAS Y COMA (,) al final me dice error de sintaxis,

Ahora si le quito las comillas al final y solo coloco la coma es decir:

rs.Open "INSERT INTO datos (pozo,estacion,ub,marca,modelo,fija,portatil,tipocorrea,protcorrea,stroker,celler,palanca,faja,bt,guaya,carrera,pintura,modelos,empaques,silla,cruceta,pines,marcame,serialme,potenciame,diametroeje,fijame,busingme,interno,externo,marcarc,serialrc,observacionrc,marcapc,serialpc,potenciapc,variadorpc,fechapc,observacionpc)" _
&VALUES(" + Text1.Text + ",'"+ Text2.Text + "','" + Text3.Text + "','" + Text4.Text + "','" + Text5.Text + "','" + Text6.Text + "','" + Text7.Text + "','" + Text8.Text + "','" + Text9.Text + "','" + Text10.Text + "','" + Text11.Text + "','" + Text12.Text + "','" + Text13.Text + "','" + Text14.Text + "','" + Text15.Text + "','" + Text16.Text + "','" + Text17.Text + "','" + Text18.Text + "','" + Text19.Text + "','" + Text20.Text + "','" + Text21.Text + "','" + Text22.Text + "','" + Text23.Text + "','" + Text24.Text + "','" + Text25.Text + "','" + Text26.Text + "','" + Text27.Text + "','" + Text28.Text + "','" & Text29.Text + "','" + Text30.Text + "','" + Text31.Text + "','" + Text32.Text + "','" + Text33.Text + "','" + Text34.Text + "','" + Text35.Text + "','" + Text36.Text + "','" + Text37.Text + "','" + Text38.Text + "','" + Text39.Text + "'),

Me dice que se espera una expresión y me marca el error en las comillas simples..!!

¡¡Espero me puedan ayudar!!
Y Bueno, muchas gracias por intentar hacerlo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:problemas con insert

Publicado por Mari3 (38 intervenciones) el 17/06/2009 19:39:59
al igual que al cerrarlo solo con comillas como me dijo en la nota que envió, tambien me arroja el mismo error es decir 'Se Espera Una Expresión'
Bueno, de verdad disculpen la molestia y espero que me puedan ayudar a solucionar este problema!!
GRACIAS DE ANTEMANO
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:problemas con insert

Publicado por aquiles (10 intervenciones) el 18/06/2009 03:55:26
* Para no tener ese tipo de errores es preferible adoptar una técnica al escribir
mandatos SQL.

* Observación;
al utilizar el '&' , VB asume que será una concatenacion.
al utilizar el '+' , VB se adapta de acuerdo al tipo de datos( suma numérica
o unión de cadenas ).

* Esta es la forma de continuar en la siguiente linea
( y creo que tiene un limite al utilizar el '_' )

Dim sql As String
sql = "INSERT INTO datos " & _
"(pozo,estacion,ub,marca) VALUES (" & _
"" & text1.Text & "" & "," & _
"" & text2.Text & "" & "," & _
"'" & text3.Text & "'" & "," & _
"'" & text4.Text & "'" & "" & ");"

MsgBox sql ' con esto verificas la sintaxis.

* Te recomiendo utilizar este formato:

Dim sql As String
sql = sql & "INSERT INTO datos"
sql = sql & "(pozo,estacion,ub,marca) VALUES ("
sql = sql & "" & text1.Text & "" & ","
sql = sql & "" & text2.Text & "" & ","
sql = sql & "'" & text3.Text & "'" & ","
sql = sql & "'" & text4.Text & "'" & ");"

MsgBox sql ' con esto verificas la sintaxis.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:problemas con insert

Publicado por Mari3 (11 intervenciones) el 18/06/2009 16:12:01
Hola nuevamente. Gracias por el aporte, ahora no me arroja el error pero no se que es lo que pasa porque no me esta guardandando.
no se si es que falta algo!
Bueno, espero y sepan que es está faltando!, este es el codigo
y Muchas gracias por sus aportes

Dim conn As New ADODB.Connection
Dim sql As String
Set rs = New ADODB.Recordset
conn.CursorLocation = adUseServer
conn.CursorLocation = adUseClient
conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE= pozos; UID=root; PWD=1234; OPTION= & 1 + 2 + 8 + 32 + 2048 + 16384"
conn.Open

sql = sql & "INSERT INTO datos"
sql = sql & "(pozo,estacion,ub,marca,modelo,fija,portatil,tipocorrea,protcorrea,stroker,celler,palanca,faja,bt,guaya,carrera,pintura,modelos,empaques,silla,cruceta,pines,marcame,serialme,potenciame,diametroeje,fijame,busingme,interno,externo,marcarc,serialrc,observacionrc,marcapc,serialpc,potenciapc,variadorpc,fechapc,observacionpc) VALUES ("
sql = sql & "" & Text1.Text & "" & ","
sql = sql & "" & Text2.Text & "" & ","
sql = sql & "'" & Text3.Text & "'" & ","
sql = sql & "'" & Text4.Text & "'" & ","
sql = sql & "'" & Text5.Text & "'" & ","
sql = sql & "'" & Text6.Text & "'" & ","
sql = sql & "'" & Text7.Text & "'" & ","
sql = sql & "'" & Text8.Text & "'" & ","
sql = sql & "'" & Text9.Text & "'" & ","
sql = sql & "'" & Text10.Text & "'" & ","
sql = sql & "'" & Text11.Text & "'" & ","
sql = sql & "'" & Text12.Text & "'" & ","
sql = sql & "'" & Text13.Text & "'" & ","
sql = sql & "'" & Text14.Text & "'" & ","
sql = sql & "'" & Text15.Text & "'" & ","
sql = sql & "'" & Text16.Text & "'" & ","
sql = sql & "'" & Text17.Text & "'" & ","
sql = sql & "'" & Text18.Text & "'" & ","
sql = sql & "'" & Text19.Text & "'" & ","
sql = sql & "'" & Text20.Text & "'" & ","
sql = sql & "'" & Text21.Text & "'" & ","
sql = sql & "'" & Text22.Text & "'" & ","
sql = sql & "'" & Text23.Text & "'" & ","
sql = sql & "'" & Text24.Text & "'" & ","
sql = sql & "'" & Text25.Text & "'" & ","
sql = sql & "'" & Text26.Text & "'" & ","
sql = sql & "'" & Text27.Text & "'" & ","
sql = sql & "'" & Text28.Text & "'" & ","
sql = sql & "'" & Text29.Text & "'" & ","
sql = sql & "'" & Text30.Text & "'" & ","
sql = sql & "'" & Text31.Text & "'" & ","
sql = sql & "'" & Text33.Text & "'" & ","
sql = sql & "'" & Text34.Text & "'" & ","
sql = sql & "'" & Text35.Text & "'" & ","
sql = sql & "'" & Text36.Text & "'" & ","
sql = sql & "'" & Text37.Text & "'" & ","
sql = sql & "'" & Text38.Text & "'" & ","
sql = sql & "'" & Text39.Text & "'" & "); conn"

'MsgBox sql ' con esto verificas la sintaxis.

MsgBox "Registro añadido a la Base de Datos", vbOKOnly + vbInformation, "Informacion"
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:problemas con insert

Publicado por Aquiles (2 intervenciones) el 18/06/2009 22:09:19
* Esto está demás ( "conn" ) despues del punto y coma(;):

sql = sql & "'" & Text39.Text & "'" & "); conn" <---


* Para ejecutar el sql :
dim nRecords as Long 'contendrá el total de registros afectados.
Set rs = conn.Execute( sql, nRecords, 0 )
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:problemas con insert

Publicado por Mari3 (11 intervenciones) el 19/06/2009 15:05:08
Ah ok!, pero tengo otra duda... cuando utilizo Execute simpre me sale Error 3704 en tiempo de ejecucion, la operacion no esta permitida si el objeto esta cerrado.. y eso mismo me pasa cuando utilizo Set rs = conn.Execute(sql, nRecords, 0)
sera que tengo que agregerle una referencia o alguna libreria?

Gracias de Antemano
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:problemas con insert

Publicado por Aquiles (10 intervenciones) el 20/06/2009 06:11:16
On Error GoTo fin

Dim sql As String
Dim nRecords As Long
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

cn.CursorLocation = adUseClient
cn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=pozos; UID=root; PWD=1234; OPTION= & 1 + 2 + 8 + 32 + 2048 + 16384"
cn.Open

sql = "INSERT INTO ..."
Set rs = cn.Execute(sql, nRecords, 0)

If nRecords > 0 Then

MsgBox "Registro insertado..."

End If


'rs.Close 'ERROR 3704, no usar close si el sql es un INSERT, DELETE, UPDATE a menos que estos retornen algun registro.
cn.Close


Exit Sub
fin:

MsgBox Err.Description, vbOKOnly + vbCritical, Err.Number & ":"
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:problemas con insert

Publicado por Jaime guerrero (361 intervenciones) el 18/06/2009 00:35:08
me parece que falta

rs.Open "insert ...........", cn

donde cn es tu connection.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar