VBA Macro-Need Help with SQL Refresh

Cartolo

New Member
Joined
Jun 12, 2017
Messages
5
Hi,

I am trying to stop the designated ranges from auto-refreshing in the original VBA code. Whenever you put something in the highlighted ranges it auto-refreshes the file. Thanks for your help!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)If Not Intersect(Target, Range("C3:C6")) Is Nothing Then


Dim sql As String
Dim pn As String
Dim loc As String
Dim line As String
Dim strsql As String
Dim endsql As String
Dim pnsql As String
Dim locsql As String
Dim sdt As String
Dim midsql As String
Dim sdtgdate As String
Dim sdtfdate As String


pn = Range("C3").Value
loc = Range("C4").Value
line = Range("C5").Value
sdt = Range("C6").Value
strsql = " select Stuff "
midsql = " Stuff "
endsql = " Stuff "
trueendsql = " Stuff "


    If pn = "" Then pnsql = "" Else pnsql = " and i.invtid like '" + pn + "%'"
    If loc = "" Then locsql = "" Else locsql = " and l.whseloc like '" + loc + "%'"
    If line = "" Then linesql = "" Else linesql = " and i.classid like '" + line + "%'"
    If sdt = "" Then sdtfdate = "" Else sdtfdate = "where promdate<getdate()+" + sdt + ""
    If sdt = "" Then sdtgdate = "" Else sdtgdate = "and trandate>getdate()-" + sdt + ""
    
    sql = strsql + sdtfdate + midsql + sdtgdate + endsql + pnsql + locsql + linesql + trueendsql


       
    Range("A8").Select
    With Selection.QueryTable
        .Connection = Array( _
        "OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Business;Data Source=dbs02\sqle" _
        , _
        "xpress;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=OXN-WKSR5H;Use Encryption for             Data=False;" _
        , "Tag with column collation when possible=False")
        .CommandType = xlCmdSql
        .CommandText = sql
        .Refresh BackgroundQuery:=False
    End With
    
    Else
    End If
    Range("C3").Select
    
End Sub

Here is what the current range is and also the SQL query has been hidden.

Imgur: The most awesome images on the Internet
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
The rest of the code:

Code:
  If sdt = "" Then sdtgdate = "" Else sdtgdate = "and trandate>getdate()-" + sdt + ""    
    sql = strsql + sdtfdate + midsql + sdtgdate + endsql + pnsql + locsql + linesql + trueendsql


       
    Range("A8").Select
    With Selection.QueryTable
        .Connection = Array( _
        "DB;Provider=DB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=OPS;Data Source=dbs02\sqle" _
        , _
        "xpress;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=WKSR5H;Use Encryption for Data=False;" _
        , "Tag with column collation when possible=False")
        .CommandType = xlCmdSql
        .CommandText = sql
        .Refresh BackgroundQuery:=False
    End With
    
    Else
    End If
    Range("C3").Select
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
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