连接ACCESS数据库,在VB建立一个窗体,有三个文本框和一个查询按钮打印按钮,在三个文本框中输入东西,点查询出现数据在该窗体上,文本框可以为空,可以单独查询,也可以输入二个或三个查询,查不到记录有提示,谢谢!请提供示例软件,发到我邮箱里[email protected],谢谢!补充一下,有一个文本框是组合框,自动导入数据库字段值

解决方案 »

  1.   

    连接ACCESS数据库,数据库有密码,在VB建立一个窗体,有三个文本框和一个查询按钮打印按钮,在三个文本框中输入东西,点查询出现数据在该窗体上,文本框可以为空,可以单独查询,也可以输入二个或三个查询,查不到记录有提示,谢谢!请提供示例软件,发到我邮箱里[email protected],谢谢!补充一下,有一个文本框是组合框,自动导入数据库字段值
      

  2.   

    Private Sub Command1_Click()
        Dim cn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        Dim strSql As String
        Dim i As Integer
        Dim j As Integer    cn.CursorLocation = adUseClient
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\mydb.mdb;Persist Security Info=False"    strSql = ""
        strSql = strSql & " select *     " & vbNewLine
        strSql = strSql & "   from mytable " & vbNewLine
        strSql = strSql & "  where 1=1   " & vbNewLine
        If Text1.Text <> "" Then
            strSql = strSql & "    and item1 = '" & Trim(Text1.Text) & "' " & vbNewLine
        End If    If Text2.Text <> "" Then
            strSql = strSql & "    and item2 = '" & Trim(Text2.Text) & "' " & vbNewLine
        End If    If Combo1.Text <> "" Then
            strSql = strSql & "    and item3 = '" & Trim(Combo1.Text) & "' " & vbNewLine
        End If
        rs.Open strSql, cn, adOpenStatic, adLockOptimistic    If rs.RecordCount > 0 Then
            With MSFlexGrid1
                .Rows = rs.RecordCount + 1            While Not rs.EOF
                    i = i + 1
                    For j = 1 To .Cols - 1
                        .TextMatrix(i, j) = rs.Fields(j - 1)
                    Next j                rs.MoveNext
                Wend
            End With
        Else
            MsgBox "没有数据"
        End If    rs.Close
        cn.Close
        Set rs = Nothing
        Set cn = Nothing
    End SubPrivate Sub Form_Load()
        Dim cn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        
        Combo1.Clear
        
        cn.CursorLocation = adUseClient
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\mydb.mdb;Persist Security Info=False"    rs.Open "select item3 from mytable group by item3", cn, adOpenStatic, adLockOptimistic    If rs.RecordCount > 0 Then
            With Combo1
                .AddItem ""
                
                While Not rs.EOF
                    .AddItem rs.Fields(0) & ""                rs.MoveNext
                Wend
            End With
        End If    rs.Close
        cn.Close
        Set rs = Nothing
        Set cn = Nothing
        
    End Sub
      

  3.   

    .TextMatrix(i, j) = rs.Fields(j - 1)
    加入我的数据库后,出现实时错误"94" 无效使用NULL,怎么改一下
      

  4.   

    .TextMatrix(i, j) = rs.Fields(j - 1) & "" 
      

  5.   

    要加载画面就全部显示?form_load里加
    CALL Command1_Click()
      

  6.   

    要加载画面就全部显示? form_load里加 
    CALL Command1_Click()不是这条语句,我的意思是刚开始显示窗体时,数据库的全部记录显示在表格中,点查询刚在显示查询记录,你现在是显示窗体时记录为空
      

  7.   

    Private Sub Command1_Click()
        Dim cn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        Dim strSql As String
        Dim i As Integer
        Dim j As Integer    cn.CursorLocation = adUseClient
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\油装置检定数据库.mdb;Persist Security Info=False"    strSql = ""
        strSql = strSql & " select *     " & vbNewLine
        strSql = strSql & "   from 信息表 " & vbNewLine
        strSql = strSql & "  where 1=1   " & vbNewLine
        If Text1.Text <> "" Then
            strSql = strSql & "    and 记录编号 = '" & Trim(Text1.Text) & "' " & vbNewLine
        End If    If Text2.Text <> "" Then
            strSql = strSql & "    and 送检单位 = '" & Trim(Text2.Text) & "' " & vbNewLine
        End If    If Combo1.Text <> "" Then
            strSql = strSql & "    and 流量计名称 = '" & Trim(Combo1.Text) & "' " & vbNewLine
        End If
        rs.Open strSql, cn, adOpenStatic, adLockOptimistic    With MSFlexGrid1
            .Rows = 1
            .Cols = 1
            
            If rs.RecordCount > 0 Then
                .Cols = rs.Fields.Count + 1
                .Rows = rs.RecordCount + 1            While Not rs.EOF
                    i = i + 1
                    For j = 1 To .Cols - 1
                        .TextMatrix(i, j) = rs.Fields(j - 1) & ""
                    Next j                rs.MoveNext
                Wend
            Else
                MsgBox "没有数据"
            End If
        End With    rs.Close
        cn.Close
        Set rs = Nothing
        Set cn = Nothing
    End SubPrivate Sub Form_Load()
        Dim cn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        
        Combo1.Clear
        Call MSFlexGrid1def
       ' Call Command1_Click
        cn.CursorLocation = adUseClient
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\油装置检定数据库.mdb;Persist Security Info=False"    rs.Open "select 流量计名称 from 信息表 group by 流量计名称", cn, adOpenStatic, adLockOptimistic    If rs.RecordCount > 0 Then
            With Combo1
                .AddItem ""
                
                While Not rs.EOF
                    .AddItem rs.Fields(0) & ""                rs.MoveNext
                Wend
            End With
        End If    rs.Close
        cn.Close
        Set rs = Nothing
        Set cn = Nothing
        
    End Sub
    Private Sub MSFlexGrid1def() '将list2的表头初始化  入库
      MSFlexGrid1.TextMatrix(0, 0) = "品名"
      MSFlexGrid1.TextMatrix(0, 1) = "规格"
      MSFlexGrid1.TextMatrix(0, 2) = "数量"
      MSFlexGrid1.TextMatrix(0, 3) = "单位"
      MSFlexGrid1.TextMatrix(0, 4) = "单价(元)"
      MSFlexGrid1.TextMatrix(0, 5) = "总额(元)"
      MSFlexGrid1.TextMatrix(0, 6) = "入库日期"
      MSFlexGrid1.TextMatrix(0, 7) = "入料人编号"
      MSFlexGrid1.TextMatrix(0, 8) = "入料人"
      MSFlexGrid1.TextMatrix(0, 9) = "经手人"
      MSFlexGrid1.TextMatrix(0, 10) = "说明"
      MSFlexGrid1.TextMatrix(0, 11) = "自动编号"
    End Sub
    品名下没有数据,一点查询别的字段没有显示,只有数据,刚开始显示窗体时,数据库的全部记录没有显示在表格中
      

  8.   

    品名
    在你的数据库中有吗?email发过来吧