|
我不懂VBA,也不会access 工作需要,论坛里抄的自己也改了点代码,你看着在自己改吧,上传表会打开个对话框选择文件,里面还有个表是记录上传时间什么的。自己在改下吧。
以下是两个按纽删除和上传的代码
Option Compare Database
Private Sub Command0_Click()
Dim cmd As New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandText = "Delete from Sheet1"
cmd.CommandType = adCmdText
cmd.Prepared = True
cmd.Execute
Set cmd = Nothing
Dim record As DAO.Recordset
Set record = CurrentDb.OpenRecordset("select * From record")
record.AddNew
record![数据操作] = "Delete"
record![操作方式] = "Auto"
record![日期] = Now()
record.Update
record.Close
Set record = Nothing
Dim Row As String
strSQL = "Select count(item_number) AS Row from Sheet1"
Set record = CurrentDb.OpenRecordset(strSQL)
record.MoveFirst
Row = record!Row
MsgBox ("以清空表,現有行数-" & Row)
End Sub
下面是上传表
Private Sub Command1_Click()
Set dOpen = Application.FileDialog(1)
With dOpen
.Filters.Clear
.Filters.Add "excel文档", "*.xlsx"
.Show
End With
If dOpen.SelectedItems.Count > 0 Then
FileName = dOpen.SelectedItems(1)
DoCmd.TransferSpreadsheet acImport, , "Sheet1", FileName, True
Else
FileName = ""
End If
Set dOpen = Nothing
Dim record As DAO.Recordset '命名结果集
Set record = CurrentDb.OpenRecordset("select * From record") '执行SQL查询语句
record.AddNew '给表record增加一行并添加数据,下面4句为添加数据语句
record![数据操作] = "Updata"
record![操作方式] = "Auto"
record![日期] = Now()
record![file] = FileName
record.Update '上传数据到表
record.Close '关闭结果集
Set record = Nothing '结果集清空
Dim Row As String '设置字符变量
strSQL = "Select count(item_number) AS hang from Sheet1" 'SQL查询句,这里返里行数,并重命名列名Row
Set record = CurrentDb.OpenRecordset(strSQL) '执行SQL查询语句
record.MoveFirst '从结果集内提取数据
Row = record!hang '把结果集内的hang列数据赋值给Row
MsgBox ("以上載表,現有行数-" & Row)
Set record = Nothing
End Sub
|
|