Excel精英培训网

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

[已解决]FROM子句语法错误

[复制链接]
发表于 2012-8-31 11:04 | 显示全部楼层 |阅读模式
FROM子句语法错误
最佳答案
2012-8-31 11:49
strsql = "select * from 核算表 WHERE "
If lx <> "" Then
strsql = strsql & "产品类型 like '" & lx & "'and "
End If
If xm <> "" Then
strsql = strsql & "项目号 like '" & xm & "'and "
End If
If ht <> "" Then
strsql = strsql & "合同号 like '" & ht & "'and "
End If
If kh <> "" Then
strsql = strsql & "客户 like '" & kh & "'and "
End If
If gys <> "" Then
strsql = strsql & "供货商 like '" & gys & "'and "
End If
If xh <> "" Then
strsql = strsql & "型号 like '" & xh & "'and "
End If
l = Len(strsql)
strsql = Left(strsql, l - 4)

合同管理.rar

63.26 KB, 下载次数: 37

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-8-31 11:40 | 显示全部楼层
这个问题是VBA 问题,请发到VBA版块!
回复

使用道具 举报

 楼主| 发表于 2012-8-31 11:40 | 显示全部楼层
回复

使用道具 举报

发表于 2012-8-31 11:49 | 显示全部楼层    本楼为最佳答案   
strsql = "select * from 核算表 WHERE "
If lx <> "" Then
strsql = strsql & "产品类型 like '" & lx & "'and "
End If
If xm <> "" Then
strsql = strsql & "项目号 like '" & xm & "'and "
End If
If ht <> "" Then
strsql = strsql & "合同号 like '" & ht & "'and "
End If
If kh <> "" Then
strsql = strsql & "客户 like '" & kh & "'and "
End If
If gys <> "" Then
strsql = strsql & "供货商 like '" & gys & "'and "
End If
If xh <> "" Then
strsql = strsql & "型号 like '" & xh & "'and "
End If
l = Len(strsql)
strsql = Left(strsql, l - 4)
回复

使用道具 举报

发表于 2012-8-31 11:51 | 显示全部楼层
注意你的空格 AND 等等,字符数,

strsql = Left(strsql, l - 4) ‘下面加一句
MsgBox strsql’看一下组合结果,是否正确!
回复

使用道具 举报

发表于 2012-8-31 12:07 | 显示全部楼层

As String

本帖最后由 mxg825 于 2012-8-31 12:18 编辑

帮你优化一下 IF 过程(做成一个子程序)
  1. Private Sub CommandButton1_Click()
  2. Dim cnn As New ADODB.Connection
  3. Dim rst As New ADODB.Recordset
  4. Dim xm As String, ht As String, kh As String
  5. Dim gys As String, lx As String, xh As String
  6. Dim stpath As String, strsql As String
  7. Dim l As Integer, n As Integer
  8. stpath = ThisWorkbook.Path & Application.PathSeparator & "fish.mdb"
  9. cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & stpath
  10. xm = ComboBox1.Text
  11. ht = ComboBox2.Text
  12. lx = ComboBox3.Text
  13. xh = ComboBox4.Text
  14. kh = ComboBox5.Text
  15. gys = ComboBox6.Text
  16.     strsql = "select * from 核算表 WHERE "
  17.     strsql = strsql & sqlAnd("产品类型", lx) '调用子程序组合
  18.     strsql = strsql & sqlAnd("项目号", xm)
  19.     strsql = strsql & sqlAnd("合同号", ht)
  20.     strsql = strsql & sqlAnd("客户", kh)
  21.     strsql = strsql & sqlAnd("供货商", gys)
  22.     strsql = strsql & sqlAnd("型号", xh)
  23. l = Len(strsql)
  24. strsql = Left(strsql, l - 5)
  25. MsgBox strsql
  26. rst.Open strsql, cnn
  27. n = Sheet2.Range("B65536").End(xlUp).Row
  28. Sheet2.Rows("5:" & n).ClearContents
  29. Sheet2.[B5].CopyFromRecordset rst
  30. rst.Close
  31. cnn.Close
  32. Set TST = Nothing
  33. End Sub
  34. Function sqlAnd(fieldName As String, Tet As String) As String
  35. If Len(Tet) > 0 Then
  36.     sqlADD = fieldName & " Like '" & Tet & "' and "
  37. End If
  38. End Function
复制代码
帮你优化一下 IF 过程(做成一个子程序)

评分

参与人数 1 +18 收起 理由
无聊的疯子 + 18 赞一个!值得学习的思路!!

查看全部评分

回复

使用道具 举报

发表于 2012-8-31 12:14 | 显示全部楼层
Private Sub CommandButton1_Click()

    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim xm As String, ht As String, kh As String
    Dim gys As String, lx As String, xh As String
    Dim stpath As String, strsql As String
    Dim l As Integer, n As Integer
    stpath = ThisWorkbook.Path & Application.PathSeparator & "fish.mdb"
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & stpath
    xm = ComboBox1.Text
    ht = ComboBox2.Text
    lx = ComboBox3.Text
    xh = ComboBox4.Text
    kh = ComboBox5.Text
    gys = ComboBox6.Text
    strsql = "select * from 核算表 WHERE"
    If lx <> "" Then
        strsql = strsql & " 产品类型 like """ & lx & """ and "
    End If
    If xm <> "" Then
        strsql = strsql & " 项目号 LIKE """ & xm & """ and"
    End If
    If ht <> "" Then
        strsql = strsql & " 合同号 LIKE """ & ht & """ and"
    End If
    If kh <> "" Then
        strsql = strsql & " 客户 LIKE """ & kh & """ and"
    End If
    If gys <> "" Then
        strsql = strsql & " 供货商 LIKE """ & gys & """ and"
    End If
    If xh <> "" Then
        strsql = strsql & " (型号 LIKE """ & xh & """)"
    End If
    'l = Len(strsql)
    'strsql = Left(strsql, l - 5)
    rst.Open strsql, cnn
    n = Sheet2.Range("B65536").End(xlUp).Row
    Sheet2.Rows("5:" & n).ClearContents
    Sheet2.[B5].CopyFromRecordset rst
    rst.Close
    cnn.Close
    Set TST = Nothing
End Sub

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-29 03:59 , Processed in 0.351972 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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