2010-10-04 98 views
0

我使用excel宏從AS400中提取一些數據。在AS400中,這個特定列(Ref)顯示20100729000078154,但是當我將它提取到excel時,它將是2.01007E + 16。我需要將20100729000078154作爲最終輸出。這是我用來提取AS400的信息的宏: -在Excel中轉​​換爲文本格式

Sub Extract() 

Dim StrSQl As String 

FromA = Format(Sheet1.Range("B3")) 
FromB = Format(Sheet1.Range("B4")) 
FromC = Format(Sheet1.Range("B5")) 
FromD = Format(Sheet1.Range("B6")) 

StrSQl = "select Cno,Itno,Ref from test " 
StrSQl = StrSQl & " where Cno= " & FromA & " and Itno like " & FromB & " and " 
StrSQl = StrSQl & " Ref >= " & FromC & " and Ref <= " & FromD & " " 
StrSQl = StrSQl & " order by Cno " 

con = "Provider=IBMDA400;Data Source=xxx.xxx.xxx.xxx;User Id=yyyyy;Password=zzzzz" 

Set Db = CreateObject("ADODB.Connection") 
Set rs = CreateObject("ADODB.recordset") 
Db.ConnectionString = con 
Db.Open 


rs.Open StrSQl, Db, 3, 3 

Sheet2.Cells(1, 1).CopyFromRecordset rs 

rs.Close 

Set rs = Nothing 
Set cn = Nothing 

End Sub 
+0

是'Ref'是整數還是字符串? – egrunin 2010-10-04 02:04:23

+1

因爲20100729000078154在Excel中不是有效的日期格式,所以您可能想要在SQL語句中格式化它(例如yyyy-mm-dd hh:mm:ss)。 – 2010-10-04 02:10:03

回答

4

,如果你只是想欄顯示爲文本可以前綴一個單引號,像(假設一個單引號字面可以用iSeries SQL中的''表示)...

StrSQl = "select Cno,Itno,CONCAT('''',Ref) as Ref from test " 
+0

它的作品!非常感謝你。 Hv一直在努力尋找正確答案:-) – Bob 2010-10-04 03:54:28