I have an access table with a column username. I have multiple users that will use the database at different times and they will add rows to the table. But everytime a user add a new row, his/her username must appear in the username column of the table, without them having to type it in by themselves. I tried putting in UCase(Environ("USERNAME")) as the default value, but Environ is an unknown function. I use this UCase(Environ("USERNAME")) in VBA to get the user name when i export tables.
Is there any way i can set the deafult value of a column to a user's username?
Create a public function as described here:
Public Function GetUserName() As String
' GetUserName = Environ("USERNAME")
' Environ("USERNAME") is easily spoofed, see comment by HansUp
GetUserName = CreateObject("WScript.Network").UserName
End Function
and use =GetUserName() as default value for the control.
Related
I'm making a students management system in access, in which I have made a signup table named as students and after that I made it's form and it's saving data in table and after that I made login form which is working fine, and after logging in it's showing personal details form (I have set that) and which also stores data, and in personal details table I have a field named student_id_fk which stores the primary key of that user which is logged in but I am confused that how to get currently logged in user id is there any way?
Simplest way would be to use Environ("username")
Alternatively you may use below function or call Windows API
Function getUsername() As String
Dim WshNetwork As Object
Set WshNetwork = CreateObject("WScript.Network")
getUsername = WshNetwork.UserName
Set WshNetwork = Nothing
End Function
I'm new to VBA and Access, but I do have some understanding.
I can set a field easily by using
[Field1] = Now()
What I want to do is save the name of a field into a string and use that variable to reference the field. This way I can save different field names into the variable and have the code act on which field name happens to be stored.
From what I can find, the proper way to do this is in my situation is:
Private Sub ctlUpdateButton_Click()
Dim varField as String
varField = Dlookup("[Targeted Field]", "[Other Table]")
Cases.Fields(varField) = Now()
End Sub
This code breaks on the reference to Cases.Fields(varField) and reports the error as 'Run-time error '424' Object Required.
The record source for the form is a query based on the Cases table.
I'm not familiar enough with what I'm working with to know if this is a sufficient explanation.
Not sure what you're trying to do with varField = Dlookup("[Targeted Field]", "[Other Table]")
DLookup returns the value of a field in another table
Is there only one row in that table?
If so - then your code will work in theory -
If not, you;ll have to specifiy the criteria to find the correct row
The last issue is that you can't use a table name directly in code (in your case "Cases")
If you want to change the underlying record in your form, you can modify it if the field name is in your query and it hasn't been renamed.
Simply use Me(varField) = Now()
I have a form as a Datasheet View and inside I have added a column for checkbox.
Let us say that the column with check-boxes is column A and the one from the right it is column B.
How can I insert some text in column B when the checkbox from column A is marked as checked. I need somehow to find out the current row on witch the checkbox is and take the ID for the record from the row on with checkobox is and run a SQL insert for that specific ID in the database table, something like: SQL = "UPDATE table SET columnB='string' WHERE ID= ROWid;"
How can I do the row selection part from the datasheet view?
Basically Access is not working with rows but with datasets. This means that particularly here Access is reacting very differently than Excel.
Do you have only one single checkbox checked or do you allow the user for checking different checkboxes before inserting your desired value?
Add AfterUpdate Event on Checkbox and add:
Private Sub Checkbox_AfterUpdate()
Dim strUser As String
strUser = CurrentUser()
If [Checkbox] = True Then
[UserName] = strUser
Else
[UserName] = ""
End If
End Sub
[UserName] is your (text)field in the right colum. With a user management you can use the login name. If not you could use the current user or the Windows login name.
CurrentUser() is mostly returning "Admin". If you're looking for the windows username have a look here:
Retrieve the user name from Windows
I use the code below to create tables and set two field's default property values to functions. These functions set the default value to the user and date time when a record is entered. A very simple way to track when data was entered and by whom.
Apparently Microsoft ACCESS 2010 does not allow functions to be used as field default values anymore. Is there something simple (setting) I may be missing?
I understand the arguments for passing in these values directly from code and that the environ function can be manipulated. The processes that use this code are not critical enough to warrant that concern.
CurrentDb.Execute "CREATE TABLE Table(ActivityYearMonth DOUBLE, UserName TEXT, UserID TEXT, UpdatedOn DATE, UpdatedBy text)"
CurrentDb.TableDefs("Table").Fields("UpdatedOn").Properties("DefaultValue") = "=Now()"
CurrentDb.TableDefs("Table").Fields("UpdatedBy").Properties("DefaultValue") = "=Environ(""UserName"")"
Access does allow some functions as field Default Value properties. But not all functions are acceptable.
"=Now()" should work. At least it does for me.
However "=Environ(""UserName"")" triggers error 3388, "Unknown function 'Environ' in validation expression or default value on 'Table.UpdatedBy'."
By default Environ is sandboxed, so Access is restrictive about where and how you can use it. Any variation of Environ will not be accepted as a field Default Value.
And this is not a new development with Access 2010. I confirmed the same behavior in Access 2007.
If such functions worked before then likely the issue is sandbox mode.
http://office.microsoft.com/en-us/access-help/use-sandbox-mode-in-access-2010-HA010342092.aspx
Disabling sanbox mode and your expressions by setting SandBoxMode = 2 instead of 3 in Registry \Software\Microsoft\Office\14.0\Access Connectivity Engine\Engines should work.
Since you're using Access 2010 you might be able to use an event-driven data macro to achieve this. I just tried it and although =Environ(...) wasn't allowed I was able to use =GetUser() which I created as a VBA function
Option Compare Database
Option Explicit
Public Function GetUser()
Static s As String
Dim WshNet As Object ' WshNetwork
If Len(s) = 0 Then
Set WshNet = CreateObject("WScript.Network") ' New WshNetwork
s = WshNet.UserName
Set WshNet = Nothing
End If
GetUser = s
End Function
The Before Change data macro was simply
If [IsInsert] Then
SetField
Name UpdatedOn
Value = =Now()
SetField
Name UpdatedBy
Value = =GetUser()
Note: This worked fine when adding a record from within Access itself, but the GetUser() call will probably cause inserts from external applications to fail, so proceed with caution.
So I have this Access 2010 database into which users must login. The username they use is saved as a global variable. I then have a form which updates a table when they enter data and click a "save" button. I am trying to set one of the comboboxes (user) that is currently linked to a column in the table to be filtered on the global variable so that each person can only enter data under their own username. Is this possible? Does anyody know how to code this? I'm a complete newbie to Access and VBA and would appreciate any help
Greets
Me
In the form_load() function of that form you should fill the combobox with the global variable. To be sure they can't edit you should disable the combobox as well.
Private Sub Form_Load()
Me.myComboBoxName = gMyGlobalVariableName
Me.myComboBoxName.enabled = false
End Sub
However I'm assuming that the combobox has two columns (id and username) of which the first one is hidden and the primary key of some table where you store all the usernames. The gMyGlobalVariableName should have stored the id, not the username itself.
You can set the row source of the combo in the load event of the form to include only the relevant rows.
Me.TheCombo.RowSource = _
"SELECT UserColumn, Etc FROM TheTable WHERE UserColumn ='" _
& TheVariable & "'"
You may also wish to ensure that the form only contains the relevant records, however, the fact that you have a save button, suggests an unbound form. In Access, save buttons are largely redundant because the default is to save a record and stopping saves is the difficult bit.
I wonder why you do not use their windows log-in user name?