Excel Connections

tg_et

New Member
Joined
Jul 17, 2018
Messages
1
I have an Excel file that pulls data frommultiple text files via the "Connections" menu. The problem I'mrunning into is I need to be able to change the path to the text files fromwithin VBA.

 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
you could put the Data paths into a cell. I have a sheet, Config, with a cell to the db.

In VBE , insert MODULE and paste the code below into it.
the connections are done via code
Code:
public goConn as ADODB.Connection
public gDb

'-----------------
sub ConnectDB()
'-----------------
'BE SURE ADO is put in VBE References:  alt-F11, tools, references.

gDB = sheets("Config").range("A2").value

sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & gDB
OLEDB:EngineType=4"
Set goConn = New ADODB.Connection
goConn.Open sConnect
end sub



'-----------------
sub GetLateRecords()
'-----------------
dim rst
dim sSql as string
 
sSql = "select * from table where [late]=true"
Set rst = getRst(sSql)

range("A1").select
ActiveCell.CopyFromRecordset rst
set rst = nothing
end subs


'-----------------
Public Function getRst(ByVal pvQry) As ADODB.recordset
'-----------------
Dim rst As ADODB.recordset

On Error GoTo errGetRst

If goConn Is Nothing Then ConnectDB

Set rst = CreateObject("ADODB.Recordset")
With rst
    Set .ActiveConnection = goConn
    .CursorLocation = adUseClient
    .Open pvQry
End With
Set getRst = rst

Exit Function
errGetRst:
MsgBox Err.Description, , "getRst():" & Err
End Function



'-----------------
Public Function RunActionQry(pvQry, Optional ByVal pbIsSql As Boolean)
'-----------------
On Error GoTo errRun

  ' Assign to ADO Command object
Set goCmd = New ADODB.Command
With goCmd
  .ActiveConnection = goConn
  .CommandText = pvQry
  
  If pbIsSql Then
    .CommandType = adCmdText
  Else
    .CommandType = adCmdStoredProc
  End If
  
  .Execute
End With
Exit Function

errRun:
RunActionQry = Err
End Function
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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