Ver Mensaje Individual
  #2 (permalink)  
Antiguo 19/10/2010, 22:44
Avatar de HackmanC
HackmanC
 
Fecha de Ingreso: enero-2008
Ubicación: Guatemala
Mensajes: 1.817
Antigüedad: 16 años, 3 meses
Puntos: 260
Sonrisa Respuesta: Ejecutar un insert a una base SQL con una macro

Hola,

Posiblemente, ejecuta la instrucción SQL que está en la celda seleccionada (ActiveCell), en la instancia de SQL Server y la base de datos especificada en la conexión. (Necesitas agregar la referencia a "Microsoft ActiveX Data Objects 2.8 Library" en Herramientas->Referencias).

Código vb:
Ver original
  1. Option Explicit
  2.  
  3. Private Sub ExecuteSQL_CurrentRow()
  4.     Dim conConnection As ADODB.Connection
  5.     Dim cmdExecute As ADODB.Command
  6.     Dim strSQL As String
  7.    
  8.     strSQL = Application.ActiveCell.Value
  9.     If Trim(strSQL) = "" Then Exit Sub
  10.    
  11.     On Error GoTo eHand
  12.     Set conConnection = New ADODB.Connection
  13.     Set cmdExecute = New ADODB.Command
  14.     conConnection.ConnectionString = _
  15.         "Provider=SQLOLEDB.1;" & _
  16.         "Password=MyPassword;" & _
  17.         "Persist Security Info=True;" & _
  18.         "User ID=sa;" & _
  19.         "Initial Catalog=MyDatabase;" & _
  20.         "Data Source=MYSERVER\MYSQLEXPRESS"
  21.     conConnection.Open
  22.     cmdExecute.ActiveConnection = conConnection
  23.     cmdExecute.CommandText = strSQL
  24.     cmdExecute.CommandTimeout = 120
  25.     cmdExecute.CommandType = adCmdText
  26.     cmdExecute.Execute
  27.     Exit Sub
  28.    
  29. eHand:
  30.     MsgBox "No se pudo completar la operación." & vbCrLf & _
  31.         Err.Description, vbExclamation + vbOKOnly, "Error"
  32. End Sub

Saludos,