Link betweek SQl and EXCEL via VBA

Kend15

New Member
Joined
Jul 8, 2015
Messages
1
Hello All,
This is my first post and I got an Issue - Hopefully it will be a good start for me on this forum -
Issue: I'd like to program Select statement in VB where I have Querytext which is 'Select' statement and 'From' part - and then there is 'where' part - The first part will remain constant during the excercise. For Instance (Select Col A, B, C, D, E....n from Table name) and the second part is tricky (atleast for me) 'where' clause, I will have two arguments namely 'cutoff date' and IN <list of CustIDs>. Now there are more than 100K custIDs.
I want the program to format select statement, picks up custids from other tab of excel, saves everything on a text file and then sends the text file to sql, and in return i get the resultant data back in excel - all by clicking one button -
Considering I am new to VB domain, heres what I tried to wrote something. Also I have 64bit excel and using SQL 2012.
' Set file details
fileDir = "C:\Users\k****\Documents"
filePath = "C:\Users\k****\Documents" & Node & "sql.sql"
'check if directory exists, if not create it
If Dir(fileDir, cbDirectory) = "" Then
MkDir fileDir
End If
' open the file
Open filePath For Output As #1
'Write to file
outputText = sqlQuery3
Print #1, outputText
'open connection
sqlCon.ConnectionString = Conn
'Cn.CursorLocation = adUseClient
sqlCon.Open
'set and execute sql command
Set sqlCommand.ActiveConnection = sqlCon
sqlCommand.CommandText = sqlQuery3
sqlCommand.CommandType = adCmdText
sqlCommand.Execute
'open recordset
Set sqlRecordSet.ActiveConnection = sqlCon
sqlRecordSet.Open sqlCommand
'If
' Not sqlRecordset.EOF
'Then
'ActiveSheet.Range("A1").CopyFromRecordset sqlRecordSet
'Else Msgbox "No records returned!" End If
'copy data to excel
ActiveSheet.Range("A1").CopyFromRecordset (sqlRecordSet)
'close connections
sqlRecordSet.Close
sqlCon.Close
'Close file
Close #1
Sub Data_Fetching3()
End Sub

Any help would be appreciated -

Many Thanks,
K
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,215,429
Messages
6,124,837
Members
449,193
Latest member
MikeVol

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