Create Temp Tables from Excel VBA

tjesus14

New Member
Joined
Apr 20, 2017
Messages
5
Hello All,

I am just try create a temp table from excel VBA. Below is the code i have which works for a select statement. But i want to create a temp table.

'Initializes variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String

'Setup the connection string for accessing MS SQL database
'Make sure to change:
'1: PASSWORD
'2: USERNAME
'3: REMOTE_IP_ADDRESS
'4: DATABASE
ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=Test;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=test;Use Encryption for Data=False;Tag with column collation when possible=false;Initial Catalog=test"

'Opens connection to the database
cnn.Open ConnectionString
'Timeout error in seconds for executing the entire query; this will run for 15 minutes before VBA timesout, but your database might timeout before this value
cnn.CommandTimeout = 900


'This is your actual MS SQL query that you need to run; you should check this query first using a more robust SQL editor (such as HeidiSQL) to ensure your query is valid
StrQuery = "SELECT Top 10 * FROM TicketHistory Where DateTime>'2017-01-01' Order By DateTime Asc"

'Performs the actual query
rst.Open StrQuery, cnn

For iCols = 0 To rst.Fields.Count - 1
Worksheets("Sheet1").Cells(1, iCols + 1).Value = rst.Fields(iCols).Name
Next


'Dumps all the results from the StrQuery into cell A2 of the first sheet in the active workbook
Sheets(1).Range("A2").CopyFromRecordset rst
End Sub


Create temp table code
Create Table #FraudMailFlow (
TicketHistoryID varchar(255), TicketID varchar(255), AgentAssigned varchar(255), MonthYear varchar(255), ActionTaken varchar(255), Status varchar(255), Mailbox varchar(255), TicketBucket varchar(255), DateTime varchar(255))
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,143,660
Messages
5,720,137
Members
422,267
Latest member
olund

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
Top