📄 product.frm
字号:
VERSION 5.00
Begin VB.Form frmProduct
Caption = "商品信息"
ClientHeight = 4365
ClientLeft = 45
ClientTop = 345
ClientWidth = 3870
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 4365
ScaleWidth = 3870
StartUpPosition = 2 '屏幕中心
Begin VB.CommandButton cmdSave
Caption = "保存(&S)"
Height = 375
Left = 600
TabIndex = 5
Top = 3840
Width = 975
End
Begin VB.CommandButton cmdExit
Caption = "返回(&R)"
Height = 375
Left = 2280
TabIndex = 6
Top = 3840
Width = 975
End
Begin VB.Frame fraMemo
Caption = "备注信息"
Height = 1095
Left = 120
TabIndex = 11
Top = 2520
Width = 3612
Begin VB.TextBox txtItem
Height = 720
Index = 3
Left = 120
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 4
Top = 240
Width = 3375
End
End
Begin VB.Frame fraInfo
Caption = "商品信息"
Height = 2295
Left = 120
TabIndex = 7
Top = 120
Width = 3612
Begin VB.TextBox txtItem
BeginProperty DataFormat
Type = 1
Format = "0"
HaveTrueFalseNull= 0
FirstDayOfWeek = 0
FirstWeekOfYear = 0
LCID = 2052
SubFormatType = 1
EndProperty
Height = 270
Index = 4
Left = 1320
MaxLength = 20
TabIndex = 3
Top = 1800
Width = 2055
End
Begin VB.TextBox txtItem
Height = 270
Index = 2
Left = 1320
MaxLength = 20
TabIndex = 2
Top = 1320
Width = 2055
End
Begin VB.TextBox txtItem
Height = 270
Index = 1
Left = 1320
MaxLength = 20
TabIndex = 1
Top = 840
Width = 2055
End
Begin VB.TextBox txtItem
Height = 270
Index = 0
Left = 1320
MaxLength = 20
TabIndex = 0
Top = 360
Width = 2055
End
Begin VB.Label lblFields
AutoSize = -1 'True
Caption = "库存数量:"
Height = 180
Index = 3
Left = 240
TabIndex = 12
Top = 1815
Width = 900
End
Begin VB.Label lblFields
AutoSize = -1 'True
Caption = "商品名称:"
Height = 180
Index = 0
Left = 240
TabIndex = 10
Top = 360
Width = 900
End
Begin VB.Label lblFields
AutoSize = -1 'True
Caption = "商品规格:"
Height = 180
Index = 1
Left = 240
TabIndex = 9
Top = 845
Width = 900
End
Begin VB.Label lblFields
AutoSize = -1 'True
Caption = "计量单位:"
Height = 180
Index = 2
Left = 240
TabIndex = 8
Top = 1335
Width = 900
End
End
End
Attribute VB_Name = "frmProduct"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'是否改动过记录,若改动过,则将其设为True
Dim mbChange As Boolean
'mbAddMode为True,表示当前的窗体用于添加记录;
'mbAddMode为False,则表示当前的窗体用于编辑记录
Public mbAddMode As Boolean
'作为设置商品的序列号
Public mnSerial As Long
Private Sub cmdExit_Click()
If mbChange And cmdSave.Enabled Then
If MsgBox("保存当前记录的变化吗?", vbOKCancel + vbExclamation, "警告") = vbOK Then
'保存当前的记录
Call cmdSave_Click
End If
End If
Unload Me
Unload frmProductList
Load frmProductList
frmProductList.SQL = "select * from products"
frmProductList.Show
End Sub
Private Sub cmdSave_Click()
Dim strMsg As String
Dim rs As ADODB.Recordset
Dim strSQL As String
'验证字段的有效性
Dim i As Integer
For i = 0 To 2
If Trim(txtItem(i) & " ") = "" Then
Select Case i
Case 0
strMsg = "商品名称"
Case 1
strMsg = "商品规格"
Case 2
strMsg = "计量单位"
End Select
strMsg = strMsg & "不能为空!"
MsgBox strMsg, vbOKOnly + vbExclamation, "警告"
txtItem(i).SetFocus
Exit Sub
End If
Next i
Set rs = New ADODB.Recordset
If mbAddMode Then
'查找是否存在相同的记录
strSQL = "select * from products where 商品名称 ='" & Trim(txtItem(0)) & "' and 商品规格 = '" & Trim(txtItem(1)) & "'"
rs.Open strSQL, gConn, adOpenStatic
If Not rs.EOF Then
MsgBox "已经存在相同内容的记录!", vbOKOnly + vbExclamation, "警告"
txtItem(0).SetFocus
rs.Close
Exit Sub
End If
rs.Close
Else '表示当前处于编辑模式
'在编辑模式下,首先删除当前记录,然后在添加记录
strSQL = "delete from products where 顺序号 =" & Trim(mnSerial)
gConn.Execute strSQL
End If
'添加新的记录
strSQL = "select * from products"
rs.Open strSQL, gConn, adOpenDynamic, adLockOptimistic
rs.AddNew
For i = 0 To rs.Fields.Count - 2
rs.Fields(i + 1) = txtItem(i)
Next i
rs.Update
rs.Close
If mbAddMode Then '当前的窗体处于添加模式
mbChange = False
MsgBox "添加商品信息成功!继续添加下一条商品信息", vbOKOnly + vbExclamation, "添加商品信息"
Unload Me
Me.Show vbModal
Else '当前的窗体用于修改记录
Unload Me
frmProductList.SQL = "select * from products"
frmProductList.Show
End If
End Sub
Private Sub Form_Load()
Dim strSQL As String
Dim i As Integer
Dim rs As New ADODB.Recordset
If mbAddMode Then
Me.Caption = Me.Caption & "添加"
Else
strSQL = "select * from products where 顺序号 ='" & Trim(frmProductList.grdList.TextMatrix(frmProductList.grdList.Row, 1)) & "'"
rs.Open strSQL, gConn, adOpenKeyset
If rs.EOF = False Then
With rs
mnSerial = .Fields(0)
For i = 0 To .Fields.Count - 2
If Not IsNull(.Fields(i + 1)) Then
txtItem(i) = .Fields(i + 1)
End If
Next i
End With
End If
rs.Close
Me.Caption = Me.Caption & "修改"
End If
mbChange = False
End Sub
Private Sub txtItem_Change(Index As Integer)
mbChange = True
End Sub
Private Sub txtItem_GotFocus(Index As Integer)
'文本框一旦取得焦点,就选中整个文本框的内容
txtItem(Index).SelStart = 0
txtItem(Index).SelLength = Len(txtItem(Index))
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -