Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 4975|回复: 17

[已解决]SQL调用程序出不了数据

[复制链接]
发表于 2014-5-5 18:39 | 显示全部楼层 |阅读模式
SQL调用程序出不了数据。
把    'On Error Resume Next 注释掉,发现:
'strSQL = "select 姓名,身份证号 from [" & s & "]" '把这句去掉就出错了,但是不去掉,就等于说上面“载入数据”这个程序是多余的。没有实现调用的过程,不知问题出在哪里?????????

最佳答案
2014-5-5 22:25
  1. Dim strSQL, S, myPath$, OutputSheet$, OutputRange$
  2. Sub 载入数据()
  3.     strSQL = "select 姓名,身份证号 from "
  4.     '条件 = "WHERE 姓名='王丽力'"
  5.     OutputSheet = "结果"
  6.     OutputRange = "A2"
  7.     Call subProgram(strSQL, Pathstr, OutputSheet, OutputRange, "WHERE 姓名='王丽力'")    '调用子程序
  8.     MsgBox "OK"
  9. End Sub

  10. Sub subProgram(ByVal strSQL$, ByVal myPath$, ByVal OutputSheet$, ByVal OutputRange$, strCondition$)    '子程序
  11.     Dim cnn As Object, Rst As Object, rs As Object
  12.     Dim strConn As String
  13.     Dim i As Integer, j%, Pathstr, S$, t$, sProvider$
  14.     Pathstr = Application.GetOpenFilename(fileFilter:="Excel文件(*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="打开Excel文件", MultiSelect:=True)    '选择多个EXCCEL 文件
  15.     If TypeName(Pathstr) = "Boolean" Then Exit Sub
  16.     Application.ScreenUpdating = False
  17.     Select Case Application.Version * 1
  18.         Case Is <= 11
  19.             sProvider = "Provider = Microsoft.Jet.Oledb.4.0;Extended Properties ='Excel 8.0';Data Source ="
  20.         Case Is >= 12
  21.             sProvider = "Provider = Microsoft.Ace.Oledb.12.0;Extended Properties ='Excel 12.0';Data Source ="
  22.     End Select
  23.     Cells.ClearContents
  24.     On Error Resume Next    '
  25.     myPath = S
  26.     For i = 1 To UBound(Pathstr)
  27.         If Pathstr(i) <> ThisWorkbook.FullName Then
  28.             Set cnn = CreateObject("ADODB.Connection")
  29.             cnn.Open sProvider & Pathstr(i)
  30.             Set rs = cnn.OpenSchema(20)
  31.             Do Until rs.EOF
  32.                 If rs.Fields("TABLE_TYPE") = "TABLE" Then
  33.                     S = Replace(rs("TABLE_NAME").Value, "'", "")
  34.                     If Right(S, 1) = "$" Then

  35.                         Set Rst = cnn.Execute(strSQL & " [" & S & "] " & strCondition)
  36.                         If Err.Number = 0 Then
  37.                             m = m + 1
  38.                             If m = 1 Then
  39.                                 For j = 0 To Rst.Fields.Count - 1
  40.                                     Cells(1, j + 1) = Rst.Fields(j).Name
  41.                                 Next
  42.                                 Range("A2").CopyFromRecordset Rst
  43.                             Else
  44.                                 Range("A65536").End(xlUp).Offset(1).CopyFromRecordset Rst
  45.                             End If
  46.                             Exit Do
  47.                         Else
  48.                             Err.Clear
  49.                         End If
  50.                     End If
  51.                 End If
  52.                 rs.MoveNext
  53.             Loop
  54.         End If
  55.     Next
  56.     Cells.EntireColumn.AutoFit
  57.     Rst.Close
  58.     rs.Close
  59.     cnn.Close
  60.     Set cnn = Nothing
  61.     Set rs = Nothing
  62.     Set Rst = Nothing
  63.     Application.ScreenUpdating = True
  64. End Sub
复制代码

出不了数据.rar

488.69 KB, 下载次数: 9

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-5-5 21:28 | 显示全部楼层
本帖最后由 hwc2ycy 于 2014-5-5 21:32 编辑

strSQL是空的,肯定要报错。

你查别的表,该表里不是不有这些字段,这都要考虑到的,否则你就要考虑到容错机制。

回复

使用道具 举报

 楼主| 发表于 2014-5-5 21:34 | 显示全部楼层
hwc2ycy 发表于 2014-5-5 21:28
strSQL是空的,肯定要报错。

你查别的表,该表里不是不有这些字段,这都要考虑到的,否则你就要考虑到容 ...

where 语句放在哪里?设置不对的。
  1. Dim strSQL, S, myPath$, OutputSheet$, OutputRange$
  2. Sub 载入数据()
  3.     strSQL = "select 姓名,身份证号 from "
  4.     条件 = "WHERE 姓名='王丽力'"
  5.     OutputSheet = "结果"
  6.     OutputRange = "A2"
  7.     Call subProgram(strSQL, Pathstr, OutputSheet, OutputRange) '调用子程序
  8.     MsgBox "OK"
  9. End Sub

  10. Sub subProgram(ByVal strSQL$, ByVal myPath$, ByVal OutputSheet$, ByVal OutputRange$) '子程序
  11.     Dim cnn As Object, Rst As Object, rs As Object
  12.     Dim strConn As String
  13.     Dim i As Integer, j%, Pathstr, S$, t$, sProvider$
  14.     Pathstr = Application.GetOpenFilename(fileFilter:="Excel文件(*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="打开Excel文件", MultiSelect:=True) '选择多个EXCCEL 文件
  15.     If TypeName(Pathstr) = "Boolean" Then Exit Sub
  16.     Application.ScreenUpdating = False
  17.     Select Case Application.Version * 1
  18.         Case Is <= 11
  19.             sProvider = "Provider = Microsoft.Jet.Oledb.4.0;Extended Properties ='Excel 8.0';Data Source ="
  20.         Case Is >= 12
  21.             sProvider = "Provider = Microsoft.Ace.Oledb.12.0;Extended Properties ='Excel 12.0';Data Source ="
  22.     End Select
  23.     Cells.ClearContents
  24.     On Error Resume Next '
  25.     myPath = S
  26.     For i = 1 To UBound(Pathstr)
  27.         If Pathstr(i) <> ThisWorkbook.FullName Then
  28.             Set cnn = CreateObject("ADODB.Connection")
  29.             cnn.Open sProvider & Pathstr(i)
  30.             Set rs = cnn.OpenSchema(20)
  31.             Do Until rs.EOF
  32.                 If rs.Fields("TABLE_TYPE") = "TABLE" Then
  33.                     S = Replace(rs("TABLE_NAME").Value, "'", "")
  34.                     If Right(S, 1) = "$" Then
  35.                      
  36.                         Set Rst = cnn.Execute(strSQL & "[" & S & "] & 条件 & ")
  37.                         If Err.Number = 0 Then
  38.                             m = m + 1
  39.                             If m = 1 Then
  40.                                 For j = 0 To Rst.Fields.Count - 1
  41.                                     Cells(1, j + 1) = Rst.Fields(j).Name
  42.                                 Next
  43.                                 Range("A2").CopyFromRecordset Rst
  44.                             Else
  45.                                 Range("A65536").End(xlUp).Offset(1).CopyFromRecordset Rst
  46.                             End If
  47.                             Exit Do
  48.                         Else
  49.                             Err.Clear
  50.                         End If
  51.                     End If
  52.                 End If
  53.                 rs.MoveNext
  54.             Loop
  55.         End If
  56.     Next
  57.     Cells.EntireColumn.AutoFit
  58.     Rst.Close
  59.     rs.Close
  60.     cnn.Close
  61.     Set cnn = Nothing
  62.     Set rs = Nothing
  63.     Set Rst = Nothing
  64.     Application.ScreenUpdating = True
  65. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-5-5 21:35 | 显示全部楼层
第4句,37句 ,要怎么修改?
回复

使用道具 举报

发表于 2014-5-5 21:36 | 显示全部楼层
  1. Sub subProgram(ByVal SQL$, ByVal myPath$, ByVal OutputSheet$, ByVal OutputRange$)    '子程序
  2.     Dim cnn As Object, Rst As Object, rs As Object
  3.     Dim strConn As String, strSQL As String
  4.     Dim i As Integer, j%, Pathstr, s$, t$, sProvider$
  5.     Pathstr = Application.GetOpenFilename(fileFilter:="Excel文件(*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="打开Excel文件", MultiSelect:=True)    '选择多个EXCCEL 文件
  6.     If TypeName(Pathstr) = "Boolean" Then Exit Sub
  7.     Application.ScreenUpdating = False
  8.     Select Case Application.Version * 1
  9.         Case Is <= 11
  10.             sProvider = "Provider = Microsoft.Jet.Oledb.4.0;Extended Properties =Excel 8.0;Data Source ="
  11.         Case Is >= 12
  12.             sProvider = "Provider = Microsoft.Ace.Oledb.12.0;Extended Properties =Excel 12.0;Data Source ="
  13.     End Select
  14.     Cells.ClearContents
  15.     'On Error Resume Next
  16.     myPath = s
  17.    
  18.     For i = 1 To UBound(Pathstr)
  19.         If Pathstr(i) <> ThisWorkbook.FullName Then
  20.             Set cnn = CreateObject("ADODB.Connection")
  21.             cnn.Open sProvider & Pathstr(i)
  22.             Set rs = cnn.OpenSchema(20)
  23.             Do Until rs.EOF
  24.                 If rs.Fields("TABLE_TYPE") = "TABLE" Then
  25.                     s = Replace(rs("TABLE_NAME").Value, "'", "")
  26.                     If Right(s, 1) = "$" Then
  27.                         strSQL = "select 姓名,身份证号 from [" & s & "]"    '把这句去掉就出错了,但是不去掉,就等于说上面“载入数据”这个程序是多余的。
  28.                         '不知问题出在哪里?????????
  29.                         On Error Resume Next
  30.                         Set Rst = cnn.Execute(strSQL)
  31.                         If Err.Number = 0 Then
  32.                             m = m + 1
  33.                             If m = 1 Then
  34.                                 For j = 0 To Rst.Fields.Count - 1
  35.                                     Cells(1, j + 1) = Rst.Fields(j).Name
  36.                                 Next
  37.                                 Range("A2").CopyFromRecordset Rst
  38.                             Else
  39.                                 Range("A65536").End(xlUp).Offset(1).CopyFromRecordset Rst
  40.                                 Exit Do
  41.                             End If
  42.                            
  43.                         Else
  44.                             Err.Clear
  45.                         End If
  46.                     End If
  47.                 End If
  48.                 rs.MoveNext
  49.             Loop
  50.         End If
  51.     Next
  52.     Cells.EntireColumn.AutoFit
  53.     Rst.Close
  54.     rs.Close
  55.     cnn.Close
  56.     Set cnn = Nothing
  57.     Set rs = Nothing
  58.     Set Rst = Nothing
  59.     Application.ScreenUpdating = True
  60. End Sub
复制代码
EXIT DO放错位置了。
回复

使用道具 举报

 楼主| 发表于 2014-5-5 21:40 | 显示全部楼层
hwc2ycy 发表于 2014-5-5 21:36
EXIT DO放错位置了。

子程序中还写入SQL语句,就是多此一举了。strSQL = "select 姓名,身份证号 from [" & s & "]" 如果是这样就根本不用调用了。
回复

使用道具 举报

发表于 2014-5-5 21:41 | 显示全部楼层
上面的我弄错了,提早跳出了循环,更不对。
  1. Sub subProgram(ByVal SQL$, ByVal myPath$, ByVal OutputSheet$, ByVal OutputRange$)    '子程序
  2.     Dim cnn As Object, Rst As Object, rs As Object
  3.     Dim strConn As String, strSQL As String
  4.     Dim i As Integer, j%, Pathstr, s$, t$, sProvider$
  5.     Pathstr = Application.GetOpenFilename(fileFilter:="Excel文件(*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="打开Excel文件", MultiSelect:=True)    '选择多个EXCCEL 文件
  6.     If TypeName(Pathstr) = "Boolean" Then Exit Sub
  7.     Application.ScreenUpdating = False
  8.     Select Case Application.Version * 1
  9.         Case Is <= 11
  10.             sProvider = "Provider = Microsoft.Jet.Oledb.4.0;Extended Properties =Excel 8.0;Data Source ="
  11.         Case Is >= 12
  12.             sProvider = "Provider = Microsoft.Ace.Oledb.12.0;Extended Properties =Excel 12.0;Data Source ="
  13.     End Select
  14.     Cells.ClearContents
  15.     'On Error Resume Next
  16.     myPath = s
  17.    
  18.     For i = 1 To UBound(Pathstr)
  19.         If Pathstr(i) <> ThisWorkbook.FullName Then
  20.             Set cnn = CreateObject("ADODB.Connection")
  21.             cnn.Open sProvider & Pathstr(i)
  22.             Set rs = cnn.OpenSchema(20)
  23.             Do Until rs.EOF
  24.                 If rs.Fields("TABLE_TYPE") = "TABLE" Then
  25.                     s = Replace(rs("TABLE_NAME").Value, "'", "")
  26.                     If Right(s, 1) = "$" Then
  27.                         strSQL = "select 姓名,身份证号 from [" & s & "]"    '把这句去掉就出错了,但是不去掉,就等于说上面“载入数据”这个程序是多余的。
  28.                         '不知问题出在哪里?????????
  29.                         On Error Resume Next
  30.                         Set Rst = cnn.Execute(strSQL)
  31.                         If Err.Number = 0 Then
  32.                             m = m + 1
  33.                             If m = 1 Then
  34.                                 For j = 0 To Rst.Fields.Count - 1
  35.                                     Cells(1, j + 1) = Rst.Fields(j).Name
  36.                                 Next
  37.                                 Range("A2").CopyFromRecordset Rst
  38.                             Else
  39.                                 Range("A65536").End(xlUp).Offset(1).CopyFromRecordset Rst
  40.                                End If
  41.                         Else
  42.                             Err.Clear
  43.                            End If
  44.                     End If
  45.                 End If
  46.                 rs.MoveNext
  47.             Loop
  48.         End If
  49.     Next
  50.     Cells.EntireColumn.AutoFit
  51.     Rst.Close
  52.     rs.Close
  53.     cnn.Close
  54.     Set cnn = Nothing
  55.     Set rs = Nothing
  56.     Set Rst = Nothing
  57.     Application.ScreenUpdating = True
  58. End Sub
复制代码
回复

使用道具 举报

发表于 2014-5-5 21:43 | 显示全部楼层
另外,好像指定的写入工作表,单元格参数还没有用到吧?
回复

使用道具 举报

 楼主| 发表于 2014-5-5 21:47 | 显示全部楼层
hwc2ycy 发表于 2014-5-5 21:43
另外,好像指定的写入工作表,单元格参数还没有用到吧?

现在不知道where 语句怎么设置。设置在哪里。
回复

使用道具 举报

发表于 2014-5-5 21:49 | 显示全部楼层
strSQL = "select 姓名,身份证号 from [" & s & "]"
在这后面再添加WHERE子句。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 17:07 , Processed in 0.521096 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表