473,513 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

connect to MS SQL with different user/password in VBA

matthardwick
19 New Member
Hi There,

I currently have all my tables in MS SQL and they are connecting in the standard way (used the external data section to link to the tables through ODBC in Office 2007 and the MS SQL 2005 Native Client).

I was wondering if there was a way in VBA before my database does anything, to specify a different username/password to connect to the SQL server with? I have 2 SQL Authentication usernames (one is read-only) and I have the two DSN files with the slightly different usernames and passwords in them.

Is there a way in VBA to specify different username and password OR to choose a different DSN file to use?

Thanks.

Matt.
Jul 27 '08 #1
6 6338
nico5038
3,080 Recognized Expert Specialist
Guess this link provides the needed information:
http://support.microsoft.com/kb/245587

Nic;o)
Jul 27 '08 #2
matthardwick
19 New Member
Thanks but that didn't work. It's really old code, and just wouldn't work in Access 2007 - threw no errors, but I was still getting prompted if there was no username and password, or it just wouldn't open the tables.
Jul 27 '08 #3
nico5038
3,080 Recognized Expert Specialist
Perhaps better to switch to an Access Data Project:
http://msdn.microsoft.com/library/de...essProject.asp
The data access section will show the needed Access 2007 connection code.

Nic;o)
Jul 27 '08 #4
matthardwick
19 New Member
I tried this:
Expand|Select|Wrap|Line Numbers
  1.    For Each X In CurrentDb.TableDefs
  2.     If Not X.Properties(4) = "" Then
  3.         strXProp = X.Properties(4)
  4.         strXProp = Replace(strXProp, "normal", "readonly")
  5.         strXProp = Replace(strXProp, "1234", "abcd")
  6.         X.Connect = strXProp
  7.         X.RefreshLink
  8.     End If
  9.    Next X
  10.  
but it doesn't work straight away. Instead it saves that to the tables properties (which is OK because I can just do the reverse when I need to) - but it means that I have to restart access, which isn't ideal.

I did look at an ADP - but coding in that would be quite different to a MDB (I already found a few functions that didn't work just in my basic testing)
Jul 27 '08 #5
nico5038
3,080 Recognized Expert Specialist
For relinking tables you can use this code:
Expand|Select|Wrap|Line Numbers
  1. Function fncRelink()
  2. 'function to relink tables to the "_be" database
  3. 'It's assumed that the "_be" database is in the same folder as the frontend !
  4.  
  5. Dim td As DAO.TableDef
  6.  
  7. For Each td In CurrentDb.TableDefs
  8.   If Len(td.Connect) > 0 Then
  9.      td.Connect = ";DATABASE=" & Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\")) & Mid(td.Connect, InStrRev(td.Connect, "\") + 1)
  10.      td.RefreshLink
  11.   End If
  12. Next
  13.  
  14. MsgBox "Ready, tables relinked"
  15.  
  16. End Function
  17.  
You'l have to edit the td.Connect to reflect the needed userid and password. Just open a linked table in design mode and check the properties, as it will show the link used.

Nic;o)
Jul 27 '08 #6
matthardwick
19 New Member
That's basically the same code, and it's what I started with (which didn't work) before I turned it into that. Property 4 is the same as connect.
All I did was just replace the old username and password with the new one.

And it's not another database that I need to connect to, it's the same SQL server - just need to change the username and password.

Thanks anyway.

Looks like we will just have to go with ADP.
Jul 27 '08 #7

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

Similar topics

0
2747
by: Bob Rivers | last post by:
Hi, I have an application that uses jdbc (9.2.0.3/classes12) to connect to an Oracle db (9i). It works fine, except when the user password is going to expire. Let me try to explain (sorry my...
0
2936
by: Bright | last post by:
Dear All I'm after a multi-User password database so that I can centrally store system passwords and give granular access to individuals based on their own unique authentication (possibly a...
1
1612
by: Daniel | last post by:
Hello everyone, I am trying to connect to a password protected Access database using: Public connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & pathDB & ";User...
8
11109
by: Bernard Lebel | last post by:
Hello, I would like to know if there is a way to run a Python file under a different user account than the one logged in. Allow me to explain. There are a bunch of people here, they are "basic...
9
15480
by: webrod | last post by:
Hi all, how can I check a user/password in a LDAP ? I don't want to connect with this user, I would like to connect to LDAP with a ADMIN_LOG/ADMIN_PWD, then do a query to find the user and...
1
7448
by: ranju | last post by:
I am trying to spawn a process (say an exe file) with different user crendentials than that of the current user. 1) Called LogonUserEx() to logon the user and recieve a handle to the token that...
1
15731
by: Oliver | last post by:
Newbie, looking at DB2 on AS400 iSeries, on Windows network. I have read that users are authenticated at the OS level, not at the database level. How do I make it possible for users to change...
0
1564
by: julia.druce | last post by:
I can't seem to launch a process as a different user and hide the window as well. If i don't specify the user, my window launches hidden. But when I specify a user, the executable.exe process...
4
3979
by: =?Utf-8?B?SHVleQ==?= | last post by:
I need to hide the new window started from the code below. If I run as is, the window is visible. If I comment out the code that sets a different user, domain, and password the window is hidden...
0
7259
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,...
1
7098
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...
0
7523
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...
0
5683
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,...
0
4745
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
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 ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
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...

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.