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

📄 frmstock.frm

📁 <Visual Basic 数据库开发实例精粹(第二版)>一书首先介绍了Visual Basic(简称VB)开发的技巧和重点技术
💻 FRM
📖 第 1 页 / 共 2 页
字号:
         ColumnCount     =   2
         BeginProperty Column00 
            DataField       =   ""
            Caption         =   ""
            BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
               Type            =   0
               Format          =   ""
               HaveTrueFalseNull=   0
               FirstDayOfWeek  =   0
               FirstWeekOfYear =   0
               LCID            =   2052
               SubFormatType   =   0
            EndProperty
         EndProperty
         BeginProperty Column01 
            DataField       =   ""
            Caption         =   ""
            BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
               Type            =   0
               Format          =   ""
               HaveTrueFalseNull=   0
               FirstDayOfWeek  =   0
               FirstWeekOfYear =   0
               LCID            =   2052
               SubFormatType   =   0
            EndProperty
         EndProperty
         SplitCount      =   1
         BeginProperty Split0 
            MarqueeStyle    =   0
            AllowRowSizing  =   0   'False
            BeginProperty Column00 
            EndProperty
            BeginProperty Column01 
            EndProperty
         EndProperty
      End
      Begin MSAdodcLib.Adodc adoStock 
         Height          =   495
         Left            =   4440
         Top             =   2160
         Width           =   1335
         _ExtentX        =   2355
         _ExtentY        =   873
         ConnectMode     =   0
         CursorLocation  =   3
         IsolationLevel  =   -1
         ConnectionTimeout=   15
         CommandTimeout  =   30
         CursorType      =   3
         LockType        =   3
         CommandType     =   8
         CursorOptions   =   0
         CacheSize       =   50
         MaxRecords      =   0
         BOFAction       =   0
         EOFAction       =   0
         ConnectStringType=   1
         Appearance      =   1
         BackColor       =   -2147483643
         ForeColor       =   -2147483640
         Orientation     =   0
         Enabled         =   -1
         Connect         =   ""
         OLEDBString     =   ""
         OLEDBFile       =   ""
         DataSourceName  =   ""
         OtherAttributes =   ""
         UserName        =   ""
         Password        =   ""
         RecordSource    =   ""
         Caption         =   "Adodc1"
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "宋体"
            Size            =   9
            Charset         =   134
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         _Version        =   393216
      End
   End
End
Attribute VB_Name = "frmStock"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Public IsModify As Boolean        '用于判断记录修改后保存,还是添加后保存
Dim TotalRecord As Integer  '打开的数据表中的记录总数


Private Sub comDate_Click()
  MonthView1.Visible = True
End Sub

Private Sub Form_Load()
  Dim i As Integer
  Dim strSQL  As String
  
  '确定窗体的初始位置
  frmStock.Left = 2000
  frmStock.Top = 250
  adoStock.LockType = adLockBatchOptimistic
  adoStock.ConnectionString = g_Conn.ConnectionString
  

  Dim StockType   As Variant
  
  '将需要添加的股票类型设置为数组
  StockType = Array("A股", "B股")
  For i = 0 To 1
      Combo1.AddItem StockType(i)
  Next i
  
  Tlbar.Buttons.add 1, "Add", "增加", , "Add"    '添加按钮
  Tlbar.Buttons.add 2, , , tbrSeparator          '添加分隔符
  Tlbar.Buttons.add 3, "Edit", "修改", , "Edit"
  Tlbar.Buttons.add 4, , , tbrSeparator
  Tlbar.Buttons.add 5, "Save", "保存", , "Save"
  Tlbar.Buttons.add 6, , , tbrSeparator
  Tlbar.Buttons.add 7, "Del", "删除", , "Del"
  Tlbar.Buttons.add 8, , , tbrSeparator
  Tlbar.Buttons.add 9, "Exit", "退出", , "Exit"
   
  Tlbar.Buttons(2).Enabled = False
  Tlbar.Buttons(3).Enabled = False
  Tlbar.Buttons(5).Enabled = False
  AllDisEnabled
  Combo1.Enabled = False
  MonthView1.Visible = False
  comDate.Enabled = False
  '选择表中必要的字段在表格中显示出来,并且按照StockChange中的索引号排序
  'Dim strSQL As String
  strSQL = "SELECT StockChange.ID, "
  strSQL = strSQL & " StockChange.Code as 股票代码, Name as 股票名称, "
  strSQL = strSQL & " Last as 昨日收盘, Today as 今日开盘, "
  strSQL = strSQL & " Volume as 交易量, [Volume Price] as 交易额, "
  strSQL = strSQL & " Change as 涨跌, TypeName as 类型, Day as 日期 "
  strSQL = strSQL & " FROM StockChange, StockInfo, StockType  "
  strSQL = strSQL & " WHERE StockChange.Code=StockInfo.Code AND "
  strSQL = strSQL & " StockInfo.TypeId=StockType.ID "
  strSQL = strSQL & " ORDER BY StockChange.ID "
  
  adoStock.RecordSource = strSQL
  adoStock.Refresh '更新数据源
  
  grdStock.Columns(0).Width = 300 '设置第一列的宽度
  'rec.MoveLast                  '将记录指针移动到最后
  TotalRecord = adoStock.Recordset.RecordCount  '然后用这一条代码统计记录总数
  'rec.MoveFirst                 '指针移动到记录的最开始
  
  '在状态栏中显示记录总数
  sbr.Panels(1).Text = " 共 :" & TotalRecord & "条记录"

End Sub

Private Sub grdStock_SelChange(Cancel As Integer)
  If grdStock.Row = -1 Then Exit Sub
  Dim tt As String
  '一共有9个TextBox
  'grdStock.get
  On Error Resume Next
  
  With grdStock
    txtID.Text = .Columns(0).CellValue(.RowBookmark(.Row))
    txtCode.Text = .Columns(1).CellValue(.RowBookmark(.Row))
    txtName.Text = .Columns(2).CellValue(.RowBookmark(.Row))
    txtLast.Text = .Columns(3).CellValue(.RowBookmark(.Row))
    txtToday.Text = .Columns(4).CellValue(.RowBookmark(.Row))
    txtVol.Text = .Columns(5).CellValue(.RowBookmark(.Row))
    txtVolPrice.Text = .Columns(6).CellValue(.RowBookmark(.Row))
    txtChange.Text = .Columns(7).CellValue(.RowBookmark(.Row))
    txtDate.Text = .Columns(9).CellValue(.RowBookmark(.Row))
    If .Columns(8).CellValue(.RowBookmark(.Row)) = "A股" Then
      Combo1.ListIndex = 0
    Else
      Combo1.ListIndex = 1
    End If
  End With
  
  '所有文本框禁用,使之不可修改
  AllDisEnabled
  '弹出日期控件的按钮禁用
  comDate.Enabled = False
  '判断并在组合框中显示每支股票的类型

  
  Tlbar.Buttons(3).Enabled = True     '修改按钮可用
  Tlbar.Buttons(5).Enabled = False    '保存按钮禁用
  Combo1.Enabled = False              '显示股票类型的组合框禁用


End Sub

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
  txtDate.Text = MonthView1.Value
  MonthView1.Visible = False
End Sub

'工具栏的按钮点击后处理函数
Private Sub Tlbar_ButtonClick(ByVal Button As MSComctlLib.Button)
  Dim strSQL As String
  Dim rst As VbMsgBoxResult
  
  '判别工具栏按钮的关键字
  Select Case Button.Key
    Case "Add"
      add            '调用add子过程
    Case "Save"
      save
    Case "Del"      '删除记录,
      '如果股票代码栏为空,则退出子过程
      If txtCode.Text = "" Then Exit Sub
      rst = MsgBox("确定要删除【" & txtName.Text & "】?(Y/N)", _
                vbQuestion + vbOKCancel + vbDefaultButton1, "股票分析系统")
      If rst = 1 Then
        '删除指定股票的一条记录
        strSQL = " DELETE * FROM [StockChange] "
        strSQL = strSQL & " WHERE Code='" & grdStock.Columns(0).CellValue(grdStock.RowBookmark(grdStock.Row)) & "'"
        g_Conn.Execute strSQL
        '对数据库数据操作后,必须进行更新
        adoStock.Refresh
        add
        '如果记录指针没有移动到结尾,指针移动到最后一条记录
        If Not adoStock.Recordset.EOF Then
          adoStock.Recordset.MoveLast
        End If
        '显示记录总数
        TotalRecord = adoStock.Recordset.RecordCount
        sbr.Panels(1).Text = " 共 :" & TotalRecord & "条记录。"
      End If
      Clear       '调用Clear过程,设置文本框和按钮是否能用
    Case "Edit"
      Edit        '调用修改过程
    Case "Exit"
      Unload Me   '退出该窗体
  End Select
End Sub

'添加数据,将各个文本框设为可用状态,并且清空
Private Sub add()
  AllEnabled                '所有文本框可用
  AllClear                  '所有文本框清空
  comDate.Enabled = True    '可以用comDate调用日期控件
  txtCode.SetFocus          '将txtCode设置为焦点
  IsModify = False                '修改标志置为“否”
  Tlbar.Buttons(5).Enabled = True   '“保存”设置为可用
  Combo1.Enabled = True     '显示股票类型的组合框可用
  txtCode.Locked = False
End Sub

'保存数据修改
Private Sub save()

  Dim strSQL As String
  On Error GoTo ErrProcess
  
  '检查输入的数据,保证输入数据有效
  If Trim(txtCode.Text) = "" Or _
    Trim(txtLast.Text) = "" Or _
    Trim(txtName.Text) = "" Or _
    Trim(txtToday.Text) = "" Or _
    Trim(txtVolPrice.Text) = "" Or _
    Trim(txtDate.Text) = "" Or _
    Val(txtDate.Text) < #1/1/100# Or _
    Val(txtDate.Text) > #12/31/9999# Or _
    Not IsNumeric(txtToday.Text) Or _
    Not IsNumeric(txtLast.Text) Or _
    Not IsNumeric(txtVolPrice.Text) Or _
    Not IsNumeric(txtVol.Text) Or _
    Not IsNumeric(txtChange.Text) Then
      Call MsgBox("数据非法,不能保存!", 0 + 16, "股票分析系统")
      Exit Sub
  End If
 
  '添加新的股票记录
  If IsModify = False Then
     g_Conn.BeginTrans
     '添加一条股票基本信息
     strSQL = " INSERT INTO StockInfo([Code], [Name], [TypeId]) "
     strSQL = strSQL & " VALUES("
     strSQL = strSQL & "'" & txtCode.Text & "',"
     strSQL = strSQL & "'" & txtName.Text & "',"
     strSQL = strSQL & Combo1.ListIndex + 1 & ")"
     g_Conn.Execute strSQL
     
     '在StockChange中添加一个新的记录
     strSQL = " INSERT INTO StockChange([Code], [Last], [Volume], [Today], "
     strSQL = strSQL & " Change, [volume Price], [Day]) "
     strSQL = strSQL & " VALUES("
     strSQL = strSQL & "'" & txtCode.Text & "',"
     strSQL = strSQL & txtLast.Text & ","
     strSQL = strSQL & txtVol.Text & ","
     strSQL = strSQL & txtToday.Text & ","
     strSQL = strSQL & txtChange.Text & ","
     strSQL = strSQL & txtVolPrice.Text & ","
     strSQL = strSQL & "#" & txtDate.Text & "#)"
     g_Conn.Execute strSQL
     
     g_Conn.CommitTrans
     
     adoStock.Refresh
     If Not adoStock.Recordset.EOF Then
        adoStock.Recordset.MoveLast
     End If
     TotalRecord = adoStock.Recordset.RecordCount
     sbr.Panels(1).Text = " 共 :" & TotalRecord & "条记录。"
   Else   '修改股票信息
      '开始事务
      g_Conn.BeginTrans
      '更新Stockchange表
      strSQL = " UPDATE StockChange "
      strSQL = strSQL & " SET [Code] = '" & txtCode.Text & "',"
      strSQL = strSQL & " [Last] = " & txtLast.Text & ","
      strSQL = strSQL & " [Volume] = " & txtVol.Text & ","
      strSQL = strSQL & " [Today] = " & txtToday.Text & ","
      strSQL = strSQL & " [Change] = " & txtChange.Text & ","
      strSQL = strSQL & " [Volume Price] =" & txtVolPrice.Text
      strSQL = strSQL & " WHERE [ID]=" & txtID.Text
      g_Conn.Execute strSQL
      '同时更新StockInfo表
      strSQL = " UPDATE StockInfo "
      strSQL = strSQL & " SET Code='" & txtCode.Text & "',"
      strSQL = strSQL & " [Name] ='" & txtName.Text & "',"
      strSQL = strSQL & " TypeId=" & Combo1.ListIndex + 1
      strSQL = strSQL & " WHERE Code ='" & txtCode.Text & "'"
      g_Conn.Execute strSQL
      '接受事务
      g_Conn.CommitTrans
      '刷新显示
      adoStock.Refresh
   End If
   Clear
   Exit Sub
ErrProcess:
  MsgBox Err.Description
End Sub
Private Sub txt_Change(Index As Integer)
  Tlbar.Buttons(5).Enabled = True
End Sub

'将文本框设置为可用状态
Private Sub Edit()
  IsModify = True                  '修改标志为“是”
  AllEnabled                 '所有文本框可用
  Tlbar.Buttons(5).Enabled = True    '保存按钮可用
  Combo1.Enabled = True      '用于选择股票类型的组合框可用
  comDate.Enabled = True     '调用日期控件的按钮可用
  txtCode.Locked = True
End Sub

Private Sub Clear()
  AllClear
  AllDisEnabled
  Tlbar.Buttons(3).Enabled = False
  Tlbar.Buttons(5).Enabled = False
  Combo1.Enabled = False
  comDate.Enabled = False
End Sub

Private Sub AllClear()
  txtCode.Text = ""
  txtLast.Text = ""
  txtName.Text = ""
  txtVol.Text = ""
  txtToday.Text = ""
  txtChange.Text = ""
  txtVolPrice.Text = ""
End Sub

'将各个text设置为不可用,即不能更改
Private Sub AllDisEnabled()
  fraStock.Enabled = False
End Sub

Private Sub AllEnabled()
  fraStock.Enabled = True
End Sub


⌨️ 快捷键说明

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