SQL Import as text not Number

Snailspace

Board Regular
Joined
Jan 28, 2009
Messages
56
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I’m trying to pull some information in from a csv file with the following code.<o:p></o:p>
Rich (BB code):
  SQL_OpenItems = "SELECT *" & _<o:p></o:p>
      " FROM Open_Invoices.csv" & _<o:p></o:p>
      " Where [Acc No]='" & strAccNo1 & "'" & _<o:p></o:p>
      " OR [Acc No]= '" & strAccNo2 & "'" & _<o:p></o:p>
      " ORDER BY [Acc No], [Invoice Date], [Doc No]"<o:p></o:p>
<o:p></o:p>
It looks like it is ‘guessing’ the data type as the majority of items in [Doc No] are numbers, however a few are text and these get omitted from the results.<o:p></o:p>
Is there a way to define the field as text when making the SQL statement?<o:p></o:p>
Cheers SP
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try:-
Code:
  SQL_OpenItems = "SELECT *[COLOR=red][B], CStr([Doc No]) As [Doc TxtNo][/B][/COLOR][COLOR=#000000]" & _
    " FROM Open_Invoices.csv" & _
      " Where [Acc No]='" & strAccNo1 & "'" & _
      " OR [Acc No]= '" & strAccNo2 & "'" & _
      " ORDER BY [Acc No], [Invoice Date], [Doc No]"
[/COLOR]
This will add an extra text field called Doc TxtNo to the output of the query, but I think the problem is that Excel is seeing all numerics and assuming it's a number.

If that doesn't work, try:-
Code:
  SQL_OpenItems = "SELECT *[COLOR=red][B], """" & [Doc No] & """" As [Doc TxtNo][/B][/COLOR][COLOR=#000000]" & _
    " FROM Open_Invoices.csv" & _
      " Where [Acc No]='" & strAccNo1 & "'" & _
      " OR [Acc No]= '" & strAccNo2 & "'" & _
      " ORDER BY [Acc No], [Invoice Date], [Doc No]"
[/COLOR]
 
Upvote 0
i generally use a schema file to declare my fields, data types and sizes, when using ADO import of CSV data
 
Upvote 0
The other way would be to set IMEX=1 in the extended properties list for ADO, but this does depend on the import being done via ADo hence my question.
 
Upvote 0
Thanks for the replies, apologies for not replying sooner, I needed a Friday night off.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Ruddles: I tried both your codes and it didn’t seem to make a change, it brought in the additional field OK for numbers, but not the text items.<o:p></o:p>
<o:p></o:p>
Rich: I think this is the info you queried, this is my first attempt at ADO so apologies if it’s not.
I think the IMEX=1 thing might be a bit over my head at the moment<o:p></o:p>
<o:p></o:p>
Rich (BB code):
ConnectionString = _<o:p></o:p>
"Provider=Microsoft.Jet.OLEDB.4.0;" & _<o:p></o:p>
"Data Source=C:\Documents and Settings\HP_Administrator\My Documents\Work\ExcelAddins\Background Files\;" & _<o:p></o:p>
"Extended Properties=Text;"<o:p></o:p>
<o:p></o:p>
Call rsOpenItems.Open(SQL_OpenItems, ConnectionString, _<o:p></o:p>
CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly, _<o:p></o:p>
CommandTypeEnum.adCmdText)<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Jim: After struggling to spell the file name correctly (doh), I’ve put the following into the Schema file, this seems to have defaulted all setting to text.
I guess its good practise to define each field’s data type.
Rich (BB code):
[Open_invoice_temp.csv]<o:p></o:p>
ColNameHeader=true<o:p></o:p>
Format=CSVDelimited<o:p></o:p>
MaxScanRows=0<o:p></o:p>
"Doc No"= text width 20<o:p></o:p>
<o:p></o:p>
Thanks for the advice, I think I’m on the right track now.<o:p></o:p>
<o:p></o:p>SP<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top