当前位置: 开源爱好者 » 编程教程 » VB教程 » 正文

VB批量将数据录入到数据库中

VB结合数据库演示如何批量录入数据:

VERSION 5.00
Object = "{0ECD9B60-23AA-11D0-B351-00A0C9055D8E}#6.0#0"; "MSHFLXGD.OCX"
Begin VB.Form Form1 
   Caption         =   "批量录入数据"
   ClientHeight    =   3300
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   9570
   LinkTopic       =   "Form1"
   ScaleHeight     =   3300
   ScaleWidth      =   9570
   StartUpPosition =   3  '窗口缺省
   Begin MSHierarchicalFlexGridLib.MSHFlexGrid MSHFlexGrid2 
      Height          =   2655
      Left            =   120
      TabIndex        =   3
      Top             =   120
      Width           =   4695
      _ExtentX        =   8281
      _ExtentY        =   4683
      _Version        =   393216
      Rows            =   0
      FixedRows       =   0
      FixedCols       =   0
      _NumberOfBands  =   1
      _Band(0).Cols   =   2
      _Band(0).GridLinesBand=   1
      _Band(0).TextStyleBand=   0
      _Band(0).TextStyleHeader=   0
      _Band(0).ColHeader=   1
   End
   Begin VB.CommandButton Command1 
      Caption         =   "保存"
      Height          =   375
      Left            =   8400
      TabIndex        =   2
      Top             =   2880
      Width           =   975
   End
   Begin VB.TextBox Text1 
      Height          =   375
      Left            =   4320
      TabIndex        =   1
      Text            =   "Text1"
      Top             =   3360
      Width           =   855
   End
   Begin MSHierarchicalFlexGridLib.MSHFlexGrid MSHFlexGrid1 
      Height          =   2655
      Left            =   4920
      TabIndex        =   0
      Top             =   120
      Width           =   4575
      _ExtentX        =   8070
      _ExtentY        =   4683
      _Version        =   393216
      _NumberOfBands  =   1
      _Band(0).Cols   =   2
      _Band(0).GridLinesBand=   1
      _Band(0).TextStyleBand=   0
      _Band(0).TextStyleHeader=   0
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Form_Load()
con.Open "  Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=book"
 rs.Open "select * from 作者", con, adOpenKeyset, adLockOptimistic
Set MSHFlexGrid2.DataSource = rs
rs.Close
con.Close
'使第一列较窄?
  MSHFlexGrid1.ColWidth(0) = MSHFlexGrid1.ColWidth(0) / 2
 '初始化编辑框
  txtEdit = ""
  MSHFlexGrid1.Rows = 101
  MSHFlexGrid1.Cols = 5
  '设置列标头。
  s$ = "^|^编号          |^姓名   |^电话     |^住址       |^城市"
  MSHFlexGrid1.FormatString = s$
End Sub
Private Sub Command1_Click()
   con.Open "  Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=book"
   rs.Open "select * from 作者", con, adOpenKeyset, adLockOptimistic
    For i = 1 To MSHFlexGrid1.Rows - 1
      If MSHFlexGrid1.TextMatrix(i, 1) <> "" And MSHFlexGrid1.TextMatrix(i, 2) <> "" And MSHFlexGrid1.TextMatrix(i, 4) <> "" Then
       '添加新记录到"报损从表"中
        rs.AddNew
        If MSHFlexGrid1.TextMatrix(i, 1) <> "" Then rs.Fields("编号") = MSHFlexGrid1.TextMatrix(i, 1)
        If MSHFlexGrid1.TextMatrix(i, 2) <> "" Then rs.Fields("姓名") = MSHFlexGrid1.TextMatrix(i, 2)
        If MSHFlexGrid1.TextMatrix(i, 3) <> "" Then rs.Fields("电话") = MSHFlexGrid1.TextMatrix(i, 3)
        If MSHFlexGrid1.TextMatrix(i, 4) <> "" Then rs.Fields("住址") = MSHFlexGrid1.TextMatrix(i, 4)
        If MSHFlexGrid1.TextMatrix(i, 5) <> "" Then rs.Fields("城市") = MSHFlexGrid1.TextMatrix(i, 5)
        rs.Update
      End If
   Next i
 MsgBox "成功添加数据"
 rs.Close
 '添加的数据显示在表格中
   rs.Open "select * from 作者", con, adOpenKeyset, adLockOptimistic
   Set MSHFlexGrid2.DataSource = rs
   rs.Close
   con.Close
   End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
  If KeyAscii = vbKeyEscape Then    '当按下ESC键时
     Text1.Visible = False     'text1不可见
     MSHFlexGrid1.SetFocus     'mshflexgrid1获得焦点
     Exit Sub
  End If
  If KeyAscii = vbKeyReturn Then   '当按下回车键时
     '赋值给text1.text
     MSHFlexGrid1.Text = Text1.Text
     If MSHFlexGrid1.Col < 5 Then
        MSHFlexGrid1.Col = MSHFlexGrid1.Col + 1
     Else
       MSHFlexGrid1.Row = MSHFlexGrid1.Row + 1
       MSHFlexGrid1.Col = 1
     End If
     '指定text1控件在mshflexgrid1表格中的大小及位置
     Text1.Width = MSHFlexGrid1.CellWidth
     Text1.Height = MSHFlexGrid1.CellHeight
     Text1.Left = MSHFlexGrid1.CellLeft + MSHFlexGrid1.Left
     Text1.Top = MSHFlexGrid1.CellTop + MSHFlexGrid1.Top
     '赋值给mshflexgrid1.Text
     Text1.Text = MSHFlexGrid1.Text
     Text1.SetFocus     'text1获得焦点
  End If
End Sub
Private Sub mshflexgrid1_Click()     '单击单元格
  If MSHFlexGrid1.Col <= 5 And MSHFlexGrid1.Row <= 90 Then
     '指定text1控件在mshflexgrid1表格中的大小及位置
     Text1.Width = MSHFlexGrid1.CellWidth
     Text1.Height = MSHFlexGrid1.CellHeight
     Text1.Left = MSHFlexGrid1.CellLeft + MSHFlexGrid1.Left
     Text1.Top = MSHFlexGrid1.CellTop + MSHFlexGrid1.Top
     '赋值给mshflexgrid1.Text
     Text1.Text = MSHFlexGrid1.Text
     'text1可见
     Text1.Visible = True
     'text1获得焦点
     Text1.SetFocus
   End If
End Sub
Private Sub mshflexgrid1_KeyPress(KeyAscii As Integer)
  mshflexgrid1_Click
End Sub

 

CopyRight 2018© 开源爱好者 All Rights Reserved 豫ICP备16030607号 隐私声明 网站地图