我还在努力学习,所以请保持温柔。 我的目标是通过点击最后一张幻灯片上的按钮来跟踪已经完成查看独立MS PowerPoint的用户。
单击completeButton时,它会捕获Windows用户名和日期。如何让事件同时将变量作为新记录插入现有的MS Access 2007数据库(幕后)?
'Click button
Private Sub completeButton_Click()
'Get Windows user
Dim empUserName As String
empUserName = Environ("UserName")
'Get Date
Dim Completed As Date
Completed = Date
'Set Path to database
Dim Path As String
Path = "C:\TestDatabase.accdb"
'Open connection to database
'Insert empUserName and Completed variables as new record
'Close connection to database
'Display message
Msgbox("Thank you for viewing the material")
'Close presentation
With Application.Presentations
For i = .Count To 1 Step -1
.Item(i).Close
Next
End With
答案 0 :(得分:2)
我在Office 2013上对此进行了测试,希望它也适合您。这些行打开连接,插入新条目并关闭连接。
Dim Path As String
Dim strCon As String
Dim con As Object
Path = "C:\TestDatabase.accdb"
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Path
Set con = CreateObject("ADODB.connection")
con.Open strCon
con.Execute "INSERT INTO Table1 (Field1, Field2) VALUES (""" & empUserName & """, """ & Completed & """)"
con.Close
Set con = Nothing
表1 - 您的表名
Field1 - 要放置的列的名称empUserName
Field2 - 要放置的列的名称Completed
答案 1 :(得分:0)
可以通过一个命令完成:
DoCmd.RunSQL "INSERT INTO C:\TestDatabase.accdb.Table1 ( Field1, Field2 ) VALUES (""" & empUserName & """, """ & Completed & """);"
您可以添加
DoCmd.SetWarnings False
插入之前以禁止警告。