|
楼主 |
发表于 2014-4-24 18:33
|
显示全部楼层
zhaogang1960 发表于 2014-4-24 18:25
请上传这样的附件看看
导入csv文件access联合查询后导入(csv文件非连接状态)-使用别名.zip
(38.39 KB, 下载次数: 2)
请查看,access中字段都改成容许为空
我想改成:
- Sub 联合查询后导入xx() '引用Microsoft ActiveX Data Objects 2.x Library
- Dim cnn As New ADODB.Connection
- Dim myPath$, MyFile$, SQL$, s$
- s = "客户代码,处理号,订单号,服务类型,处理点,[重量(g)] as 重量,[长度(cm)] as 长度,[宽度(cm)] as 宽度,[高度(cm)] as 高度,[体积(cm^3)] as 体积,国家,省,城市,创建时间=case when 创建时间 is not null then format(创建时间,'yyyy-m-d') else 0 end,完成时间= case when 完成时间 is null then format(完成时间,'yyyy-m-d') else 0 end"
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & "\expackage.mdb" '连接数据库
- myPath = ThisWorkbook.Path & ""
- MyFile = Dir(myPath & "*.csv")
- Do While MyFile <> "" '创建多个文本文件的联合查询
- If Len(SQL) Then
- SQL = SQL & " union all select " & s & " from [Text;FMT=Delimited;imex=1;HDR=Yes;DATABASE=" & myPath & ";].[" & MyFile & "]"
- Else
- SQL = "select " & s & " from [Text;FMT=Delimited;imex=1;HDR=Yes;DATABASE=" & myPath & ";].[" & MyFile & "]"
- End If
- MyFile = Dir()
- Loop
- SQL = "insert into expackage select * from (" & SQL & ")" '向access数据表中添加数据语句
- cnn.Execute SQL
- MsgBox "已经成功将文本文件数据保存为数据库!", vbInformation
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码
|
|