如图
我想运行初始时,datagrid中显示数据 ,当按下按钮时根据时间查询,将结果显示在datagrid中。
怎样联接数据,并显示。请帮忙。谢谢!
我写的代码:
Dim mrc As ADODB.Recordset
Dim mrc1 As ADODB.Recordset
Dim MsgText As String
Dim txtSQL As String
Private Sub Form_Load()
DataGrid1.Columns(0).Caption = "报障编号"
DataGrid1.Columns(1).Caption = "报障日期"
DataGrid1.Columns(2).Caption = "报障部门"
DataGrid1.Columns(3).Caption = "报障时间"
DataGrid1.Columns(4).Caption = "故障现象"
DataGrid1.Columns(0).Width = 800
DataGrid1.Columns(1).Width = 1200
DataGrid1.Columns(2).Width = 800
DataGrid1.Columns(3).Width = 800
DataGrid1.Columns(4).Width = 800
txtSQL = "select * from bzd"
Set mrc = ExecuteSQL(txtSQL, MsgText)
Find = Fals
End Sub
Public Sub showdata()
Dim j As Integer
Dim i As Integer
Set mrc = New ADODB.Recordset
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = False Then
mrc.MoveFirst
With DataGrid1
Do While Not mrc.EOF
.Columns = .Columns + 1
For i = 1 To mrc.Fields.Count
.DataGrid1.Columns(.Columns.Count, i) = mrc.Fields(i - 1)
Next i
.Columns.Count = .Columns.Count + 1
mrc.MoveNext
Loop
End With
Else
If Find = False Then
zzz = MsgBox("对不起,没有此学生的档案记录!", vbOKOnly, "查询")
End If
End If
Set mrc = Nothing
End Sub
Private Sub 执行查询_Click()
txtSQL = ""
If txtSQL <> "" Then
txtSQL = txtSQL & "and 报障日期>=#" & DTPicker1.Value & "# and 报障日期<=#" & DTPicker2.Value & "#"
Else
txtSQL = " 报障日期>=#" & DTPicker1.Value & "# and 报障日期<=#" & DTPicker2.Value & "#"
End If
Dim str As String
str = "select * from fkd where " & txtSQL
Set mrc = ExecuteSQL(str, MsgText)
If mrc.EOF = True Then
zzz = MsgBox("对不起,没有此月维修记录!", vbOKOnly, "查询")
Exit Sub
End If
txtSQL = "select 报障编号,报障日期,报障部门,报障时间,故障现象 from fkd where " & txtSQL & "order by 报障编号 desc"
showdata
End Sub