How to get current logged in user id in access-db 2016 - ms-access

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

Related

("WScript.Network").UserName in not being Saved in Access

I used the following VBA Code in The Form (General)
Public Function GetUserName() As String
GetUserName = CreateObject("WScript.Network").UserName
End Function
The Control source = User_Name
Default Value = GetUserName()
The Problem -
The username is correctly Pulled in the form, however it is not being saved in the Control Source i.e in the Table. Even after I save and close the form.
I need the form to capture and save the Username every time someone makes changes to a record.
Please Help I am new to MS Access
It looks like you are assigning the user name to the control source. Access does not work like this. The control source is supposed to contain a column name.
Create a table having columns you can use to store things. Then set the form's Record Source property to this table name at design time in the properties window. Then set the Control Source of a text box to the column name where you want to store data or add objects to the form using the fields list.
Iin the form load event you can assign a user name to this textbox with
me!theTextBoxName = GetUserName()
You can also do this with an unbound textbox, but this name will not be stored when you close the form.
But as #June7 points out, you have probably done this already. In this case you should open the form with
DoCmd.OpenForm "theFormName", DataMode:=acFormAdd
... to create a new record.

Access VBA: Find if computer username is an active user

I'm using Access 2013.
I want to disable buttons on some forms based on the user that is logged into the computer.
I'm using code from Dev Ashish to determine the name of the logged in user to the computer.
The username is stored in global variable LoggedInUser
I have a table called "Users" in which I am storing the login emails for the admins who need expanded functions. This table along with other fields has the following fields:
"Email" Short Text
"Active" yes/no
I want to check if the logged in user exists in the users table and if they are marked as Active.
I then want a global boolean variable called AuthUser to hold a true/false for if the user exists in the table and is active or not.
I think this might need to be done with a dlookup or dcount, but I just can't seem to make it work - any ideas on a solution?
You haven't shown your query that doesn't work?
I can guess - and you can tell me I guessed wrong
Here's something you can play with
dim rs as DAO.Recordset
strSQL = "SELECT Username from Users WHERE [Username] = """ & LoggedInUser & """ AND [Active]"
set rs = currentdb.openrecordset(strSQL)
AuthUser = Not rs.EOF
rs.close

Associating user with login session in VB.NET

I'm creating a project in VB.NET with a MySQL back end. The user logs in via a login system created in VB.NET and the users details are stored in the database.
I was just wondering theoretically how the user that logged in can be associated with that particular session through the life time of that session? I need this to happen so I can do further queries once the user has logged in which involves that user :)
Thanks in advance,
Robin
Save the UserID in a Session Variable (Assuming that you have pulled the Users ID from the database, and it is stored in a variable I am calling userid):
HttpContext.Current.Session.Add("UserID", userid)
Then when you want to use it on any page (Assuming that you're userid is an Integer):
Dim userid as Integer = HttpContext.Current.Session["UserID"]
or in a Query String:
Dim strQuery As String = "SELECT * FROM TABLE WHERE USERID = " _
+ HTTPContext.Current.Session["UserID"].ToString() + ";"

Filter combobox on global variable

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?

Creating a submit button on a registration form in Microsoft Access

I dont have much Access experience. I'm an intern at a database management company and my boss gave me a test project. He wants me to create a registration form with a submit button to enter some data into a table. I created the form, but how do I create a submit button that enters data into the table?
If you are using straight Microsoft Access - you could base a form on the table (I know, not a terribly great idea for those in the know - just keeping it simple), and as you enter information into the form, it enters it into each respective column of the record - no submission action necessary, when you reach the end of the record - the next new record with blank columns is given to you automatically for filling in.
Now, if you -really- need a form that works like they do on the internet - you'd have to create an unbound form - then create a 'Submit' button with VBA code that takes the text boxes you have on the form and inserts them into the table in your database, clearing your fields after submission to allow for the entry of your next record. You can even write up a 'Cancel' button that clears all of your fields to start again if you so desire.
I am sorry if I am being very general - but you haven't given us a lot to go on.
As the others have said the way to go here is an unbound form. Here is a generic code template I use when connecting to a JET database. Note the use of DAO over ADO as DAO is faster when talking to a JET data source
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = DBEngine(0).OpenDatabase(strLinked_db_path)
Set rst = db.OpenRecordset("tblHours_lost", dbOpenTable)
With rst
.Addnew
!Hours_Lost = Me.txtHours_lost
!Type = Me.cboLoss_type
!do_not_deduct = Me.chkDo_not_deduct
!Notes = Me.txtNotes
!Start_time = Me.txtStart_time
.Update
End With
rst.Close
db.Close
Set rst = Nothing
Set db = Nothing
All you need to do is open the recordset and put the data in, its quite simple to do once you get the hang of it
You don't need to make a string date should be saved.
Microsoft access is designed to add in the properties from your form a table and then you can add a field and you don't have to make a Submit button the state should be saved, all strings will be done automatically by the system.
U need to create connection string which will provide path to to date
then create a command object then give the con object as parameter to command object along with command and execute it