0

I'm trying to work out why this query does not return results. The same query works fine in Access but not in VB.Net. Any help would be appreciated. The code Fails when trying to fill the data set. Thanks.

Function Populate_Month()

    Dim dbConnection As New OleDbConnection
    Dim dbConnectionStr As String
    Dim dbProvider As String
    Dim dbSource As String

    dbProvider = "Microsoft.Jet.OLEDB.4.0;"
    dbSource = "C:\Users\Scraps\Documents\Gossamer.mdb"

    dbConnectionStr = "Provider=" & dbProvider & "Data Source=" & dbSource
    dbConnection = New OleDbConnection(dbConnectionStr)
    dbConnection.Open()

    Dim dbAdapter As OleDbDataAdapter
    Dim dbDataSet = New DataSet
    Dim dbQueryStr As String
    Dim CurrentRow As Integer

    dbQueryStr = "SELECT * FROM Forecast_TDL" & _
         "WHERE Forecast_TDL.EIAC & Forecast_TDL.LCN & Forecast_TDL.Servicing & Forecast_TDL.Interval & Forecast_TDL.Interval_Type" & _
         "NOT IN (SELECT EIAC & LCN & Servicing & Interval & Interval_Type FROM Grouped_Servicings);"

    dbAdapter = New OleDbDataAdapter(dbQueryStr, dbConnection)

    dbAdapter.Fill(dbDataSet, "forecast")
    CurrentRow = 0
    MsgBox(dbDataSet.Tables("forecast").Rows(CurrentRow)("Interval"))

    dbConnection.Close()
    Return Nothing

End Function
4
  • Fails how? We don't like to guess stuff here.
    – rory.ap
    Mar 7, 2015 at 13:49
  • start by turning on Option Strict Mar 7, 2015 at 13:50
  • The Error is this "IErrorInfo.GetDescription failed with E_FAIL(0x80004005)." The code works fine when Querying just a Table eg "SELECT * FROM Forecast_TDL" So for a lack of information this is the first time I have worked with SQL in VB.Net I turned on Option Strict just now.
    – ZedsDead
    Mar 7, 2015 at 13:54
  • I think the problem is with the query itself but as I have not much experience with this I can not find the problem. So I suppose the better question to ask is, given the above query can anyone see anything that would stop it from returning a result in a VB.Net setting?
    – ZedsDead
    Mar 7, 2015 at 14:00

2 Answers 2

0

There is no way that works in MS Access either. You would have to do something like:

SELECT * FROM Forecast_TDL 
WHERE Forecast_TDL.EIAC NOT IN(SELECT EIAC FROM Grouped_Servicings) AND 
Forecast_TDL.LCN NOT IN(SELECT LCN FROM Grouped_Servicings) AND
Forecast_TDL.Servicing NOT IN(SELECT Servicing FROM Grouped_Servicings) And 
Forecast_TDL.Interval NOT IN(SELECT Interval FROM Grouped_Servicings) And 
Forecast_TDL.Interval_Type NOT IN (SELECT Interval_Type FROM Grouped_Servicings)

After understanding the question better, you can avoid comparing concatenated strings by using a JOIN and finding where a row doesn't exist.

SELECT * FROM Forecast_TDL
LEFT OUTER JOIN Grouped_Servicings
ON Forecast_TDL.EIAC = Grouped_Servicings.EIAC AND
 Forecast_TDL.LCN = Grouped_Servicings.LCN AND
 Forecast_TDL.Servicing = Grouped_Servicings.Servicing AND
 Forecast_TDL.Interval = Grouped_Servicings.Interval AND
 Forecast_TDL.Interval_Type = Grouped_Servicings.Interval_Type
WHERE Grouped_Servicings.EIAC IS NULL OR
      Grouped_Servicing.LCN IS NULL OR
      Grouped_Servicing.Servicing IS NULL OR
      Grouped_Servicing.Interval IS NULL OR
      Grouped_Servicing.Interval_Type IS NULL
10
  • My Query DOES work in access! but I will see if this also works thankyou.
    – ZedsDead
    Mar 7, 2015 at 15:02
  • Your Query does not provide any results in MS ACCESS. My Query concatenates a row of fields to find if a concatenation of the same fields is not in another table.
    – ZedsDead
    Mar 7, 2015 at 15:06
  • @ZedsDead ok, that makes sense now. I didn't get what you are trying to do but now I understand. You might have to place single quotes around the concatenation of fields.
    – Crowcoder
    Mar 7, 2015 at 15:11
  • dbQueryStr = "SELECT * FROM Forecast_TDL" & _ " WHERE 'Forecast_TDL.EIAC & Forecast_TDL.LCN & Forecast_TDL.Servicing & Forecast_TDL.Interval & Forecast_TDL.Interval_Type'" & _ " NOT IN (SELECT 'EIAC & LCN & Servicing & Interval & Interval_Type' FROM Grouped_Servicings)"
    – ZedsDead
    Mar 7, 2015 at 15:18
  • WOOP thank you the above worked perfectly I really appreciate the help. I would never have figured that out by myself. Can you please explain to me the need of the single quotes in VB.Net.
    – ZedsDead
    Mar 7, 2015 at 15:19
0

From my point of view you need spaces before the clauses like "Where" and "Not in". And remove the semicolone at end of the query.

2
  • Thanks I added the spaces and removed the ";" but the error hasn't changed. I also just wrote it out on one Line to see if that made a difference but no luck. :(
    – ZedsDead
    Mar 7, 2015 at 14:16
  • IN SQL Statement Remove & by "And" in 2nd line and in 3rd line write "Comma" in lieu of "&". The statement should be bQueryStr = "SELECT * FROM Forecast_TDL " & _ "WHERE Forecast_TDL.EIAC And Forecast_TDL.LCN And Forecast_TDL.Servicing And Forecast_TDL.Interval And Forecast_TDL.Interval_Type " & _ "NOT IN (SELECT EIAC , LCN , Servicing , Interval , Interval_Type FROM Grouped_Servicings)"
    – Sanu
    Mar 7, 2015 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.