+ Reply to Thread
Results 1 to 13 of 13

Variable in OLE DB Connection

Hybrid View

  1. #1
    Registered User
    Join Date
    02-16-2009
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel for Office 365
    Posts
    17

    Variable in OLE DB Connection

    Hi All,

    I have the below connection specified in the Data->Import External Data->Edit Query but I wish to replace the static SHADES799 with a variable Username that will pick up the Windows username of the person running the sheet. How and where can I make this change to achieve desired results?

    Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=d:\Documents and Settings\SHADES799\My Documents\Employee_RankingGrid.xls;Mode=ReadWrite;Extended Properties="HDR=YES;";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False
    Many Thanks for any help,
    Colm
    Last edited by VBA Noob; 02-18-2009 at 09:29 AM.

  2. #2
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Variable in OLE DB Connection

    Hi Colm

    Perhaps:

    Dim strUser As String, strConn As String
    
    strUser = Environ$("Username")
    
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=d:\Documents and Settings\" & strUser & "\My Documents\Employee_RankingGrid.xls;Mode=ReadWrite;Extended Properties="HDR=YES;";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"
    Richard
    Richard Schollar
    Microsoft MVP - Excel

  3. #3
    Registered User
    Join Date
    02-16-2009
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel for Office 365
    Posts
    17

    Re: Variable in OLE DB Connection

    Thanks Richard,

    This looks good, the only thing is I'm not sure where I can enter this code so that is uses the connection string.

    I've tried putting it into Workbook Open( ) but it didn't seem to work, either because of something I was doing wrong or some other reason.

    Thanks for your help,
    Colm

  4. #4
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Variable in OLE DB Connection

    Assuming that the querytable is located on sheet1 of your workbook, in the workbook open event you could then include the code thus:

    Private Sub Workbook_Open()
    Dim strUser As String, strConn As String
    
    strUser = Environ$("Username")
    
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=d:\Documents and Settings\" & strUser & "\My Documents\Employee_RankingGrid.xls;Mode=ReadWrite;Extended Properties="HDR=YES;";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"
    
    Worksheets("Sheet1").QueryTables(1).Connection = strConn
    End Sub
    Note that you will need to refresh the querytable to view the data coming from the new connection.

    Richard

  5. #5
    Registered User
    Join Date
    02-16-2009
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel for Office 365
    Posts
    17

    Re: Variable in OLE DB Connection

    Apologies Richard but I'm pretty new to this.

    Does it make a difference if my data is coming from a pivot table? I tried the following code:

    Worksheets("pivot").QueryTables(1).Connection = strConn
    and I got the error "Subscript out of Range"

    I also tried

    Worksheets("pivot").QueryTables("PivotTableSource").Connection = strConn
    and got the same error. I then tried

    Worksheets("pivot").PivotTable("PivotTableSource").Connection = strConn
    and got the message "Object doesn't support this property or method"

    Apologies if I am doing something stupid. Thanks again.

  6. #6
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Variable in OLE DB Connection

    Yes, pivot tables have a different object model to query tables, so you won't be able to use exactly the same code. Assuming Sheet1 contains your pivot table (amend as appropriate) can you place the following into a sub in a module in your workbook and run it and then copy and paste the contents of the new sheet created into your reply:

    Dim sdArray, i
    Set newSheet = ActiveWorkbook.Worksheets.Add
    sdArray = Worksheets("Sheet1").UsedRange.PivotTable.SourceData
    For i = LBound(sdArray) To UBound(sdArray)
        newSheet.Cells(i, 1) = sdArray(i)
    Next i
    Richard

  7. #7
    Registered User
    Join Date
    02-16-2009
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel for Office 365
    Posts
    17

    Re: Variable in OLE DB Connection

    sdArray = Worksheets("pivot").UsedRange.PivotTable.SourceData
    Returns the message:
    <Application-defined or object-defined error>
    I looked at the statment in Quick watch and everything seems fine up to Worksheets("pivot").UsedRange.PivotTable. but the SourceData contains the above text

    Thanks for your help again Richard, it is much appreciated.

  8. #8
    Registered User
    Join Date
    02-16-2009
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel for Office 365
    Posts
    17

    Re: Variable in OLE DB Connection

    I was doing some other reading because I didn't expect you to do all the work!!

    Anyway I found this link http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

    That says "This property is not available for OLE DB data sources." I am pretty sure mine is an OLE DB Data Source so possibly that is why
    sdArray = Worksheets("pivot").UsedRange.PivotTable.SourceData
    doesn't work.

  9. #9
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Variable in OLE DB Connection

    Colm

    Change just that line to:

    sdArray = Worksheets("pivot").PivotTables(1).SourceData

    EDIT: Sorry, didn't see your post. I didn't realise that restriction existed for OLE DB data sources. I will have to have a think on this.

  10. #10
    Registered User
    Join Date
    02-16-2009
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel for Office 365
    Posts
    17

    Re: Variable in OLE DB Connection

    Apologies, it is in the title of the thread but I didn't really make it clear on my first post. I'll continue searching myself. Thanks again!

  11. #11
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Variable in OLE DB Connection

    No, I knew you were using OLE DB (as per your thread title - I just didn't know about the restriction when trying to access the SourceData property). I will raise a question with some knowledgeable people and see if they can suggest an alternative.

    Richard

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1