VBA - SQL Database - ActiveSheet.Name

Galviniser

New Member
Joined
May 31, 2019
Messages
3
Hi,

Straight to the point but here is the code

Code:
stADO = "Provider=SQLOLEDB;Data Source=UKD1-ENG-01;Initial Catalog=" & ActiveSheet.Name & "WWALMDB;User ID=SA;Password=*****"

Basically this works, however instead of it pulling from: (sheet name)WWALMDB i would like it to pull from WWALMDB(sheet name)

So when i tried this code:
Code:
stADO = "Provider=SQLOLEDB;Data Source=UKD1-ENG-01;Initial Catalog=" & "WWALMDB;User ID=SA;Password=*****" & ActiveSheet.Name

It did not work, i am getting an error saying:

(There has been an error during refresh: -2147217843 Login failed for user 'SA'.)

i have tried different variations of the code from putting the "ActiveSheet.Name" in different places

It also works if you type WWALMDB_Sheet1 where Sheet1 is the name of the sheet. But i would like it to automatically do it depending on the sheet name, not have to change the code every time.

I hope there is enough info here

Regards

Chris
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Basically this works, however instead of it pulling from: (sheet name)WWALMDB i would like it to pull from WWALMDB(sheet name)

Hi, welcome to the board :) - you can try like this:

Code:
STADO = "Provider=SQLOLEDB;Data Source=UKD1-ENG-01;Initial Catalog=WWALMDB" & ActiveSheet.Name & ";User ID=SA;Password=*****"

It also works if you type WWALMDB_Sheet1 where Sheet1 is the name of the sheet.


Or, if that underscore is important:

Code:
STADO = "Provider=SQLOLEDB;Data Source=UKD1-ENG-01;Initial Catalog=WWALMDB_" & ActiveSheet.Name & ";User ID=SA;Password=*****"
 
Last edited:
Upvote 0
Thank-you for your reply,

It has worked, which proves it was just syntax, the way i was writing the code

Really appreciate it

Regards

Chris
 
Upvote 0
Just to clarify, what is the ruling behind this syntax?

Hi, there's no rule as such, we are simply inserting the active sheet name into the string at the right place. It looks like you were trying to append it at the end of the string instead of after the "WWALMDB_" part.

Try googling "concatenating strings with variables in VBA" for some tutorials / explanations.
 
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,827
Members
449,051
Latest member
excelquestion515

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