Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2023
    Posts
    15

    Automatic login for Access


    Hello everyone,
    I'm trying to create something that automatically writes the username and password to my databases when I open them.

    I found this piece of code:
    Open() method of the ADODB.Connection object:
    Provider=SQLOLEDB.1;Data Source=SqlServerComputerName;Initial
    Catalog=DatabaseName;User ID=MyAccount;Password=MyPassword

    The issue is; I have no idea how or where to implement this. My initial idea was to create an "opening form" which has a "when this form opens"- function that runs the code.
    I have been looking around the net and now I'm even more confused after researching. I've tried going through; https://learn.microsoft.com/en-us/tr...ordset-objects, but I'm still not sure where I need to write the code. The article refers to something called Northwind, which is not something I'm familiar with.

    Do I need some software to use the code? Maybe there is a file in Windows where I can edit this in? Or is there a simpler and easier method to achieve the same? (I want to note that I don't have the luxury of recreating every single database and check "save password").

    Thanks.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    i have an AUTOEXEC macro, that runs function STARTUP
    the inspects the users:



    Code:
    function Startup()
       'get users login id
    vUser = getUserID()
    
    
       'see if in the database
    If IsValidUser(vUser) then
        docmd.Openform "fMainMenu"
    else
       msgbox "You are not authorized",vbCritical, "Denied"
       docmd.Quit
    endif
    end function
    
    
    
    
    Public Function IsValidUser(byval pvUser) as boolean
    IsValidUser =not IsNull( dlookup("[UserID]","tblUsers","[UserID]='" & pvUser & "'"))
    End Function
    
    
    
    
    Public Function getUserID() As String
    getUserID = Environ("Username")
    End Function

  3. #3
    Join Date
    Mar 2023
    Posts
    15
    Quote Originally Posted by ranman256 View Post
    i have an AUTOEXEC macro, that runs function STARTUP
    the inspects the users:



    Code:
    function Startup()
       'get users login id
    vUser = getUserID()
    
    
       'see if in the database
    If IsValidUser(vUser) then
        docmd.Openform "fMainMenu"
    else
       msgbox "You are not authorized",vbCritical, "Denied"
       docmd.Quit
    endif
    end function
    
    
    
    
    Public Function IsValidUser(byval pvUser) as boolean
    IsValidUser =not IsNull( dlookup("[UserID]","tblUsers","[UserID]='" & pvUser & "'"))
    End Function
    
    
    
    
    Public Function getUserID() As String
    getUserID = Environ("Username")
    End Function
    Would this work when connecting to an SQL server? It's actually the SQL-server, I need to write the same password 3 times for, because Access connects to 3 different databases.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You download the Northwind DB and view how they did it. Though that just uses an Access BE?

    Not the latest and greatest Northwind Traders BTW.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Have a look at this thread and see if that helps.
    https://www.accessforums.net/showthread.php?t=87371
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Join Date
    Mar 2023
    Posts
    15
    Quote Originally Posted by Welshgasman View Post
    Have a look at this thread and see if that helps.
    https://www.accessforums.net/showthread.php?t=87371
    Thank you. I think it puts me on the right path. I'm still not sure where to write the code though. I'm used to tieing the code to fields, buttons etc. But I have no idea where to find a place to write code that runs as soon as I open the database. Is it as simple at creating a form that opens when the database does? and then tieing the code to the opening of the form?

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    As Ranman mentioned, you can create an AutoExec or put it in the first form that loads in the DB.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  9. #9
    Join Date
    Mar 2023
    Posts
    15
    Quote Originally Posted by Welshgasman View Post
    As Ranman mentioned, you can create an AutoExec or put it in the first form that loads in the DB.
    Thank you. That clarified it for me.

  10. #10
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    connect the SqlSvr tables via odbc. No need to login to each table, only once to open the app.

  11. #11
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    you can turn on the login audit on SQL server: https://learn.microsoft.com/en-us/sq...l-server-ver15
    or retrieve the information of active logins from sys.dm_exec_sessions . There you'll find all info for all active sessions on your server, included login name, login time, which database, used CPU time, hostname: from which computer did they logon, ect. If you create a log table and a procedure and job that writes this info every x minutes to the log table you have an excellent login audit.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Access Report Automatic Formatting
    By Rahat Manzoor in forum Reports
    Replies: 2
    Last Post: 11-23-2022, 02:18 AM
  2. Replies: 2
    Last Post: 08-31-2019, 12:08 PM
  3. Automatic Updation of PPT in Access
    By Mahendra1000 in forum Access
    Replies: 2
    Last Post: 07-22-2015, 02:59 AM
  4. Replies: 2
    Last Post: 04-17-2015, 10:59 AM
  5. ODBC Automatic login
    By Metrazal in forum Macros
    Replies: 3
    Last Post: 10-09-2014, 03:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums