VBA - ODBC Query

Mattandy55

New Member
Joined
May 27, 2016
Messages
20
Hi there, I have the below code I have used for a long time on a specific report.. For some reason, when running and then doing some F8 trouble shooting, it seems to be skipping the .commandtext and the End With. So its not picking up all the results/fully updating. Any ideas as I am lost?

VBA Code:
    'HRMS Data
        Sheets("Qry1").Select
            Range("A3").Select
                With Selection.ListObject.QueryTable
                    .Connection = "ODBC;DRIVER=SQL Server;SERVER=SUN-SRV-SQL\SAGEHRMS;UID=XXXXX;PWD=XXXXX;APP=Microsoft Office;WSID=SUN-SR-RDS2;DATABASE=SageHRMS_Live"
                    .CommandText = "SELECT hbene.b_empno, hrpersnl.p_ssn, hrpersnl.p_fname, hrpersnl.p_mi, hrpersnl.p_lname, hbene.b_benecode, hbene.b_401eamt, hbene.b_401epct " _
                                 & "FROM SageHRMS_Live.dbo.hbene hbene, SageHRMS_Live.dbo.hrpersnl hrpersnl " _
                                 & "WHERE hrpersnl.p_empno = hbene.b_empno AND hbene.b_company = hrpersnl.p_company AND hrpersnl.p_active In ('A','L') AND hbene.b_beneid='S' AND hbene.b_effdate<={ts '2023-11-22 00:00:00'} AND (hbene.b_expdate Is Null or hbene.b_expdate>{ts '" & Sheets("Lists").Range("C1").Value & " 00:00:00'}) AND (hbene.b_401eamt<>0 or hbene.b_401epct<>0)"
                    .Refresh BackgroundQuery:=False
                End With
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,215,403
Messages
6,124,710
Members
449,182
Latest member
mrlanc20

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