?? form1.frm
字號:
VERSION 5.00
Begin VB.Form Form1
Caption = "復制EXCEL數(shù)據(jù)到ACCESS"
ClientHeight = 1590
ClientLeft = 60
ClientTop = 345
ClientWidth = 5760
LinkTopic = "Form1"
ScaleHeight = 1590
ScaleWidth = 5760
StartUpPosition = 3 'Windows Default
Begin VB.TextBox txtAccessFile
Height = 285
Left = 1560
TabIndex = 3
Top = 480
Width = 4095
End
Begin VB.CommandButton cmdLoad
Caption = "數(shù)據(jù)復制"
Default = -1 'True
Height = 495
Left = 2280
TabIndex = 2
Top = 960
Width = 1215
End
Begin VB.TextBox txtExcelFile
Height = 285
Left = 1560
TabIndex = 1
Top = 120
Width = 4095
End
Begin VB.Label Label1
Caption = "ACCESS文件"
Height = 255
Index = 1
Left = 120
TabIndex = 4
Top = 480
Width = 1335
End
Begin VB.Label Label1
Caption = "EXCEL文件"
Height = 255
Index = 0
Left = 120
TabIndex = 0
Top = 120
Width = 1335
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'數(shù)據(jù)復制按鈕單擊事件
Private Sub cmdLoad_Click()
Dim excel_app As Object
Dim excel_sheet As Object
Dim db As Database
Dim new_value As String
Dim row As Integer
Screen.MousePointer = vbHourglass
DoEvents
'創(chuàng)建Excel應用程序對象
Set excel_app = CreateObject("Excel.Application")
'打開Excel文件
excel_app.Workbooks.Open FileName:=txtExcelFile.Text
'檢查最新的版本
If Val(excel_app.Application.Version) >= 8 Then
Set excel_sheet = excel_app.ActiveSheet
Else
Set excel_sheet = excel_app
End If
' 打開ACCESS數(shù)據(jù)庫
Set db = OpenDatabase(txtAccessFile.Text)
' 獲得Excel數(shù)據(jù),并插入到Access數(shù)據(jù)庫表中
row = 1
Do
'獲得下一個值
new_value = Trim$(excel_sheet.Cells(row, 1))
'檢查是否是空值
If Len(new_value) = 0 Then Exit Do
'將值插入到表中
db.Execute "INSERT INTO TestValues VALUES (" & _
new_value & ")"
row = row + 1
Loop
' 關閉數(shù)據(jù)庫
db.Close
Set db = Nothing
' 設定關閉Excel的時候,不保存
excel_app.ActiveWorkbook.Close False
'關閉Excel,并釋放資源
excel_app.Quit
Set excel_sheet = Nothing
Set excel_app = Nothing
Screen.MousePointer = vbDefault
MsgBox "Copied " & Format$(row - 1) & " values."
End Sub
'窗體載入事件
' 本程序需要引用 DAO 3.51 Object Library
Private Sub Form_Load()
Dim file_path As String
file_path = App.Path
If Right$(file_path, 1) <> "\" Then file_path = file_path & "\"
'初始化excel和access路徑,如果改成其他文件,注意實現(xiàn)的程序也要改變
txtExcelFile.Text = file_path & "XlsToMdb.xls"
txtAccessFile.Text = file_path & "XlsToMdb.mdb"
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -