回复 8楼 xxwyyf007
根据你的要求,做个例子。程序代码:
Private Sub Command2_Click() '填写表头 MSHFlexGrid3.Cols = 5 MSHFlexGrid3.Col = 1 MSHFlexGrid3.Row = 0 MSHFlexGrid3.Text = "编号" MSHFlexGrid3.Col = 2 MSHFlexGrid3.Text = "规格" MSHFlexGrid3.Col = 3 MSHFlexGrid3.Text = "已生产" MSHFlexGrid3.Col = 4 MSHFlexGrid3.Text = "已包装" Set cn = New ADODB.Connection cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\rc.mdb" '////////////////////////// Set rs1 = New ADODB.Recordset rs1.Open "select 编号,规格,sum(数量) as 已生产 from sc group by 编号,规格 ", cn, adOpenStatic, adLockOptimistic If rs1.RecordCount > 0 Then Set MSHFlexGrid1.DataSource = rs1 MSHFlexGrid3.Rows = rs1.RecordCount + 1 '设置表格行数 Else Exit Sub End If '/////////////////////////// Set rs2 = New ADODB.Recordset rs2.Open "select 编号,sum(数量) as 已包装 from bz group by 编号 ", cn, adOpenStatic, adLockOptimistic If rs2.RecordCount > 0 Then Set MSHFlexGrid2.DataSource = rs2 Else Exit Sub End If '/////////////////////////// Dim i As Integer, j As Integer Do While Not rs1.EOF i = i + 1 MSHFlexGrid3.Row = i For j = 0 To 2 MSHFlexGrid3.Col = j + 1 MSHFlexGrid3.Text = rs1(j) Next MSHFlexGrid3.Col = 4 rs2.MoveFirst Do While Not rs2.EOF If rs1(0) = rs2(0) Then MSHFlexGrid3.Text = rs2(1) Exit Do Else MSHFlexGrid3.Text = 0 End If rs2.MoveNext Loop rs1.MoveNext Loop rs1.Close rs2.Close cn.Close Set rs1 = Nothing Set rs2 = Nothing Set cn = Nothing End Sub