Username and Password SQL Connection

davie1982

Board Regular
Joined
Nov 19, 2007
Messages
170
Office Version
  1. 365
  2. 2019
Hi

Basically, i have an ODBC query and i would like to hardcode the server username and password into VBA.

Code:
--------------------------------------------------------------------------------


Dim qt As QueryTable
Set qt = ActiveSheet.QueryTables(1)
qt.CommandText = "SELECT slitemm.customer," _
& " [item_no] AS [Invoice Number], [refernce] AS [2nd Ref], " _
& "[dated] AS [Invoice Date], [kind] AS [Type], " _
& "[unall_amount] AS [Amount], slitemm.open_indicator" & vbNewLine _
& "FROM scheme.slitemm slitemm" & vbNewLine _
& sqlWH _
& "ORDER BY slitemm.customer, slitemm.dated, slitemm.item_no"
qt.Refresh

--------------------------------------------------------------------------------


The popup requires people to enter a username and password. I'd like that to be put in the code, but i don't know how to manage it.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi,

You could maybe approach authorisations a different by housing a list of authorised users in a sheet and having the code only run if there name appears on the list;

Use Environ("Username") to check the user.
 
Upvote 0
You need to look at the querytable's connection property. It will need to be something like:
Code:
ODBC;Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
 
Upvote 0
mm, it's not that.

When you go to refresh the data on the spreadsheet it requires a login and password to the SQL database which is always the same. Is there a way to store these 2 in the vb code so you don't need to put the password in?
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,298
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top