
Public Sub Scrivi_Dati()
On Error GoTo RigaErrore
Dim cn As Object
Dim sh As Worksheet
Dim sSQL As String
Set cn = CreateObject("ADODB.Connection")
Set sh = Worksheets("Foglio2")
With sh
sSQL = "INSERT INTO Ubicazio " & _
"(ID, Codice, Descrizione) " & _
" VALUES (" & _
"'" & .Range("A1").Value & "', " & _
"'" & .Range("B1").Value & "', " & _
"'" & .Range("C1").Value & "'" & _
")"
End With
With cn
.CursorLocation = 1
.Open "Provider=SQLNCLI10;" & _
"Server=UTENTE-PCSQLEXPRESS;" & _
"Database=mig50;" & _
"Trusted_Connection=yes;"
.Execute sSQL, , 1
End With
RigaChiusura:
If cn.State = 1 Then
cn.Close
End If
Set cn = Nothing
Set sh = Nothing
Exit Sub
RigaErrore:
MsgBox Err.Number & vbNewLine & Err.Description
Resume RigaChiusura
End Sub
|
Option Explicit
Public Sub Scrivi_Dati()
Dim cn As Object
Dim cell As Range
Dim sSQL As String
Set cn = CreateObject("ADODB.Connection")
With cn
.CursorLocation = 1
.Open "Provider=SQLNCLI10; Server=UTENTE-PCSQLEXPRESS; Database=mig50; Trusted_Connection=yes;"
End With
Worksheets("Foglio2").Activate
On Error GoTo RigaErrore
For Each cell In Range("A:A")
If cell.Row > 1 Then
If Trim(cell) = "" Then Exit For
sSQL = "INSERT INTO Ubicazio (ID, Codice, Descrizione) " & _
" VALUES ('" & cell & "', '" & cell.Offset(, 1) & "', '" & cell.Offset(, 2) & "')"
cn.Execute sSQL, , 1
End If
Next
RigaChiusura:
If cn.State = 1 Then cn.Close
Set cn = Nothing
Exit Sub
RigaErrore:
MsgBox Err.Number & vbNewLine & Err.Description
Resume RigaChiusura
End Sub |
Option Explicit
Public Sub Scrivi_Struttura()
Dim cn As Object
Dim cell As Range
Dim sSQL As String
Set cn = CreateObject("ADODB.Connection")
With cn
.CursorLocation = 1
.Open "Provider=SQLNCLI10; Server=UTENTE-PCSQLEXPRESS; Database=mig50; Trusted_Connection=yes;"
End With
Worksheets("Foglio8").Activate
On Error GoTo RigaErrore
For Each cell In Range("A:A")
If cell.Row > 1 Then
If Trim(cell) = "" Then Exit For
sSQL = "INSERT INTO Struttur (ID, Livello, Padre, F_tag, F_ScTcn, IdCentriCo, IdUbicazio, idTipStrut, Codice, Descrizione, Qta, Nota, Pathid, F_templPre, F_Figlio, PathIdEsterno, F_Comp) " & _
" VALUES ('" & cell & "', '" & cell.Offset(, 1) & "', '" & cell.Offset(, 2) & cell.Offset(, 3) & cell.Offset(, 4) & cell.Offset(, 5) & cell.Offset(, 6) & cell.Offset(, 7) & cell.Offset(, 8) & cell.Offset(, 9) & cell.Offset(, 10) & cell.Offset(, 11) & cell.Offset(, 12) & cell.Offset(, 13) & cell.Offset(, 14) & cell.Offset(, 15) & cell.Offset(, 16) & cell.Offset(, 17) & "')"
cn.Execute sSQL, , 1
End If
Next
RigaChiusura:
If cn.State = 1 Then cn.Close
Set cn = Nothing
Exit Sub
RigaErrore:
MsgBox Err.Number & vbNewLine & Err.Description
Resume RigaChiusura
End Sub
|
sSQL = "INSERT INTO Struttur (ID, Livello, Padre, F_tag, F_ScTcn, IdCentriCo, IdUbicazio, idTipStrut, Codice, Descrizione, Qta, Nota, Pathid, F_templPre, F_Figlio, PathIdEsterno, F_Comp) " & _
" VALUES ('" & cell & "', '" & cell.Offset(, 1) & "', '" & cell.Offset(, 2) & "', '" & cell.Offset(, 3) & "', '" & cell.Offset(, 4) & "', '" & cell.Offset(, 5) & "', '" & cell.Offset(, 6) & "', '" & cell.Offset(, 7) & "', '" & cell.Offset(, 8) & "', '" & cell.Offset(, 9) & "', '" & cell.Offset(, 10) & "', '" & cell.Offset(, 11) & "', '" & cell.Offset(, 12) & "', '" & cell.Offset(, 13) & "', '" & cell.Offset(, 14) & "', '" & cell.Offset(, 15) & "', '" & cell.Offset(, 16) & "', '" & cell.Offset(, 17) & "')" |
cn.Execute "DROP TABLE tabella"
Public Sub Leggi_struttura()
On Error GoTo RigaErrore
Dim cn As Object
Dim rs As Object
Dim sh As Worksheet
Dim sSQL As String
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set sh = Worksheets("Foglio8")
sSQL = "SELECT * FROM struttur" ' seleziona la tabella da leggere
With cn
.CursorLocation = 1
.Open "Provider=SQLNCLI10;" & _
"Server=UTENTE-PCSQLEXPRESS;" & _
"Database=mig50;" & _
"Trusted_Connection=yes;"
End With
With rs
.CursorLocation = 1
.Open sSQL, cn, 1, 3, 1
End With
With sh
.Range("A2").Cells.Clear
.Range("A2").CopyFromRecordset rs
End With
RigaChiusura:
If rs.State = 1 Then
rs.Close
End If
If cn.State = 1 Then
cn.Close
End If
Set rs = Nothing
Set cn = Nothing
Set sh = Nothing
Exit Sub
RigaErrore:
MsgBox Err.Number & vbNewLine & Err.Description
Resume RigaChiusura
End Sub
|
Option Explicit
Public Sub Scrivi_Struttura()
Dim cn As Object
Dim cell As Range
Dim sSQL As String
Dim Last_ID as long, Last_cell as range
Set cn = CreateObject("ADODB.Connection")
With cn
.CursorLocation = 1
.Open "Provider=SQLNCLI10; Server=UTENTE-PCSQLEXPRESS; Database=mig50; Trusted_Connection=yes;"
End With
'recuperiamo l'ultimo ID dal database e lo assegniamo a Last_ID
Last_ID = cn.execute("SELECT Max(ID) FROM struttur")(0)
Worksheets("Foglio8").Activate
set Last_cell = range("A:A").find(Last_ID)
For Each cell In Range("A" & (Last_cell.row + 1) & ":A65535")
If Trim(cell) = "" Then Exit For
sSQL = "INSERT INTO Struttur (ID, Livello, Padre, F_tag, F_ScTcn, IdCentriCo, IdUbicazio, idTipStrut, Codice, Descrizione, Qta, Nota, Pathid, F_templPre, F_Figlio, PathIdEsterno, F_Comp) " & _
" VALUES ('" & cell & "', '" & cell.Offset(, 1) & "', '" & cell.Offset(, 2) & cell.Offset(, 3) & cell.Offset(, 4) & cell.Offset(, 5) & cell.Offset(, 6) & cell.Offset(, 7) & cell.Offset(, 8) & cell.Offset(, 9) & cell.Offset(, 10) & cell.Offset(, 11) & cell.Offset(, 12) & cell.Offset(, 13) & cell.Offset(, 14) & cell.Offset(, 15) & cell.Offset(, 16) & cell.Offset(, 17) & "')"
cn.Execute sSQL, , 1
Next
cn.Close
Set cn = Nothing
End Sub |
sSQL = "INSERT INTO Struttur (ID, Livello, Padre, F_tag, F_ScTcn, IdCentriCo, IdUbicazio, idTipStrut, Codice, Descrizione, Qta, Nota, Pathid, F_templPre, F_Figlio, PathIdEsterno, F_Comp) " & _
" VALUES ('" & cell & "', '" & cell.Offset(, 1) & "', '" & cell.Offset(, 2) & "', '" & cell.Offset(, 3) & "', '" & cell.Offset(, 4) & "', '" & cell.Offset(, 5) & "', '" & cell.Offset(, 6) & _
"', '" & cell.Offset(, 7) & "', '" & cell.Offset(, 8) & "', '" & cell.Offset(, 9) & "', '" & cell.Offset(, 10) & "', '" & cell.Offset(, 11) & "', '" & cell.Offset(, 12) & "', '" & cell.Offset(, 13) & _
"', '" & cell.Offset(, 14) & "', '" & cell.Offset(, 15) & "', '" & cell.Offset(, 16) & "')"
|
... ", " & cell.Offset(, 5) & ", " ...
INSERT INTO tabella
(cognome, nome, indirizzo, luogo, datainizio, datafine, tipoevento) VALUES
('" & txtCognome.text & "','" & txtNome.Text & "','" & txtIndirizzo
& "','" & txtLuogo.Text & "'",#" & txtDataInizio.Text & "#,#"
& txtDataFine.Text & "#,'" & txtTipoEcento.Text & "')"
Dim par As ADODB.Parameter
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Dim sSQL As String
sSQL = sSQL & " INSERT INTO tabella"
sSQL = sSQL & " (cognome, nome, indirizzo, luogo, datainizio, datafine, tipoevento)"
sSQL = sSQL & " VALUES (@cognome, @nome, @indirizzo, @luogo, @datainizio, @datafine, @tipoevento)"
sSQL = sSQL & " VALUES ?, ?, ?, ?, ?, ?, ?)"
Set parameter = command.CreateParameter (Name, Type, Direction, Size, Value)
With cmd
Set par = .CreateParameter ("@cognome", adVarChar, adParamInput, 50, txtCognome.Text)
.Parameters.Append par
Set par = .CreateParameter ("@nome", adVarChar, adParamInput, 50, txtNome.Text)
.Parameters.Append par
Set par = .CreateParameter ("@indirizzo", adVarChar, adParamInput, 50, txtIndirizzo.Text)
.Parameters.Append par
Set par = .CreateParameter ("@luogo", adVarChar, adParamInput, 50, txtLuogo.Text)
.Parameters.Append par
Set par = .CreateParameter ("@datainizio", adDate, adParamInput, , txtDataInizio.Text)
.Parameters.Append par
Set par = .CreateParameter ("@datafine", adDate, adParamInput, , txtDataFine.Text)
.Parameters.Append par
Set par = .CreateParameter ("@tipoevento", adVarChar, adParamInput, 20, txtTipoEvento.Text)
.Parameters.Append par
End With
cmd.ActiveConnection = CN ‘è la connessione aperta al database
cmd.CommandType = adCmdText ‘adCmdText = 1: è il Tipo di comando, è semplice testo
cmd.CommandText = sSQL ‘il testo del comando, la nostra query
cmd.Execute
staff@excelvba.it
