+ Reply to Thread
Results 1 to 2 of 2

Connections fail on some files, not others

Hybrid View

  1. #1
    Registered User
    Join Date
    10-01-2012
    Location
    molde
    MS-Off Ver
    Excel 2010
    Posts
    49

    Connections fail on some files, not others

    Hi
    I recorded a macro that should connect a table in my workbook to different Access databases. I changed some of the code so that it finds the filepath in a cell in one of the worksheets. The code works fine on meny different Access databases. But not on others. I can't figure out why...

        Range("Tabell_Database_1[[#Headers],[ID]]").Select
    
        With ActiveWorkbook.Connections(1).OLEDBConnection
    
            .BackgroundQuery = True
    
            .CommandText = Array("Resultater")
    
            .CommandType = xlCmdTable
    
            .Connection = Array( _
    
            "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=" & Worksheets("Samlet").Range("O1") & ";Mode=Share Deny Write;Extended Properties="""";Jet OLEDB:System database="""";Jet OLE" _
    
            , _
    
            "DB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Pa" _
    
            , _
    
            "rtial 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;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False" _
    
            )
    
            .RefreshOnFileOpen = False
    
            .SavePassword = False
    
            .SourceConnectionFile = ""
    
            .SourceDataFile = Worksheets("Samlet").Range("O1")
    
            .ServerCredentialsMethod = xlCredentialsMethodIntegrated
    
            .AlwaysUseConnectionFile = False
    
     
    
     
    
     
    
            End With
    
        With ActiveWorkbook.Connections(1)
    
            .Name = Worksheets("Samlet").Range("O2").Text
    
            .Description = ""
    
        End With
    
        ActiveWorkbook.Connections(1).Refresh

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: Connections fail on some files, not others

    The best thing to do with a connection string is to get the string, break it into pieces, change the parts you want and put them back together.
    Sub Change_Connection()
    Dim ConnString As String
    Dim ConnPieces() As String
    
    ' Get the connection string
    ConnString = ActiveWorkbook.Connections("qry_Model_D").ODBCConnection.Connection
    
    ' This is what the connection string looks like
    ' DSN=RPTPRD01;UID=myname;PWD=XXX;DBQ=RPTPRD01;DBA=W;APA=T;EXC=F;FEN=T;
    ' QTO=T;FRC=10;FDL=10;LOB=T;RST=T;BTD=F;BNF=F;BAM=IfAllSuccessful;NUM=NLS;DPM=F;MTS=T;MDI=F;
    ' CSR=F;FWC=F;FBS=64000;TLO=O;MLD=0;ODA=F;
    
    MsgBox ConnString ' So you can see it
    
     ' Split the connection string into pieces
    ConnPieces = Split(ConnString, ";")
    
    ' Change the parts you want
    ConnPieces(1) = "UID=NewName"   ' User Name
    ConnPieces(2) = "PWD=YYY"       ' Password
    
    ' Put it back together
    ConnString = Join(ConnPieces, ";")
    
    End Sub
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to make a cell display "FAIL" when range of cells is FAIL
    By crazychile in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-18-2017, 04:13 PM
  2. [SOLVED] deleting names associated with connections to CSV files
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-31-2016, 09:57 PM
  3. deleting connections to CSV files
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-09-2015, 01:11 PM
  4. [SOLVED] Need a formula for pass/fail based on no. of Y answers but automatically fail if 3a is N
    By Researcher1111 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 09-15-2013, 10:20 PM
  5. Replies: 2
    Last Post: 05-24-2012, 08:43 AM
  6. SQL connections
    By mholman in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-17-2009, 03:46 PM
  7. [SOLVED] remote connections
    By John A Grandy in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 05-25-2006, 08:10 PM

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