我通过一个模板程序来读取excel表格的数据,是通过union all select 的方式来实现的,现在union个数超过10个提示语法错误了,想咨询下各位高手union最高只能是10 个吗?我现在有十几张表需要连,这种情况程序改怎么改呢?具体的程序如下:
Sub Query()
Dim Conn As Object, Rst As Object
Dim strConn As String, strSQL As String
Dim i As Integer, PathStr As String
Set Conn = CreateObject("ADODB.Connection")
Set Rst = CreateObject("ADODB.Recordset")
PathStr = "C:\Users\SQ\Desktop\自动化系统设备选型清单.xlsx" '设置工作簿的完整路径和名称
t = ActiveSheet.Name
Select Case Application.Version * 1 '设置连接字符串,根据版本创建连接
Case Is <= 11
strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr
Case Is >= 12
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathStr & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
End Select
'设置SQL查询语句
strSQL = "select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [01PLC$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [02上位机软件$]union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [03断路器$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [04UPS$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [05网络设备$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [06机柜及其附件$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [07电脑及服务器$]union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [08视频监控$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [09电子围栏$]union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [10门禁$]" ‘此处只能连十个,超过后就报错了!
Conn.Open strConn '打开数据库链接
Set Rst = Conn.Execute(strSQL) '执行查询,并将结果输出到记录集对象
'ThisWorkbook.Sheets.Add after:=Worksheets("数据源") '添加一个新工作表在第一工作后
For Each ws In Worksheets
If ws.Name = "提数结果" Then ws.Delete
Next
Sheets.Add.Name = "提数结果"
With Sheets("提数结果")
.Cells.Clear
.Tab.Color = 255
For i = 0 To Rst.Fields.Count - 1 '填写标题
.Cells(1, i + 1) = Rst.Fields(i).Name
Next i
.Range("A2").CopyFromRecordset Rst
' .Cells.EntireColumn.AutoFit '自动调整列宽
End With
Rst.Close '关闭数据库连接
Conn.Close
Set Conn = Nothing
Set Rst = Nothing
End Sub
Sub Query()
Dim Conn As Object, Rst As Object
Dim strConn As String, strSQL As String
Dim i As Integer, PathStr As String
Set Conn = CreateObject("ADODB.Connection")
Set Rst = CreateObject("ADODB.Recordset")
PathStr = "C:\Users\SQ\Desktop\自动化系统设备选型清单.xlsx" '设置工作簿的完整路径和名称
t = ActiveSheet.Name
Select Case Application.Version * 1 '设置连接字符串,根据版本创建连接
Case Is <= 11
strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr
Case Is >= 12
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathStr & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
End Select
'设置SQL查询语句
strSQL = "select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [01PLC$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [02上位机软件$]union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [03断路器$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [04UPS$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [05网络设备$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [06机柜及其附件$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [07电脑及服务器$]union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [08视频监控$] union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [09电子围栏$]union all select 类别,系列,名称,型号订货号,规格参数,数量,单位,单价,总价,品牌,备注 from [10门禁$]" ‘此处只能连十个,超过后就报错了!
Conn.Open strConn '打开数据库链接
Set Rst = Conn.Execute(strSQL) '执行查询,并将结果输出到记录集对象
'ThisWorkbook.Sheets.Add after:=Worksheets("数据源") '添加一个新工作表在第一工作后
For Each ws In Worksheets
If ws.Name = "提数结果" Then ws.Delete
Next
Sheets.Add.Name = "提数结果"
With Sheets("提数结果")
.Cells.Clear
.Tab.Color = 255
For i = 0 To Rst.Fields.Count - 1 '填写标题
.Cells(1, i + 1) = Rst.Fields(i).Name
Next i
.Range("A2").CopyFromRecordset Rst
' .Cells.EntireColumn.AutoFit '自动调整列宽
End With
Rst.Close '关闭数据库连接
Conn.Close
Set Conn = Nothing
Set Rst = Nothing
End Sub