473,785 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieve (Get) SQL Username from SQL Server using VBA code and Access 2003

Hi all

I am trying to retrieve the SQL username for the currently logged on
user from my backend SQL Database so i can populate a form.
I have the following 2 procedures. However obviously its always
returning the username SQL as per the connection string. Is there a
way of doing this for the CURRENTLY LOGGED IN USER?? if i remove
credentials from the connection string the procedure fails. Also if i
use 'CurrentProject .connection' this also fails as it refers to a JET
connection
Public Function DB_Connection() As Variant
'DB_Connection = CurrentProject. Connection
Dim strConnection As String
strConnection = "Driver={SQ L
Server};Server= servername;Data base=db;Uid=SQL ;Pwd=SQL;"
DB_Connection = strConnection
End Function
Function Current_SQL_Use r() As Variant
Dim strUserName As Variant
Dim rst As New ADODB.Recordset
With rst
.ActiveConnecti on = DB_Connection
.CursorLocation = adUseClient
.Source = "SELECT suser_sname() "
.Open
strUserName = .Fields(0)
.Close
End With
Set rst = Nothing
strUserName = Username
Current_SQL_Use r = strUserName
End Function
Thanks in advance

Mar 5 '07 #1
2 14101
On Mar 5, 11:51 am, boyl...@gmail.c om wrote:
Hi all

I am trying to retrieve the SQL username for the currently logged on
user from my backend SQL Database so i can populate a form.

I have the following 2 procedures. However obviously its always
returning the username SQL as per the connection string. Is there a
way of doing this for the CURRENTLY LOGGED IN USER?? if i remove
credentials from the connection string the procedure fails. Also if i
use 'CurrentProject .connection' this also fails as it refers to a JET
connection

Public Function DB_Connection() As Variant
'DB_Connection = CurrentProject. Connection
Dim strConnection As String
strConnection = "Driver={SQ L
Server};Server= servername;Data base=db;Uid=SQL ;Pwd=SQL;"
DB_Connection = strConnection
End Function

Function Current_SQL_Use r() As Variant
Dim strUserName As Variant
Dim rst As New ADODB.Recordset

With rst
.ActiveConnecti on = DB_Connection
.CursorLocation = adUseClient
.Source = "SELECT suser_sname() "
.Open
strUserName = .Fields(0)
.Close
End With
Set rst = Nothing

strUserName = Username
Current_SQL_Use r = strUserName

End Function

Thanks in advance
Does this help?
Function GetSqlUser() As String
Dim qrd As DAO.QueryDef, rst As DAO.Recordset

Set qrd = CurrentDb.Creat eQueryDef("")
With qrd
.Connect = CurrentDb.Table Defs(0).Connect
.SQL = "SELECT SYSTEM_USER"
.ReturnsRecords = True
End With

Set rst = qrd.OpenRecords et()
GetSqlUser = rst(0).Value

rst.Close
Set rst = Nothing
Set qrd = Nothing
End Function

Mar 5 '07 #2
hi this works perfectly.

How come you have to use DAO and not ADO, seems silly.

thanks a milion, was really starting to bug me :-)

On 5 Mar, 19:32, "Gord" <g...@kingston. netwrote:
On Mar 5, 11:51 am, boyl...@gmail.c om wrote:


Hi all
I am trying to retrieve the SQL username for the currently logged on
user from my backend SQL Database so i can populate a form.
I have the following 2 procedures. However obviously its always
returning the username SQL as per the connection string. Is there a
way of doing this for the CURRENTLY LOGGED IN USER?? if i remove
credentials from the connection string the procedure fails. Also if i
use 'CurrentProject .connection' this also fails as it refers to a JET
connection
Public Function DB_Connection() As Variant
'DB_Connection = CurrentProject. Connection
Dim strConnection As String
strConnection = "Driver={SQ L
Server};Server= servername;Data base=db;Uid=SQL ;Pwd=SQL;"
DB_Connection = strConnection
End Function
Function Current_SQL_Use r() As Variant
Dim strUserName As Variant
Dim rst As New ADODB.Recordset
With rst
.ActiveConnecti on = DB_Connection
.CursorLocation = adUseClient
.Source = "SELECT suser_sname() "
.Open
strUserName = .Fields(0)
.Close
End With
Set rst = Nothing
strUserName = Username
Current_SQL_Use r = strUserName
End Function
Thanks in advance

Does this help?

Function GetSqlUser() As String
Dim qrd As DAO.QueryDef, rst As DAO.Recordset

Set qrd = CurrentDb.Creat eQueryDef("")
With qrd
.Connect = CurrentDb.Table Defs(0).Connect
.SQL = "SELECT SYSTEM_USER"
.ReturnsRecords = True
End With

Set rst = qrd.OpenRecords et()
GetSqlUser = rst(0).Value

rst.Close
Set rst = Nothing
Set qrd = Nothing
End Function- Hide quoted text -

- Show quoted text -

Mar 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6204
by: Robin | last post by:
Hi all, I'm working with a VB app, using DAO 3.6, that used to generate Access '97 databases using the following code: Set dbsNew = wrkDefault.CreateDatabase(strLPAccessInv, dbLangGeneral, dbVersion30) I was able to have it create databases in Access 2000 by changing "dbVersion30" to "dbVersion40". Do I generate a database in Access
3
3455
by: nick | last post by:
Is possible for server side code to access the text of <asp:label> changed by client side javascript code?
5
6701
by: jhcorey | last post by:
We're using a third-party treeview and found that the functionality we need works only when we set the web site security to allow Anonymous Access. We also need the user's network id, which we could get from System.Web.HttpContext.User.Identity.Name if we don't allow AA. Is the user's id somehow still accessible in the app even though we're using Anonymous Access?
1
3808
by: jmarr02s | last post by:
Is it possible to limit the number of Subform records using MS Access 2003? That is, my end users want the capability of entering up to 12 records on their subform. Is that possible? Thanks!,
6
6085
by: mrowe | last post by:
I am working on a form in an Access 2003 database (using ADO). The detail section of my form has as checkbox named ‘Select’ that occurs once for each row of data. I have placed another checkbox in the header of the form named ‘SelectAll’. When ‘SelectAll’ is modified, I want to modify every occurrence of the ‘Select’ checkbox in the detail section. I looked through several posts and have played around with the code, but I have received an...
0
1754
by: PROGRAMMINGMAESTO | last post by:
How to Fetch and Retrieve image in ASP using MS Access. -------------------------------------------------------------------------------- Hello Friends, Can Anybody guide about how should i code to fetch image and save image in Access database. i have used OLE object data type in access database. Platform : ASP(VB Script) Database : Ms access please anybody can help me about this.
1
6015
by: huntress | last post by:
I am trying to create pivot tables using VBA in Access 2003. Is this possible? How do I go about it?
5
4135
by: Canuno | last post by:
Hi, I'm trying to send a small string to a serial port rs232 located in com 4. the string is: "ST GA#Ž" The Baud Rate is: 38400 Parity: None Data Bits: 8 Stops Bits: 1 Flow Control: None COM: 4
0
1124
by: Swan | last post by:
Can anyone plz tell me,Can I write file on server using binary access,or it is only to write on local?(Actually I am creating OCX for Http File Upload Control in VB using API's.So for that I need to raed file from local and write it to server.) Thanking you!
0
9480
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.