hello there,
please I need your support in editing SQL query via VBA,
i'm not familiar with SQL or VBA coding but after some serches into internet reached to have a code with SQL Connection and i set a SQL query into VBA.
what i want is I have 2 static criterias used in SQL "DATETIMEFROMPARTS(2016,5,11,16,30,0,0)" and "DATETIMEFROMPARTS(2016,5,11,19,15,0,0)"
is that available if can i set a specific 2 blank cells or popup form then fill them with inputs to be variable instead of this static criterias? and how can i perform that
gratefully,
please I need your support in editing SQL query via VBA,
i'm not familiar with SQL or VBA coding but after some serches into internet reached to have a code with SQL Connection and i set a SQL query into VBA.
what i want is I have 2 static criterias used in SQL "DATETIMEFROMPARTS(2016,5,11,16,30,0,0)" and "DATETIMEFROMPARTS(2016,5,11,19,15,0,0)"
is that available if can i set a specific 2 blank cells or popup form then fill them with inputs to be variable instead of this static criterias? and how can i perform that
Code:
Sub ConnectSqlServer()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=COMP1\SERVER1;" & _
"Initial Catalog=EGCAHCSGALT352\SQLEXPRESS;" & _
"Integrated Security=SSPI;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute("select * into test1 from [All VDN Calls]" & _
"where" & _
"DATETIMEFROMPARTS([All VDN Calls].year, [All VDN Calls].[New Month], [All VDN Calls].Day, [All VDN Calls].Hour, [All VDN Calls].Min, 0, 0)>=DATETIMEFROMPARTS(2016,5,11,16,30,0,0)" & _
"AND" & _
"DATETIMEFROMPARTS([All VDN Calls].year, [All VDN Calls].[New Month], [All VDN Calls].Day, [All VDN Calls].Hour, [All VDN Calls].Min, 0, 0)<=DATETIMEFROMPARTS(2016,5,11,19,15,0,0)" & _
"AND" & _
"VDN = 'SA_New_ar'" & _
";")
' Check we have data.
If Not rs.EOF Then
' Transfer result.
Sheets(1).Range("A1").CopyFromRecordset rs
' Close the recordset
rs.Close
Else
MsgBox "Error: No records returned.", vbCritical
End If
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
gratefully,