Access add new line to table

萝らか妹 提交于 2019-12-23 05:48:06

问题


I have sub that runs when the database is opened to a specific form and I'm trying to get it to add information in a table.

The table name is UnAuthorizedAccess and the columns in table are ID(auto number), NAME(text), COMPUTERNAME(text), ATTEMPTDATE(date/time).

What commands do I need to use to add a new record to this table? I have a VBA that if they're login information Isn't there is will force close access all together. I'm trying to gather information on the user as well before kicking them out.

I figured this is the easiest way as outlook won't let you send a hidden email from the user unless they first see it.


回答1:


You can add records to a recordset with the following code, but I am unsure whether you have a field called COMPUTERNAME. You shouldn't need to add the ID value as its an autonumber.

dim Rst as recordset      
Set Rst = CurrentDb.OpenRecordset(Name:="UnauthorizedAccess", Type:=RecordsetTypeEnum.dbOpenDynaset)
         With Rst
            .AddNew
            ![NAME] = Me.Name.Value
            ![COMPUTERNAME] = Me.COMPUTERNAME.Value
            ![ATEMPTDATE] = date()
            .Update
         End With

As for sending hidden emails, see this question I asked not so long ago. It sends an email via outlook, but remember to reference the Microsoft Outlook Object library in the VBA Editor.




回答2:


CurrentDB.Execute is the method for executing SQL statements, and INSERT INTO is the SQL statement for adding records to a DB table.

CurrentDB.Execute "INSERT INTO UnAuthorizedAccess (NAME, COMPUTERNAME, ATTEMPTDATE) " & _
"VALUES (" & Your_NAME_Variable & ", " & Your_COMPUTERNAME_Variable  & ", " & Now() & ")

Replace Your_NAME_Variable and Your_COMPUTERNAME_Variable with the variables in your code containing these values.



来源:https://stackoverflow.com/questions/36992004/access-add-new-line-to-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!