i = MSFlexGrid1.Row j = MSFlexGrid1.Col
If j = 0 And Trim(Text5.Text) = \
MsgBox \物品名称不能为空\ Text5.SetFocus Exit Sub End If
If j = 1 And Not IsNumeric(Text5.Text) Then
MsgBox \单价请输入数字!\ Text5.SetFocus Exit Sub End If
If j = 2 And Not IsNumeric(Text5.Text) Then
MsgBox \数量请输入数字!\ Text5.SetFocus Exit Sub End If
If j = 3 And Trim(Text5.Text) = \
MsgBox \单位不能为空!\ Text5.SetFocus Exit Sub End If
If j = 3 And Not IsNull(Text5.Text) Then
MSFlexGrid1.Col = 1 '金额由程序算出 price = CDbl(MSFlexGrid1.Text) MSFlexGrid1.Col = 2
coun = CInt(MSFlexGrid1.Text) MSFlexGrid1.Col = 4
MSFlexGrid1.Text = price * coun
MSFlexGrid1.Col = MSFlexGrid1.Col + 1 Text5.Visible = False
setcombo2 MSFlexGrid1.Row, MSFlexGrid1.Col KeyAscii = 0 Exit Sub End If
MSFlexGrid1.Col = MSFlexGrid1.Col + 1 KeyAscii = 0
nextposition MSFlexGrid1.Row, MSFlexGrid1.Col End If Exit Sub texterror:
MsgBox Err.Description End Sub
51
Public Sub setcombo2(ByVal r As Integer, ByVal c As Integer) On Error GoTo seterror
Combo2.Width = MSFlexGrid1.CellWidth
Combo2.Left = MSFlexGrid1.Left + MSFlexGrid1.ColPos(c) Combo2.Top = MSFlexGrid1.Top + MSFlexGrid1.RowPos(r) Combo2.Text = MSFlexGrid1.Text Combo2.Visible = True Combo2.SetFocus Exit Sub seterror:
MsgBox Err.Description End Sub
12.4.7
Option Explicit
Dim rs_data2 As New ADODB.Recordset Dim select_row As String Dim showgrid2 As Boolean
Private Sub Form_Load() On Error GoTo loaderror
displaygrid1 '调用显示Datagrid1子程序 setgrid2head loaderror:
If Err.Number <> 0 Then MsgBox Err.Description End If End Sub
Public Sub displaygrid1() '显示msflexgrid1子程序 Dim i As Integer
On Error GoTo displayerror setgrid setgridhead
MSFlexGrid1.Row = 0 If Not rs_data1.EOF Then rs_data1.MoveFirst
Do While Not rs_data1.EOF
MSFlexGrid1.Row = MSFlexGrid1.Row + 1 MSFlexGrid1.Col = 0
If Not IsNull(rs_data1.Fields(0)) Then MSFlexGrid1.Text = rs_data1.Fields(0) Else MSFlexGrid1.Text = \
MSFlexGrid1.Col = 1
If Not IsNull(rs_data1.Fields(1)) Then MSFlexGrid1.Text = rs_data1.Fields(1) Else
52
MSFlexGrid1.Text = \
MSFlexGrid1.Col = 2
If Not IsNull(rs_data1.Fields(2)) Then MSFlexGrid1.Text = rs_data1.Fields(2) Else MSFlexGrid1.Text = \
MSFlexGrid1.Col = 3
If Not IsNull(rs_data1.Fields(3)) Then MSFlexGrid1.Text = rs_data1.Fields(3) Else MSFlexGrid1.Text = \
MSFlexGrid1.Col = 4
If Not IsNull(rs_data1.Fields(4)) Then MSFlexGrid1.Text = rs_data1.Fields(4) Else MSFlexGrid1.Text = \
MSFlexGrid1.Col = 5
If Not IsNull(rs_data1.Fields(5)) Then MSFlexGrid1.Text = rs_data1.Fields(5) Else MSFlexGrid1.Text = \
MSFlexGrid1.Col = 6
If rs_data1.Fields(6) = True Then MSFlexGrid1.Text = \入库\出库\
rs_data1.MoveNext Loop End If
displayerror:
If Err.Number <> 0 Then MsgBox Err.Description End If End Sub
Public Sub setgrid() Dim i As Integer
On Error GoTo seterror With MSFlexGrid1
.ScrollBars = flexScrollBarBoth .FixedCols = 0
.Rows = rs_data1.RecordCount + 1 .Cols = 7
.SelectionMode = flexSelectionByRow For i = 0 To .Rows - 1 .RowHeight(i) = 315 Next
For i = 0 To .Cols - 1 .ColWidth(i) = 1300 Next i End With Exit Sub seterror:
MsgBox Err.Description
53
End Sub
Public Sub setgridhead() On Error GoTo setheaderror MSFlexGrid1.Row = 0 MSFlexGrid1.Col = 0
MSFlexGrid1.Text = \货单号\MSFlexGrid1.Col = 1
MSFlexGrid1.Text = \日期\MSFlexGrid1.Col = 2
MSFlexGrid1.Text = \货源地\MSFlexGrid1.Col = 3
MSFlexGrid1.Text = \编号\MSFlexGrid1.Col = 4
MSFlexGrid1.Text = \经手人\MSFlexGrid1.Col = 5
MSFlexGrid1.Text = \备注\MSFlexGrid1.Col = 6
MSFlexGrid1.Text = \出入库\Exit Sub setheaderror:
MsgBox Err.Description End Sub
Private Sub MSFlexGrid1_Click() On Error GoTo griderror Dim getrow As Long If showgrid2 = True Then rs_data2.Close End If
getrow = MSFlexGrid1.Row If MSFlexGrid1.Rows = 1 Then
MsgBox \无相关纪录\Else
select_row = MSFlexGrid1.TextMatrix(getrow, 0) displaygrid2 End If griderror:
If Err.Number <> 0 Then MsgBox Err.Description End If End Sub
54
Public Sub displaygrid2() Dim sql As String Dim i As Integer
On Error GoTo displaybasicerror
sql = \货物明细 where 货单号=\rs_data2.Open sql, conn, adOpenKeyset, adLockPessimistic showdata
showgrid2 = True Exit Sub
displaybasicerror:
MsgBox Err.Description End Sub
Public Sub setgrid2head() Dim i As Integer
On Error GoTo set2error With MSFlexGrid2
.ScrollBars = flexScrollBarBoth .FixedCols = 0 .Cols = 9
.SelectionMode = flexSelectionByRow For i = 0 To .Rows - 1 .RowHeight(i) = 315 Next
For i = 0 To .Cols - 1 .ColWidth(i) = 1000 Next i
.Row = 0 .Col = 0
.Text = \货单号\ .Col = 1
.Text = \日期\ .Col = 2
.Text = \货源地\ .Col = 3
.Text = \物品名称\ .Col = 4
.Text = \单价\ .Col = 5
.Text = \数量\ .Col = 6
.Text = \单位\ .Col = 7
.Text = \金额\
55
VB实例源码
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)