⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form_recmove.frm

📁 vb编程+从基础到实践光盘代码
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Frm_RecMove 
   Caption         =   "Form1"
   ClientHeight    =   3975
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   7260
   LinkTopic       =   "Form1"
   ScaleHeight     =   3975
   ScaleWidth      =   7260
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text9 
      Height          =   315
      Left            =   4920
      TabIndex        =   25
      Text            =   "Text9"
      Top             =   1680
      Width           =   1995
   End
   Begin VB.TextBox Text8 
      Height          =   315
      Left            =   4920
      TabIndex        =   23
      Text            =   "Text8"
      Top             =   1200
      Width           =   1995
   End
   Begin VB.TextBox Text7 
      Height          =   315
      Left            =   4920
      TabIndex        =   21
      Text            =   "Text7"
      Top             =   720
      Width           =   1995
   End
   Begin VB.CommandButton Command1 
      Caption         =   "添加"
      Height          =   555
      Left            =   360
      TabIndex        =   14
      Top             =   2640
      Width           =   1275
   End
   Begin VB.CommandButton Command2 
      Caption         =   "删除"
      Height          =   555
      Left            =   1680
      TabIndex        =   13
      Top             =   2640
      Width           =   1275
   End
   Begin VB.CommandButton Command3 
      Caption         =   "修改"
      Height          =   555
      Left            =   3000
      TabIndex        =   12
      Top             =   2640
      Width           =   1275
   End
   Begin VB.CommandButton Command4 
      Caption         =   "保存"
      Height          =   555
      Left            =   4320
      TabIndex        =   11
      Top             =   2640
      Width           =   1275
   End
   Begin VB.CommandButton Command5 
      Caption         =   "取消"
      Height          =   555
      Left            =   5640
      TabIndex        =   10
      Top             =   2640
      Width           =   1275
   End
   Begin VB.CommandButton Command6 
      Caption         =   "首记录"
      Height          =   555
      Left            =   360
      TabIndex        =   9
      Top             =   3240
      Width           =   1275
   End
   Begin VB.CommandButton Command7 
      Caption         =   "前一条"
      Height          =   555
      Left            =   1680
      TabIndex        =   8
      Top             =   3240
      Width           =   1275
   End
   Begin VB.CommandButton Command8 
      Caption         =   "后一条"
      Height          =   555
      Left            =   3000
      TabIndex        =   7
      Top             =   3240
      Width           =   1275
   End
   Begin VB.CommandButton Command9 
      Caption         =   "尾记录"
      Height          =   555
      Left            =   4320
      TabIndex        =   6
      Top             =   3240
      Width           =   1275
   End
   Begin VB.TextBox Text1 
      Height          =   315
      Left            =   1440
      TabIndex        =   5
      Text            =   "Text1"
      Top             =   240
      Width           =   1995
   End
   Begin VB.TextBox Text2 
      Height          =   315
      Left            =   1440
      TabIndex        =   4
      Text            =   "Text2"
      Top             =   720
      Width           =   1995
   End
   Begin VB.TextBox Text3 
      Height          =   315
      Left            =   1440
      TabIndex        =   3
      Text            =   "Text3"
      Top             =   1200
      Width           =   1995
   End
   Begin VB.TextBox Text4 
      Height          =   315
      Left            =   1440
      TabIndex        =   2
      Text            =   "Text4"
      Top             =   1680
      Width           =   1995
   End
   Begin VB.TextBox Text5 
      Height          =   315
      Left            =   1440
      TabIndex        =   1
      Text            =   "Text5"
      Top             =   2160
      Width           =   1995
   End
   Begin VB.TextBox Text6 
      Height          =   315
      Left            =   4920
      TabIndex        =   0
      Text            =   "Text6"
      Top             =   240
      Width           =   1995
   End
   Begin VB.Label Label9 
      AutoSize        =   -1  'True
      Caption         =   "最小库存:"
      Height          =   180
      Left            =   3840
      TabIndex        =   26
      Top             =   1740
      Width           =   900
   End
   Begin VB.Label Label8 
      AutoSize        =   -1  'True
      Caption         =   "订单数量:"
      Height          =   180
      Left            =   3840
      TabIndex        =   24
      Top             =   1260
      Width           =   900
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "库存数量:"
      Height          =   180
      Left            =   3840
      TabIndex        =   22
      Top             =   720
      Width           =   900
   End
   Begin VB.Label Label2 
      AutoSize        =   -1  'True
      Caption         =   "产品编号:"
      Height          =   180
      Left            =   360
      TabIndex        =   20
      Top             =   240
      Width           =   900
   End
   Begin VB.Label Label3 
      AutoSize        =   -1  'True
      Caption         =   "产品名称:"
      Height          =   180
      Left            =   360
      TabIndex        =   19
      Top             =   720
      Width           =   900
   End
   Begin VB.Label Label4 
      AutoSize        =   -1  'True
      Caption         =   "供应商编号:"
      Height          =   180
      Left            =   360
      TabIndex        =   18
      Top             =   1200
      Width           =   1080
   End
   Begin VB.Label Label5 
      AutoSize        =   -1  'True
      Caption         =   "类别编号:"
      Height          =   180
      Left            =   360
      TabIndex        =   17
      Top             =   1680
      Width           =   900
   End
   Begin VB.Label Label6 
      AutoSize        =   -1  'True
      Caption         =   "单位数量:"
      Height          =   180
      Left            =   360
      TabIndex        =   16
      Top             =   2160
      Width           =   900
   End
   Begin VB.Label Label7 
      AutoSize        =   -1  'True
      Caption         =   "单价:"
      Height          =   180
      Left            =   3840
      TabIndex        =   15
      Top             =   240
      Width           =   540
   End
End
Attribute VB_Name = "Frm_RecMove"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private myConn As New ADODB.Connection
Private myRecord As New ADODB.Recordset


'添加按钮单击事件,表示增加记录
Private Sub Command1_Click()
    '调用RecordSet对象的AddNew方法
    myRecord.AddNew
    '在调用AddNew以后,文本框控件处于添加模式,因此
    '为了防止程序出错,需要屏蔽其他按钮的使用
    
    '进行按钮设置
    Text1.Text = ""
    Text2.Text = ""
    Text3.Text = ""
    Text4.Text = ""
    Text5.Text = ""
    Text6.Text = ""
    Text7.Text = ""
    Text8.Text = ""
    Text9.Text = ""
End Sub

'删除按钮单击事件
Private Sub Command2_Click()
    '调用RecordSet对象的Delete方法删除数据
    myRecord.Delete
    
    '显示数据
    ShowData
End Sub

'修改按钮单击事件
Private Sub Command3_Click()
       
    '为了防止程序出错,需要屏蔽其他按钮的使用
    '同时使得各编辑框处于编辑状态
    '因为ADO同DAO不一样,没有提供Edit属性
    '事实上,只要修改记录后,直接调用update方法即可
End Sub

'保存按钮单击事件
Private Sub Command4_Click()
On Error Resume Next
    '保存数据,这里需要通过编程来实现
    '产品编号
    '产品编号不能为空
    If Text1.Text = "" Then
        MsgBox "产品编号不能为空!"
        Text1.SetFocus
        Exit Sub
    End If
    '其他文本框的判断类似,这里省略
    '保存产品编号
    myRecord.Fields("ProductID").Value = Val(Text1.Text)
    '保存产品名称
    myRecord.Fields("ProductName").Value = Text2.Text
    '保存供应商编号
    myRecord.Fields("SupplierID").Value = Val(Text3.Text)
    '保存类别编号
    myRecord.Fields("CategoryID").Value = Val(Text4.Text)
    '保存单位数量
    myRecord.Fields("QuantityPerUnit").Value = Text5.Text
    '保存单价
    myRecord.Fields("UnitPrice").Value = Val(Text6.Text)
    '保存库存数量
    myRecord.Fields("UnitsInStock").Value = Text7.Text
    '保存订单数量
    myRecord.Fields("UnitsOnOrder").Value = Val(Text8.Text)
    '保存重订等级
    myRecord.Fields("ReorderLevel").Value = Val(Text9.Text)
    
    '调用Recordset对象Update方法保存数据
    myRecord.Update
    
    '显示数据
    ShowData
    
End Sub

Private Sub Command5_Click()
    '调用Recordset对象cancelUpdate方法取消数据保存
    myRecord.CancelUpdate
    myRecord.MoveFirst
    '显示数据
    ShowData
End Sub

'首记录按钮单击事件
Private Sub Command6_Click()
    '调用MoveFirst移动
    myRecord.MoveFirst
    '显示记录
    ShowData
End Sub

'前一条按钮单击事件
Private Sub Command7_Click()
    '判断是否超出头记录
    '若超出头记录,再执行MovePrevious会出错
    If Not myRecord.BOF Then
        myRecord.MovePrevious
    Else
        '超出头记录
        '使用MoveFirst恢复到指向第一条记录
        myRecord.MoveFirst
    End If
    '显示数据
    ShowData
End Sub

'后一条按钮单击事件
Private Sub Command8_Click()
    '移动记录到下一条的方法是MoveNext
    '在调用MoveNext方法之前,首先需要判断,当前是不是已经超出尾纪录
    '若超出尾记录,再执行MoveNext会出错
    If Not myRecord.EOF Then
        myRecord.MoveNext
    Else
        '超出尾记录,使用MoveLast恢复指向尾记录
        myRecord.MoveLast
    End If
   '显示数据
   ShowData
    
End Sub

'尾记录按钮单击事件
Private Sub Command9_Click()
    '调用MoveLast
    myRecord.MoveLast
    '显示记录
    ShowData
End Sub

Private Sub Form_Load()
Dim mySQL As String
Dim strSQL As String
    '设定连接字符串
    mySQL = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;"
    mySQL = mySQL + "Data Source=" & App.Path & "\Product.mdb"
    
    'Set myConn = New ADODB.Connection
    
    myConn.ConnectionString = mySQL  '设定连接字符串
    
    'MsgBox mySQL
    myConn.Open '打开连接
    
    myRecord.ActiveConnection = myConn  '设定RecordSeet的连接对象为Connection
    
    strSQL = "select * from Products" '设定查询字符串
    '打开myRecord
    myRecord.Open strSQL, , adOpenDynamic, adLockOptimistic
    
    '显示数据
    ShowData
End Sub
'显示数据
Private Sub ShowData()
On Error Resume Next
    '产品编号
    Text1.Text = myRecord.Fields("ProductID").Value
    '产品名称
    Text2.Text = myRecord.Fields("ProductName").Value
    '供应商编号
    Text3.Text = myRecord.Fields("SupplierID").Value
    '类别编号
    Text4.Text = myRecord.Fields("CategoryID").Value
    '单位数量
    Text5.Text = myRecord.Fields("QuantityPerUnit").Value
    '单价
    Text6.Text = myRecord.Fields("UnitPrice").Value
    '库存数量
    Text7.Text = myRecord.Fields("UnitsInStock").Value
    '订单数量
    Text8.Text = myRecord.Fields("UnitsOnOrder").Value
    '重订等级
    Text9.Text = myRecord.Fields("ReorderLevel").Value
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -