窗体上有六个标签,标签分别为数据库中记录的各个字段。标签为控件数组。相对应的还有六个文本框,也是控件数组,分别放查找条件,还有一个命令按钮。点击进行查询。即从图书信息表中查找图书信息.
我是这样想的,查询的时候首先判断六个文本框中是否有查询条件,若有则用AND连接起来。然后再查询显示在网格控件中
我想请教一下,ztj = tz & "="&'"&trim(ty)&"'        ztj = ztj & "and" & tz & "=" & '& " & Trim(ty) & "&'&"
上面这两句条件连接的语句该如何写。
        
       Private Sub Form_Load()
Set con = New ADODB.Connection
str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\桌面\图书管理系统\图书管理系统.mdb;Persist Security Info=False"
con.Open str
Set rs = con.Execute("select * from 图书信息表  ")
End SubPrivate Sub Command1_Click
Dim a As Integer
     For i = 0 To 5
     If Text1(i) <> "" Then
      a = a + 1
       If a = 1 Then
       tz = Label1(i).Caption      记录字段的名称
       ty = Text1(i).Text        字段的值
       ztj = tz & "="&'"&trim(ty)&"'   连接起来,字段=值
        Else
        tz = Label1(i).Caption
       ty = Text1(i).Text
        ztj = ztj & "and" & tz & "=" & '& " & Trim(ty) & "&'&"
       End If
      End If
     Next i
End Select
       q = "select * from 图书信息表 where"
  Set rs = con.Execute(q & ztj)
  Set mg1.DataSource = rs

解决方案 »

  1.   

    dim ss as string
    dim sql as string
    if len(text1(1))>0 then ss= Label1.caption & text1(1).text 
    if len(text1(2))>0  and ss<>"" then ss="and" & Label2.caption & text1(2).text
    elseif  ss=""
    ss=Label2.caption & text1(2).text
    end if
    ......
    if len(text1(6))>0 and ss<>'' then ss="and " & Label6.caption & text1(6).textsql="select * from 图书信息表 where" & ss
      

  2.   

    不行啊,我改了一下,改为如下。可还是总提示from子句语法错误。头都大了。又是哪里的错啊。好心人邦忙Private Sub Command1_Click(Index As Integer)
    Select Case Index
        Case 0
        Dim a As Integer
        For i = 0 To 5
         If Text1(i) <> "" Then
          a = a + 1               判断条件个数
           If a = 1 Then
           ztj = "Label1(i).Caption ='" & Text1(i).Text & "'"   第一个条件不需要“AND”
         
           Else
           ztj = ztj & "and " & Label1(i).Caption & "=" & Text1(i).Text  二个或二个以上用AND                                      连接
          End If
        End If
        Next i
      
       Set rs = con.Execute("select * from 图书信息表 where" & ztj)
    End Select  
    End Sub
      

  3.   

    Private Sub Command1_Click(Index As Integer)
    Select Case Index
        Case 0
        Dim a As Integer
        For i = 0 To 5
         If Text1(i) <> "" Then
          a = a + 1              
           If a = 1 Then
           ztj = Label1(i).Caption & "=" & "'" & Text1(i).Text & "'"       
           Else
           ztj = ztj & "and " & Label1(i).Caption & "=" & "'" & Text1(i).Text &"'"        End If
        End If
        Next i
      
       Set rs = con.Execute("select * from 图书信息表 where" & ztj)
    End Select  
    End Sub