473,396 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

connect to MS SQL with different user/password in VBA

matthardwick
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 6334
nico5038
3,080 Expert 2GB
Guess this link provides the needed information:
http://support.microsoft.com/kb/245587

Nic;o)
Jul 27 '08 #2
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 Expert 2GB
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
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 Expert 2GB
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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
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,...

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.