How do i use Thisworkbook.path with this VBA

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
720
Office Version
  1. 365
Platform
  1. Windows
Hi,
i have some code which works fine with a set URL but i want to substitute it to use the workbook path

ThisWorkbook.Path & "\MYDB\DATABASE1.mdb"



the code below
Code:
Sub getData1()


dat1 = Range("I2").Value


MYSQL = "SELECT * FROM `C:\test\MYDB\DATABASE1.mdb`.TABLE1 TABLE1WHERE TABLE1.EDAT=" & dat1 & " "


    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
        "ODBC;DSN=MS Access Database;DBQ=C:\test\MYDB\DATABASE1.mdb;DefaultDir=C:\test\MYDB\;DriverId=2;FIL=MS Access;MaxBufferSize" _
        ), Array("=2048;PageTimeout=5;")), Destination:=Range("$a$1")).QueryTable
        .CommandText = MYSQL
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = False
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
    
End Sub

i usually can use
DBlocation = ThisWorkbook.Path & "\MYDB\DATABASE1.mdb"

then substitute like & DBlocation & but this doesnt seem to work here

any help appreciated
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,827
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Are you sure that MYDB isn't already included in ThisWorkbook.Path?
What does

Code:
Sub Test1()
Debug.Print ThisWorkbook.Path
End Sub

give you?
 
Last edited:

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
720
Office Version
  1. 365
Platform
  1. Windows
Are you sure that MYDB isn't already included in ThisWorkbook.Path?
What does

Code:
Sub Test1()
Debug.Print ThisWorkbook.Path
End Sub

give you?

yes im sure

this outputs C:\test
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,337
Members
414,058
Latest member
Kate Fergie

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
Top