ciao Roberto,
ci sono diverse opzioni per creare una tabella, Ado, Dao, e DDL.
dal codice che posti ho come l'impressione tu abbia fatto un mix con l'evento not in list di una combo a Ado :-).
provo a darti un esempio di quanto sopra indicato.
Crea una maschera con una TextBox la chiami txtTable.
Crea un commanButton lo chiami cmdCreateTable.
su evento click invochi :
Option Compare Database
Option Explicit
Private Sub cmdCreateTable_Click()
With Me
If Len(.txtTable & vbNullString) = 0 Then
VBA.MsgBox prompt:="Nessuna nome di tabella inserito", _
buttons:=vbCritical, _
title:="Attenzione"
Exit Sub
End If
CreaTabellaConDAO .txtTable 'prova anche le altre due soluzioni
End With
End Sub
CreaTabellaConDAO è una delle sub che inserirai un modulo standard.
Inserisco anche le altre due routine per creare una tabella con Ado e uno statement SQL, ( Data, Definition, Language)
Option Compare Database
Option Explicit
Public Sub CreaTabellaConDDL(ByVal strTableName As String)
On Error GoTo errorHandler
Dim strSql As String
strSql = "CREATE TABLE [" & strTableName & "](" & _
"CreationTime2 DATE," & _
"LastModificationTime DATE," & _
"SenderName MEMO," & _
"SenderAddress MEMO," & _
"SentOn DATE," & _
"Sent YESNO," & _
"TO memo," & _
"CC memo," & _
"BCC memo," & _
"UnRead YESNO," & _
"ReceivedByName MEMO," & _
"ReceivedOnBehalfOfName MEMO," & _
"ReceivedTime DATE," & _
"ConversationTopic MEMO," & _
"Subject memo," & _
"Categories MEMO," & _
"HTMLBody MEMO," & _
"Size Long," & _
"fullPath MEMO," & _
"Attachments MEMO)"
CurrentDb.Execute strSql, &H80
exitErrHandler:
Application.RefreshDatabaseWindow
Exit Sub
errorHandler:
With Err
Select Case .Number
Case 3010
If MsgBox(prompt:="La tabella esiste già. La elimino e la creo di nuovo?", _
buttons:=vbYesNo + vbExclamation, _
Title:="Attenzione") = vbYes Then
CurrentDb.TableDefs.Delete strTableName
Resume
End If
Case Else
VBA.MsgBox "Errore: " & Err & vbCrLf & Err.Description
End Select
Resume exitErrHandler
End With
End Sub
Public Sub CreaTabellaConAdo(ByVal strTableName As String)
On Error GoTo errorHandler
Dim cat As New ADOX.Catalog
Dim tbl As New ADOX.Table
cat.ActiveConnection = CurrentProject.Connection
With tbl
.Name = strTableName
.Columns.Append "nome", adVarWChar
.Columns.Append "cognome", adVarWChar
.Columns.Append "indirizzo2", adVarWChar
.Columns.Append "Note", adLongVarWChar
.Columns("Note").Attributes = adColNullable
End With
exitErrHandler:
cat.Tables.Append tbl
Set cat = Nothing
Application.RefreshDatabaseWindow
Exit Sub
errorHandler:
With Err
Select Case .Number
Case -2147217857
If MsgBox(prompt:="La tabella esiste già. La elimino e la creo di nuovo?", _
buttons:=vbYesNo + vbExclamation, _
Title:="Attenzione") = vbYes Then
cat.Tables.Delete strTableName
Resume
End If
Case Else
VBA.MsgBox "Errore: " & Err & vbCrLf & Err.Description
End Select
Resume exitErrHandler
End With
End Sub
Public Sub CreaTabellaConDAO(ByVal strTableName As String)
On Error GoTo errorHandler
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Set tdf = CurrentDb.CreateTableDef(strTableName)
Set fld = tdf.CreateField("CampoProva", dbText)
tdf.Fields.Append fld
With tdf
.Fields.Append .CreateField("NomeCognome", dbText)
.Fields.Append .CreateField("Qualifica", dbText)
.Fields.Append .CreateField("Telefono", dbText)
.Fields.Append .CreateField("Note", dbMemo)
.Fields("Note").Required = False
End With
CurrentDb.TableDefs.Append tdf
exitErrHandler:
Application.RefreshDatabaseWindow
Exit Sub
errorHandler:
With Err
Select Case .Number
Case 3010
If MsgBox(prompt:="La tabella esiste già. La elimino e la creo di nuovo?", _
buttons:=vbYesNo + vbExclamation, _
Title:="Attenzione") = vbYes Then
CurrentDb.TableDefs.Delete strTableName
Resume
End If
Case Else
VBA.MsgBox "Errore: " & Err & vbCrLf & Err.Description
End Select
Resume exitErrHandler
End With
end sub
se lavori con Jet/Ace non scomoderei ado, ma andrei con Dao oppure con la DDL.
HTH.
Ciao, Sandro.