在vb里进行SQL查询时总是提示语句错误,能帮我看下这代码哪里有问题吗?
Dim strSQL As String
Dim strCon(7) As StringDim i As Integer
If Text1.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(1) = "货物申请表.申请单号 like '%" & Text1.Text & "%'"
    Else
        strCon(1) = "货物申请表.申请单号='" & Text1.Text & "'"
    End If
Else
    strCon(1) = ""
End If
If Text2.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(2) = "货物申请表.运单号 like '%" & Text2.Text & "%'"
    Else
        strCon(2) = "货物申请表.运单号='" & Text2.Text & "'"
    End IfElse
    strCon(2) = ""
End If
If Text3.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(3) = "客户信息表.地址 like '%" & Text3.Text & "%'"
    Else
        strCon(3) = "客户信息表.地址 ='" & Text3.Text & "'"
    End IfElse
    strCon(3) = ""
End IfIf Text4.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(4) = "货物申请表.收件人地址 like '%" & Text4.Text & "%'"
    Else
        strCon(4) = "货物申请表.收件人地址='" & Text4.Text & "'"
    End If
Else
    strCon(4) = ""
End IfIf Text5.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(5) = "客户信息表.客户姓名 like '%" & Text5.Text & "%'"
    Else
        strCon(5) = "客户信息表.客户姓名='" & Text5.Text & "'"
    End If
Else
    strCon(5) = ""
End If
If Text6.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(6) = "货物申请表.收件人姓名 like '%" & Text6.Text & "%'"
    Else
        strCon(6) = "货物申请表.收件人姓名='" & Text6.Text & "'"
    End If
Else
    strCon(6) = ""
End If
 If Combo2.Text = "根据申请单号查询" Then
  If Text7.Text <> "" Then
     strCon(7) = "货物申请表.申请单号 ='" & Text7.Text & "'"
  Else
     strCon(7) = ""
  End If
End IfIf Combo1.Text = "根据目的地查询" Then
  If Text7.Text <> "" Then
     strCon(7) = "货物申请表.收件人地址='" & Text7.Text & "'"
  Else
     strCon(7) = ""
  End If
End IfIf Combo2.Text = "根据发件人查询" Then
  If Text7.Text <> "" Then
     strCon(7) = "客户信息表.客户姓名 ='" & Text7.Text & "'"
  Else
     strCon(7) = ""
  End If
End IfIf Combo2.Text = "根据收件人查询" Then
  If Text7.Text <> "" Then
     strCon(7) = "货物申请表.收件人姓名 ='" & Text7.Text & "'"
  Else
     strCon(7) = ""
  End If
End IfIf Combo2.Text = "根据快件状态查询" Then
  If Text7.Text <> "" Then
     strCon(7) = "在途跟踪信息表.运输状态 ='" & Text7.Text & "'"
  Else
     strCon(7) = ""
  End If
End If    
    For i = 1 To 7
       If strCon(i) <> "" Then
           strSQL = strSQL + " and " + strCon(i)
       End If
   Next   strSQL = "select 货物申请表.申请单号,货物申请表.运单号,客户信息表.地址," _
& " 货物申请表.收件人地址,客户信息表.客户姓名,货物申请表.收件人姓名,在途跟踪信息表.运输状态 from 货物申请表,在途跟踪信息表,客户信息表" _
& " where 货物申请表.申请单号 = 在途跟踪信息表.申请单号 and 货物申请表.运单号 = 在途跟踪信息表.运单号"
    Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=物流管理;Initial Catalog=W-L"
    Adodc1.CursorLocation = adUseClient
    Adodc1.CommandType = adCmdText
     Adodc1.RecordSource = strSQL
    Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1

解决方案 »

  1.   

    Debug.Print strSQL
    然后把语句贴到 SSMS 的查询窗口中执行,自然会指出错误行。
    通过手工分行来确定错误位置。
      

  2.   

    Sql查询句写法貌似有错误。
      

  3.   

    SQL查询语言主要结构为: 
    Select 查询字段 from 表名 Where 查询条件语句 [排序语句或分组语句] 
      

  4.   

    将SQL语句打印出来,复制粘贴到查询分析器里面手动运行,必要的话还可以单步调试,无误后再继续。
      

  5.   

    竟然用中文命名。。 尝试删掉一些sql语句看看吧。 估计是你表和字段搞错了,比如某个字段根本不存在于这个表中,你还去.它
      

  6.   

       strSQL = "select 货物申请表.申请单号,货物申请表.运单号,客户信息表.地址," _
    & " 货物申请表.收件人地址,客户信息表.客户姓名,货物申请表.收件人姓名,在途跟踪信息表.运输状态 from 货物申请表,在途跟踪信息表,客户信息表" _
    & " where 货物申请表.申请单号 = 在途跟踪信息表.申请单号 and 货物申请表.运单号 = 在途跟踪信息表.运单号 and 货物申请表.客户ID = 客户信息表.客户ID"没有给出客户信息表的关联条件。
      

  7.   

    路过学习一下,刚好有个项目会涉及到SQL数据库。