📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "写数据到EXCEL文件"
ClientHeight = 2775
ClientLeft = 60
ClientTop = 345
ClientWidth = 5115
LinkTopic = "Form1"
ScaleHeight = 2775
ScaleWidth = 5115
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command3
Caption = "退出"
BeginProperty Font
Name = "Times New Roman"
Size = 9.75
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 735
Left = 3240
TabIndex = 6
Top = 1680
Width = 1095
End
Begin VB.CommandButton Command2
Caption = "写到EXCEL文件中"
BeginProperty Font
Name = "Times New Roman"
Size = 9.75
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 735
Left = 1920
TabIndex = 5
Top = 1680
Width = 1215
End
Begin VB.CommandButton Command1
Caption = "保存数据到数据库"
BeginProperty Font
Name = "Times New Roman"
Size = 9.75
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 735
Left = 480
TabIndex = 4
Top = 1680
Width = 1335
End
Begin VB.TextBox Text2
Height = 735
Left = 1320
TabIndex = 3
Top = 720
Width = 3615
End
Begin VB.TextBox Text1
Height = 285
Left = 1320
TabIndex = 0
Top = 240
Width = 3615
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "地 址:"
BeginProperty Font
Name = "Times New Roman"
Size = 9.75
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 225
Left = 120
TabIndex = 2
Top = 840
Width = 855
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "姓 名:"
BeginProperty Font
Name = "Times New Roman"
Size = 9.75
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 225
Left = 120
TabIndex = 1
Top = 240
Width = 855
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
'*******For Excel Report Generation*******
Dim X As Excel.Application
Function connectDB()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source= " & App.Path & "\datareport.mdb"
End Function
Private Sub Command1_Click()
Call connectDB
rs.Open "SELECT * FROM demo", conn, adOpenStatic, adLockOptimistic
rs.AddNew
rs.Fields(0) = Text1.Text
rs.Fields(1) = Text2.Text
rs.Update
Set rs = Nothing
Set conn = Nothing
End Sub
Private Sub Command2_Click()
Call xlsreport
End Sub
Private Function xlsreport()
Dim n As Long
Dim a, b As String
Call connectDB
Set rs = conn.Execute("select * from demo")
m = rs.RecordCount
Set X = CreateObject("Excel.application")
With X
.Workbooks.Open FileName:=App.Path & "\reports.xls"
.Sheets("data").Activate
n = 2
a = "A" & n
b = "B" & n
.Range("A1").Value = rs.Fields(0).Name
.Range("B1").Value = rs.Fields(1).Name
Do Until rs.EOF
.Range(a).Value = rs.Fields(0)
.Range(b).Value = rs.Fields(1)
.Range(a).Select
.Selection.EntireRow.Insert
n = n + 1
rs.MoveNext
Loop
.ActiveWorkbook.Save
.Application.Quit
End With
Set X = Nothing
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -