Private Sub cmdImport_Click()
'Declare temporary variables
Dim cnnXls As ADODB.Connection
Dim rsSchema As ADODB.Recordset
Dim rsXl As ADODB.Recordset
Dim lRecs As Long
Dim iCount As Integer
Dim iCols As Integer
Dim i As Integer
Dim sFields As String
Dim iValues As Integer
Dim sValues As String
If FileName = "" Then
If DBFileName = "" Then
FileName = frmOpen.FileName1
DBFileName = frmOpen.DBFileName1
Else
End If
Else
End If
If FileName = "" Then
If DBFileName = "" Then
MsgBox "Please make sure you have the nescessry files open"
Else
'Set the ADODB connection as a new ADODB connection
Set cnnXls = New ADODB.Connection
'Open the selected Excel Project
cnnXls.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";Extended Properties=Excel 8.0;"
'Set the Record set equal to the cnnXls open schema
Set rsSchema = cnnXls.OpenSchema(adSchemaTables)
'Set rsXl as a new record set
Set rsXl = New ADODB.Recordset
Debug.Print rsSchema.Fields("TABLE_NAME").Value
'Filter the record set to get the table name
rsSchema.Filter = "TABLE_NAME = 'Sheet1$'"
If rsSchema.BOF = True And rsSchema.EOF = True Then
'Disply a message box stating that a Excel file was not found
MsgBox "Excel sheet not found!", vbOKOnly + vbExclamation
'Go to the clean up section
GoTo CleanUp
End If 'End if
rsXl.Open "SELECT * FROM `" & rsSchema.Fields("TABLE_NAME").Value & "`", cnnXls, adOpenStatic, adLockReadOnly, adCmdText
'Close the record set
rsSchema.Close
'Set the record set to nothing
Set rsSchema = Nothing
'Get the number of records
iCount = rsXl.RecordCount
'Get the number of fields
iCols = rsXl.Fields.Count
If rsXl.BOF = True And rsXl.EOF = True Then
'Disply a message box stating that a Excel data was not found
MsgBox "No Excel data found"
Else
'Fill the sFields
sFields = vbNullString
For i = 0 To rsXl.Fields.Count - 1
sFields = sFields & " " & rsXl.Fields(i).Name & ","
Next
sFields = Left$(sFields, Len(sFields) - 1)
'Delete all the records from
moCnn.Execute "DELETE FROM Table1;", lRecs
'Display a message box telling the user how many records were deleted
MsgBox lRecs & " - Records deleted!", vbOKOnly + vbInformation
For i = 1 To rsXl.RecordCount
'Assing a null string to the sValues variable
sValues = vbNullString
For iValues = 0 To rsXl.Fields.Count - 1
sValues = sValues & rsXl.Fields(iValues).Value & "','"
Next
sValues = Left$(sValues, Len(sValues) - 2)
'Insert the info into the database
moCnn.Execute "INSERT INTO Table1 (" & sFields & ") VALUES ('" & sValues & ")", lRecs
'Add 1 to the counter
Counter = Counter + 1
'Move the the next record
rsXl.MoveNext
Next
End If 'End if
'Display a message box telling the user how many records were imported
MsgBox Counter & " -Records imported successfully!", vbOKOnly + vbInformation
'Set the counter to 0
Counter = 0
End If
Else
'Set the ADODB connection as a new ADODB connection
Set cnnXls = New ADODB.Connection
'Open the selected Excel Project
cnnXls.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";Extended Properties=Excel 8.0;"
'Set the Record set equal to the cnnXls open schema
Set rsSchema = cnnXls.OpenSchema(adSchemaTables)
'Set rsXl as a new record set
Set rsXl = New ADODB.Recordset
Debug.Print rsSchema.Fields("TABLE_NAME").Value
'Filter the record set to get the table name
rsSchema.Filter = "TABLE_NAME = 'Sheet1$'"
If rsSchema.BOF = True And rsSchema.EOF = True Then
'Disply a message box stating that a Excel file was not found
MsgBox "Excel sheet not found!", vbOKOnly + vbExclamation
'Go to the clean up section
GoTo CleanUp
End If 'End if
rsXl.Open "SELECT * FROM `" & rsSchema.Fields("TABLE_NAME").Value & "`", cnnXls, adOpenStatic, adLockReadOnly, adCmdText
'Close the record set
rsSchema.Close
'Set the record set to nothing
Set rsSchema = Nothing
'Get the number of records
iCount = rsXl.RecordCount
'Get the number of fields
iCols = rsXl.Fields.Count
If rsXl.BOF = True And rsXl.EOF = True Then
'Disply a message box stating that a Excel data was not found
MsgBox "No Excel data found"
Else
'Fill the sFields
sFields = vbNullString
For i = 0 To rsXl.Fields.Count - 1
sFields = sFields & " " & rsXl.Fields(i).Name & ","
Next
sFields = Left$(sFields, Len(sFields) - 1)
'Delete all the records from
moCnn.Execute "DELETE FROM Table1;", lRecs
'Display a message box telling the user how many records were deleted
MsgBox lRecs & " - Records deleted!", vbOKOnly + vbInformation
For i = 1 To rsXl.RecordCount
'Assing a null string to the sValues variable
sValues = vbNullString
For iValues = 0 To rsXl.Fields.Count - 1
sValues = sValues & rsXl.Fields(iValues).Value & "','"
Next
sValues = Left$(sValues, Len(sValues) - 2)
'Insert the info into the database
moCnn.Execute "INSERT INTO Table1 (" & sFields & ") VALUES ('" & sValues & ")", lRecs
'Add 1 to the counter
Counter = Counter + 1
'Move the the next record
rsXl.MoveNext
Next
End If 'End if
'Display a message box telling the user how many records were imported
MsgBox Counter & " -Records imported successfully!", vbOKOnly + vbInformation
'Set the counter to 0
Counter = 0
End If
Exit Sub
'Clean up
CleanUp:
'Set the recordset to nothing
Set rsXl = Nothing
If cnnXls.State = adStateOpen Then cnnXls.Close
End Sub 'End sub