Importing a Range from Excel
I have this code below that works, and imports the data into the relevant tables. on the line DoCmd.TransferSpreadsheet acLink, , strTempTable, strFilePath, True How can I specify that it import from a range. I have a Range called ImportData on the excel sheet?
Private Sub cmdImport_Click()
On Error GoTo Err
Dim cnn As ADODB.Connection
Dim sQRY As String
DoCmd.SetWarnings False
strTempTable = "tblCSATAddressTEMP"
strFilePath = Me.txtFilePath
'Check that BusinessType has been selected- quit if not
If IsNull(Me.cboBusinessType) Or Me.cboBusinessType = "" Then
MsgBox "You must select a Business Type from the dropdown list", vbExclamation, cApplicationName
Exit Sub
End If
If VBA.Len(strFilePath) <> 0 Then
'Import the spreadsheet
DoCmd.TransferSpreadsheet acLink, , strTempTable, strFilePath, True
Set cnn = New ADODB.Connection
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin; " & _
"Data Source=" & cTables
'insert new addresses into tblAddress
sQRY = _
"INSERT INTO tblCSATAddress" & vbCrLf & _
"( JobNumber, Address, ProjectID, Project, JobDate, TeamCode, Engineer, Contract, BusinessType )" & vbCrLf & _
"IN '" & cTables & "' " & vbCrLf & _
"SELECT" & vbCrLf & _
"tblCSATAddressTEMP.[No]," & vbCrLf & _
"tblCSATAddressTEMP.Description," & vbCrLf & _
"tblCSATAddressTEMP.[Bill-to Customer No]," & vbCrLf & _
"tblCSATAddressTEMP.[Scheme Code]," & vbCrLf & _
"tblCSATAddressTEMP.[Planned Start Date]," & vbCrLf & _
"tblCSATAddressTEMP.[Team Code]," & vbCrLf & _
"tblFamilyTree.Engineer," & vbCrLf & _
"tblFamilyTree.Contract," & vbCrLf & _
"'" & Me.cboBusinessType & "' AS BusinessType" & vbCrLf & _
"FROM tblCSATAddressTEMP " & vbCrLf & _
"LEFT JOIN tblFamilyTree ON tblCSATAddressTEMP.[Team Code] = tblFamilyTree.TeamCode"
CurrentProject.Connection.Execute sQRY
End If
DoCmd.DeleteObject acTable, strTempTable
MsgBox Me.cboBusinessType & " Data has been imported", vbExclamation, cApplicationName
cnn.Close
Set cnn = Nothing
Exit Sub
Err:
MsgBox VBA.Error$
End Sub
Thanks, Jez
|