0

I have code that is pulling in data from a SQL table, and using VB and a data validation List Box that allows the user to select a criteria then it should pull in the data associated with that criteria: but I am not getting any errors and it is running the code but not pulling in any data. Can someone let me know what I am doing wrong: Here is my example: I select scenario from the Data Validation List Box and get " No Records Returned" here is my code:

    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sConnString As String
    Dim sSQL As String
    Dim ceRg As Worksheet

    Set ceRg = ThisWorkbook.Worksheets("RG Schedule")

    If ceRg.FilterMode Then
        ceRg.ShowAllData
    End If
    ceRg.Range("A3").ClearContents

    ' Create the connection string.
    sConnString = "Provider=SQLOLEDB;Data Source=SQL\SQLEXPRESS;Initial Catalog=Sample;Trusted_Connection=yes;"


    ' Create the Connection and Recordset objects.
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset

    ' Open the connection and execute.
    conn.Open sConnString


    sSQL = "SELECT [ORDER #],[SCENARIO],[LOCATION],[FORM],[STATUS],"
    sSQL = sSQL & "CAST([Date Ordered] as date),"
    sSQL = sSQL & "[Design],[Cost],"
    sSQL = sSQL & "CAST([Critical Obligation Date] as date),"
    sSQL = sSQL & "[MGMT_APPROVED],[AUTHORIZATION_Y_N],[CUSTOMER]"
    sSQL = sSQL & "FROM [dbo].[vwRG_Schedule]"
    sSQL = sSQL & "WHERE [SCENARIO] = '" & Scenario & "' "
    sSQL = sSQL & "ORDER BY [ORDER #]"

    Set rs = conn.Execute(sSQL)

    ' Check we have data.
    If Not rs.EOF Then
        ' Transfer result.
        ceRg.Range("A3").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
4
  • 1
    Take the final value of sSQL and copy into your IDE and run it against your table. Does it return rows? (hint: no because there are syntax problems)
    – Jacob H
    Commented May 30, 2018 at 20:28
  • 1
    If you are getting to No Records Returned then it is most likely there are no matching records in your database. I would suggest you run the Sql directly in Sql Management Studio to confirm there is or is not any data
    – JayV
    Commented May 30, 2018 at 20:28
  • 1
    @JayV The error is from the MsgBox I think, not coming from SQL. But we have the same idea about testing the query on it's own.
    – Jacob H
    Commented May 30, 2018 at 20:29
  • Ok if I test this as a Select query I get data but in Excel VBA it says Provider cannot be found. It may not be properly installed. I am using SQL\SQLExpress not sure how to find the provider name or how to correct the syntax errors. In Sql I get an error with the Original Code beginning with DIm in VBA Provider can't be found. All I am trying to do is pull in data from a sql table using data validation.
    – Kat14
    Commented May 31, 2018 at 20:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.