I'm using the Microsoft Excel Driver to import an Excel document into a adodb.recordset so that I can remove duplicate rows and put it into the SQL Server database.
The first column has values like 192, 13U, JJJ, etc. but for some reason the query casts it as a double value, and any rows with alpha values get converted to Null. From what I can tell this is because the Majority type is numeric instead of text.
I tried casting it, but got an error.
Here's my function:
Function Read_Excel(ByVal sFile As String) As ADODB.Recordset
On Error GoTo fix_err
Dim rs As ADODB.Recordset
rs = New ADODB.Recordset
Dim sconn As String
rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient
rs.CursorType = ADODB.CursorTypeEnum.adOpenKeyset
rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic
sconn = "DRIVER=Microsoft Excel Driver (*.xls); ImportMixedTypes=Text; " & "DBQ=" & sFile & ";Extended Properties='Excel 8.0;HDR=No;IMEX=1';"
rs.Open("SELECT Code, Description FROM [sheet1$]", sconn)
tot += rs.RecordCount
rs.Close()
rs.Open("SELECT Distinct * FROM [sheet1$]", sconn)
Read_Excel = rs
rs = Nothing
Exit Function
fix_err:
Debug.Print(Err.Description + " " + _
Err.Source, vbCritical, "Import")
Err.Clear()
End Function
Is there a way to get the first column as text easily?
EDIT: When I try to "SELECT cast(RPOCode as varchar(10)), Description FROM [sheet1$]"
I get this error
"[Microsoft][ODBC Excel Driver] Syntax error (missing operator) in query expression 'cast(Code as varchar)'. Microsoft OLE DB Provider for ODBC Drivers"
I've tried varchar, varchar(10) and text as the casting types with the same result.
Best Answer
Try:
Cast is not available in Jet/ACE SQL.