Question : GEt SQL data from Excel VBA

I've done a bit of googling but i can't find anything that seems usable.

We have a SQL Database and i'm trying to get Table Information from it.

Server Name:      QCSVR02
Password ect.:    Windows Authentication
DataBase Name: QCLIVE
Table Name:        Quick Corporate$Salesperson_Purchaser

If its any help we also use MS Dynamics Nav (Navision)
There are several Columns that i would need to filter by, but that would be optional.

Any starting points?

I'm reasonably good with VBA, not so much in SQL

Answer : GEt SQL data from Excel VBA

You said youre home at VBA

So here goes, create a new module, paste my code, and call it in any way you prefere.

I paste from row 2 to allow for VBA macro to write column names (hard coded).

/Marten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
Sub DataExtract()

' Create a connection object.
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

' Provide the connection string.
Dim strConn As String

'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"

'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=QCSVR02;INITIAL CATALOG=QCLIVE;"

'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"

'Now open the connection.
cn.Open strConn

' Create a recordset object.
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

With rs
    ' Assign the Connection object.
    .ActiveConnection = cn
    ' Extract the required records.
    .Open "SELECT * FROM [Quick Corporate$Salesperson_Purchaser]"
    ' Copy the records into cell A1 on Sheet1.
    Sheet1.Range("A2").CopyFromRecordset rs
    
    ' Tidy up
    .Close
End With

cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
Random Solutions  
 
programming4us programming4us