?? form1.frm
字號:
VERSION 5.00
Begin VB.Form Form1
Caption = "Excel To MDB"
ClientHeight = 2100
ClientLeft = 60
ClientTop = 345
ClientWidth = 5760
LinkTopic = "Form1"
ScaleHeight = 2100
ScaleWidth = 5760
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox txtAccessFile
Height = 285
Left = 1560
TabIndex = 3
Top = 720
Width = 4095
End
Begin VB.CommandButton cmdLoad
Caption = "轉換數據"
Default = -1 'True
Height = 495
Left = 2160
TabIndex = 2
Top = 1440
Width = 1215
End
Begin VB.TextBox txtExcelFile
Height = 285
Left = 1560
TabIndex = 1
Top = 240
Width = 4095
End
Begin VB.Label Label1
Caption = "Access數據庫:"
Height = 255
Index = 1
Left = 120
TabIndex = 4
Top = 735
Width = 1335
End
Begin VB.Label Label1
Caption = "Excel文件:"
Height = 255
Index = 0
Left = 120
TabIndex = 0
Top = 255
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
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
' 建立Excel對象
Set excel_app = CreateObject("Excel.Application")
'打開Excel工作薄.
excel_app.Workbooks.Open FileName:=txtExcelFile.Text
' 檢查Excel的版本
If Val(excel_app.Application.Version) >= 8 Then
Set excel_sheet = excel_app.ActiveSheet
Else
Set excel_sheet = excel_app
End If
' 打開數據庫
Set db = OpenDatabase(txtAccessFile.Text)
'取得Excel表中單元格的值,然后插入數據庫的TestValues表中
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
'關閉數據庫
db.Close
Set db = Nothing
'退出Excel
excel_app.ActiveWorkbook.Close False
excel_app.Quit
Set excel_sheet = Nothing
Set excel_app = Nothing
Screen.MousePointer = vbDefault
MsgBox "復制了 " & Format$(row - 1) & " 個值到數據庫中。"
End Sub
Private Sub Form_Load()
Dim file_path As String
file_path = App.Path
If Right$(file_path, 1) <> "\" Then file_path = file_path & "\"
txtExcelFile.Text = file_path & "XlsToMdb.xls"
txtAccessFile.Text = file_path & "XlsToMdb.mdb"
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -