Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: jianwu1106

[已解决]SQL语句 请高手看看为什么会出现错误?

[复制链接]
 楼主| 发表于 2013-7-19 23:24 | 显示全部楼层
hwc2ycy 发表于 2013-7-19 23:13
表1里哪有加工号这一列数据呢。

加工号有的 但还没加进去
这个是我以前的问题一直没搞定,http://www.excelpx.com/thread-304789-1-1.html
回复

使用道具 举报

 楼主| 发表于 2013-7-19 23:27 | 显示全部楼层
hwc2ycy 发表于 2013-7-19 23:13
另外,如果想用SQL做好,凡是要查询到的数据列全部要填上。

数据列全部要填上 一定要填上吗?加工号有些要后期才能补上
回复

使用道具 举报

 楼主| 发表于 2013-7-19 23:30 | 显示全部楼层
hwc2ycy 发表于 2013-7-19 23:15
HEADER=TRUE,另外,很多数据是空值,没法显示完整的,转置还要出错,否则又要循环生成数组。

数据的不 ...

版主麻烦你帮我改一下代码吧,
回复

使用道具 举报

 楼主| 发表于 2013-7-19 23:33 | 显示全部楼层
hwc2ycy 发表于 2013-7-19 23:13
另外,如果想用SQL做好,凡是要查询到的数据列全部要填上。

如果是空的能不能用空格代替
回复

使用道具 举报

发表于 2013-7-19 23:36 | 显示全部楼层
jianwu1106 发表于 2013-7-19 23:33
如果是空的能不能用空格代替

等会吧,不行就只有明天了,有点忙了。
回复

使用道具 举报

 楼主| 发表于 2013-7-20 02:30 | 显示全部楼层
hwc2ycy 发表于 2013-7-19 23:36
等会吧,不行就只有明天了,有点忙了。

好的 麻烦版主写一下代码了
回复

使用道具 举报

发表于 2013-7-20 08:57 | 显示全部楼层    本楼为最佳答案   
组合框查询.rar (86.91 KB, 下载次数: 10)
回复

使用道具 举报

发表于 2013-7-20 17:58 | 显示全部楼层
'改用筛选的方法
  1. Dim AdoConn As Object, AdoRst As Object

  2. Private Sub UserForm_Initialize()
  3. '窗体初始化代码

  4.     Dim strFullname As String
  5.     Dim strsql As String

  6.     On Error GoTo ErrorHandler

  7.     strFullname = ThisWorkbook.Path & Application.PathSeparator & "Database\表1.xls"
  8.     If Len(Dir(strFullname)) = 0 Then
  9.         MsgBox strFullname & vbCrLf & "不存在", vbCritical + vbOKOnly
  10.         Exit Sub
  11.     End If

  12.     strsql = "select * from [sheet1$A3:f]"
  13.     If Not CreateConnection(strFullname, strsql) Then
  14.         MsgBox "数据连接建立失败"
  15.         Exit Sub
  16.     End If
  17.     If Query("公司货号 <> ''") Then
  18.         If AdoRst.RecordCount > 0 Then
  19.             '            Dim arr
  20.             '            arr = AdoRst.GetRows(Fields:=1)
  21.             Me.ComboBox1.List = WorksheetFunction.Transpose(AdoRst.GetRows(Fields:=1))
  22.         End If
  23.     End If

  24.     Exit Sub

  25. ErrorHandler:
  26.     MsgBox Err.Number & vbCrLf & Err.Description
  27.     Err.Clear
  28. End Sub

  29. Function CreateConnection(strFullname As String, strsql As String, Optional blnHasHeader As Boolean = True) As Boolean
  30. '建立数据连接

  31.     Const adUseClient = 3
  32.     Const adModeShareDenyWrite = 8
  33.     Const adModeReadWrite = 3
  34.     Const adModeRead = 1
  35.     Dim StrConn$

  36.     Set AdoConn = CreateObject("ADODB.Connection")

  37.     StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  38.               "Data Source='" & strFullname & "';Extended Properties='Excel 8.0;HDR=" & blnHasHeader & ";imex=1'"

  39.     'Debug.Print StrConn

  40.     On Error GoTo ErrorHandler

  41.     With AdoConn
  42.         .CommandTimeout = 5
  43.         .ConnectionTimeout = 5
  44.         .CursorLocation = adUseClient
  45.         .Mode = adModeRead    'Write    'adModeShareDenyWrite
  46.         .ConnectionString = StrConn
  47.         .Open
  48.     End With

  49.     Set AdoRst = AdoConn.Execute(strsql)
  50.     CreateConnection = True
  51.     '    Dim arr
  52.     '    arr = AdoRst.GetRows
  53.     '    Stop

  54.     Exit Function

  55. ErrorHandler:
  56.     MsgBox Err.Number & vbCrLf & Err.Description
  57.     Set AdoRst = Nothing
  58.     Set AdoConn = Nothing
  59. End Function

  60. Function Query(strCondition As String)
  61. '查询函数
  62.     On Error GoTo ErrorHandler
  63.     If AdoConn Is Nothing Then
  64.         Exit Function
  65.     End If

  66.     If AdoRst Is Nothing Then
  67.         Exit Function
  68.     End If

  69.     With AdoRst
  70.         .Filter = ""
  71.         .Filter = strCondition
  72.     End With
  73.     Query = True
  74.     Exit Function

  75. ErrorHandler:
  76.     MsgBox Err.Number & vbCrLf & Err.Description
  77.     Err.Clear
  78. End Function

  79. Private Sub ComboBox1_Change()
  80. '公司货号
  81.     If Len(Me.ComboBox1.Value) = 0 Then Exit Sub
  82.     'select 公司色号 from [sheet1$A3:f] where 公司货号='" & me.combobox1.value & "'"
  83.     If Not Query("公司货号 like '" & Me.ComboBox1.Value & "'") Then
  84.         MsgBox "查询失败"
  85.         Exit Sub
  86.     End If
  87.     Me.ComboBox3.Clear
  88.     With Me.ComboBox2
  89.         .Clear
  90.         If AdoRst.RecordCount Then .List = WorksheetFunction.Transpose(AdoRst.GetRows(Fields:=2))
  91.     End With

  92. End Sub

  93. Private Sub ComboBox2_Change()
  94. '公司色号
  95.     If Len(Me.ComboBox1.Value) And Len(Me.ComboBox2.Value) Then
  96.         If Not Query("公司货号 like '" & Me.ComboBox1.Value & "' and 公司色号 like '" & Me.ComboBox2.Value & "' and 加工号 <>''") Then
  97.             MsgBox "查询失败"
  98.             Exit Sub
  99.         End If
  100.         With Me.ComboBox3
  101.             .Clear
  102.             If AdoRst.RecordCount Then .List = WorksheetFunction.Transpose(AdoRst.GetRows(Fields:=3))
  103.         End With
  104.     End If

  105. End Sub
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-31 04:19 , Processed in 0.309481 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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