473,566 Members | 2,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tracking Record Activities into a table

3 New Member
Hi there,

I am trying to create a recordlog table that will track record activities in my database (adding, modifying, etc.. of records) but can't seem to work my head around it.

I have created a recordlog table that was meant to be populated whenever a record activity on my desired form which I cannot currently

Below is what I have so far from online guides which i searched

The form which I want to log record activities from

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub Form_BeforeUpdate(Cancel As Integer)
  4.     ModBy = Environ("Username")
  5.     ModDate = Now()
  6.  
  7. End Sub
  8.  
  9. Private Sub Form_Current()
  10. If Me.NewRecord Then
  11.     CreatedBy = Environ("Username")
  12. End If
  13. End Sub
I am totally new to access so please kindly assist

Thanks in advance
Oct 19 '17 #1
2 2231
MikeTheBike
639 Recognized Expert Contributor
Hi

Not sure what the problem you are having is, but I think this
Expand|Select|Wrap|Line Numbers
  1. If Me.NewRecord Then
  2. 11.     CreatedBy = Environ("Username")
  3. 12. End If
should be in the Form_BeforeUpda te event.

Does that help?

MTB
Oct 20 '17 #2
NeoPa
32,564 Recognized Expert Moderator MVP
My first, and probably most important, piece of advice must be Require Variable Declaration.

After that, I would say that you're probably better off using SQL for adding new records to a separate table when you're logging updates. It can also be done using Recordsets (EG Access VBA DAO Recordset Loop Using Two Recordsets) but that's another question.

Notice in the code below how the SQL is formed first; How it's invoked; Where it's invoked from. All important to understand if you're to create a system that actually works. Also, you have nothing in your code that includes the table name of where you're hoping to write this data. That's a strong clue the code won't work. In my suggested code I'll use the name [tblLog].
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option explicit
  3.  
  4. Private Sub Form_AfterUpdate()
  5.     Dim strSQL As String
  6.     Dim dbVar As DAO.Database
  7.  
  8.     strSQL = "INSERT INTO [tblLog]([%F],[ModDate]) VALUES '%U',#%D#"
  9.     strSQL = Replace(strSQL, "%F", IIf(Me.NewRecord, "CreatedBy", "ModBy"))
  10.     strSQL = Replace(strSQL, "%U", Environ("USERNAME"))
  11.     strSQL = Replace(strSQL, "%D", Format(Now(), "yyyy\-m\-d HH:nn:ss"))
  12.     Set dbVar = CurrentDb()
  13.     Call dbVar.Execute(strSQL, dbFailOnError)
  14. End Sub
NB. While Environ("USERNA ME") will normally give you an accurate reading of who's logged on, it can easily be hacked so shouldn't be used where security or accurate logging is required. A far more reliable approach can be found at Function to Return UserName (NT Login) of Current User.

Also, see Literal DateTimes and Their Delimiters (#) for why simply using Now() in a SQL string is not a good idea.
Oct 30 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
5141
by: Dan Greenblatt | last post by:
I am writing some software that, among other things, needs to track the state of database tables. This includes occasionally checking the table to see what records or added, modified, or deleted. The added and modified parts of this have not proven too difficult to implement, but is there an easy/elegant way to determine what records have been...
5
5344
by: byrocat | last post by:
Sybase and DB2 both have the capability of tracking user activities at a number of levels: invalid access attempts to databases, table, etc.; creation/deletion/modification of database objects/users/groups, grants/revokes. For MS SQLServer, the only setting that I've seen in the documentation is access attempts (none, fail only, etc.) ...
0
2420
by: Mark | last post by:
All, Excuse the re-post but I have found something which works for the UserID but not for the Password (see previous post below). The problem is I don't understand how it works and therefore cannot update it to do as I need. Could someone explain A) how to modify this to make sure the username & password are valid or B) How the below works so...
1
1868
by: gaurkamal | last post by:
I want to delete record in table B when i delete record in Table A both table have a common column. I want to do it using trigger .Can any body give some idea. Table details are. Table A: Channel_ID, Name Table B User_Id,Channel_ID, Description
1
2135
by: Becky99 | last post by:
In the database I'm working on (2003) I have a client table that includes a client status field, and I have a linked notes table that includes the note entry date, note type, and note details. Each client has multiple notes. Each time the client status is changed in the client table, I would like to have a note automatically created in the...
5
4335
by: saga git | last post by:
how to display nth record from table
3
2118
by: debo sniffa | last post by:
I want to add a value from a record in Table A to a record in table B if the value is same as 2 other fields in the Table A. How could i get this done in access?? Pls help me... ;-)
0
1121
by: Neven Huynh | last post by:
Hi Everyone, Here i my LINQ query to get record in Table Menu with condition are parentID == 0(get root menu) and ID != (parentID list) (which is parent ID list is are id of menu record that have child), i just want to load all record includes root menu that have no children record and children record : List<Menu> menus =...
0
776
by: akhilesh56 | last post by:
hello i want add 1000 above record in table lookup row source section but only 200 something record added. how increase row source limitation
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.