Question : Trouble Converting SQL Server Query to MS Access

I'm having issues converting a SQL Server query to MS Access to use in my VB.NET program.

My original SQL Server query:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
cmd = New OdbcCommand("SELECT CD_WELL.well_common_name, " & _
        "CD_WELLBORE_FORMATION.formation_name, " & _
        "CD_WELLBORE_FORMATION.prognosed_md + {fn IFNULL(CD_DATUM.datum_elevation, 0)}, " & _
        "CD_WELLBORE_FORMATION.update_date " & _
        "FROM CD_WELL " & _
        "INNER JOIN CD_WELLBORE_FORMATION ON CD_WELL.well_id = CD_WELLBORE_FORMATION.well_id " & _
        "INNER JOIN (" & _
        "SELECT	well_id, " & _
        "MAX(update_date) Latestupdate_date " & _
        "FROM CD_WELLBORE_FORMATION " & _
        "GROUP BY " & _
        "well_id) CD_WELLBORE_FORMATION2 ON CD_WELLBORE_FORMATION.well_id = CD_WELLBORE_FORMATION2.well_id " & _
        "AND CD_WELLBORE_FORMATION.update_date = CD_WELLBORE_FORMATION2.Latestupdate_date " & _
        "INNER JOIN CD_SITE ON CD_WELL.site_id = CD_SITE.site_id " & _
        "INNER JOIN CD_DATUM ON CD_WELL.well_id = CD_DATUM.well_id " & _
        "WHERE UPPER(CD_SITE.site_name) LIKE UPPER('%" & TextPadName.Text & "%') " & _
        "AND ((({fn UCASE(CD_DATUM.is_default)} = 'Y') " & _
        "OR CD_DATUM.datum_id IS NULL " & _
        "OR ({fn CONCAT (CD_DATUM.well_id, CD_DATUM.datum_id)} IN ('03q2DecAl4nlEgX', '86zNT2KSuII6yfH', '8drumnJseey3L5f', '966uBIK5Xhtos05', " & _
        "'aaIx5gNxghWGqO0', 'AXm50zSOPrgZsZ8', 'dOBDPIleVGuPdj9', 'e2qQjvwmitACVhA', " & _
        "'e7aRS7VfTwfciQ1', 'FUtaJlUXErY5Qiy', 'iOVCMPIgTeX7Dub', 'JC1Rm8EZ9HmGttJ', " & _
        "'lsZtTPuanusLIL9', 'ntfL8vG8VsoG82r', 'rvPgl6F83dhPqcZ', 'RwGi6bVLL6W2d2i', " & _
        "'SJog0u94MuVqjWN', 'sSKqoFU4uaSbrde', 'uttJBp2CsFxWI9W', 'uWUtReyLy1h2WFd', " & _
        "'wJbDqiXc1wcEPQw', 'ww39g31VygDulen', 'YWwMjlUFn0WNMVP', 'ZILDpIPYMHfWqVj')))) " & _
        "ORDER BY 4 DESC;", conn2)


What I'm working on for MS Access:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
cmd = New OdbcCommand("SELECT dbo_CD_WELL.well_common_name, " & _
        "dbo_CD_WELLBORE_FORMATION.formation_name, " & _
        "dbo_CD_WELLBORE_FORMATION.prognosed_md + NZ(dbo_CD_DATUM.datum_elevation, 0), " & _
        "dbo_CD_WELLBORE_FORMATION.update_date " & _
        "FROM dbo_CD_WELL " & _
        "INNER JOIN dbo_CD_WELLBORE_FORMATION ON dbo_CD_WELL.well_id = dbo_CD_WELLBORE_FORMATION.well_id " & _
        "INNER JOIN (" & _
        "SELECT	well_id, " & _
        "MAX(update_date) AS Latestupdate_date " & _
        "FROM dbo_CD_WELLBORE_FORMATION " & _
        "GROUP BY " & _
        "well_id) AS CD_WELLBORE_FORMATION2 ON dbo_CD_WELLBORE_FORMATION.well_id = CD_WELLBORE_FORMATION2.well_id " & _
        "AND dbo_CD_WELLBORE_FORMATION.update_date = CD_WELLBORE_FORMATION2.Latestupdate_date " & _
        "INNER JOIN dbo_CD_SITE ON dbo_CD_WELL.site_id = dbo_CD_SITE.site_id " & _
        "INNER JOIN dbo_CD_DATUM ON dbo_CD_WELL.well_id = dbo_CD_DATUM.well_id " & _
        "WHERE UCASE(dbo_CD_SITE.site_name) LIKE UCASE('%" & TextPadName.Text & "%') " & _
        "AND (((UCASE(dbo_CD_DATUM.is_default) = 'Y') " & _
        "OR ISNULL(dbo_CD_DATUM.datum_id) " & _
        "OR dbo_CD_DATUM.well_id + dbo_CD_DATUM.datum_id IN ('03q2DecAl4nlEgX', '86zNT2KSuII6yfH', '8drumnJseey3L5f', '966uBIK5Xhtos05', " & _
        "'aaIx5gNxghWGqO0', 'AXm50zSOPrgZsZ8', 'dOBDPIleVGuPdj9', 'e2qQjvwmitACVhA', " & _
        "'e7aRS7VfTwfciQ1', 'FUtaJlUXErY5Qiy', 'iOVCMPIgTeX7Dub', 'JC1Rm8EZ9HmGttJ', " & _
        "'lsZtTPuanusLIL9', 'ntfL8vG8VsoG82r', 'rvPgl6F83dhPqcZ', 'RwGi6bVLL6W2d2i', " & _
        "'SJog0u94MuVqjWN', 'sSKqoFU4uaSbrde', 'uttJBp2CsFxWI9W', 'uWUtReyLy1h2WFd', " & _
        "'wJbDqiXc1wcEPQw', 'ww39g31VygDulen', 'YWwMjlUFn0WNMVP', 'ZILDpIPYMHfWqVj')))) " & _
        "ORDER BY 4 DESC;", conn2)


My MS Access version keeps telling me that I'm missing an operator, but I can't figure out what I'm missing.

Answer : Trouble Converting SQL Server Query to MS Access

Your SQL habits could use some improvement.  ;-)

You probably need to stop trying to use the column number in your ORDER BY clause and, instead of using the 4 use CD_WELLBORE_FORMATION.update_date (or whichever column name you are wanting to actually order by).

Also, look at the following:

dbo_CD_DATUM.well_id + dbo_CD_DATUM.datum_id

If those are character strings, then you need to use an ampersand (&) to concatenate them plus you probably want to wrap that in parenthesis:

OR (dbo_CD_DATUM.well_id + dbo_CD_DATUM.datum_id) IN ('03q2DecAl4nlEgX',
                                                          '86zNT2KSuII6yfH',
... , 'ZILDpIPYMHfWqVj')

You might want to put an alias on the following line:

dbo_CD_WELLBORE_FORMATION.prognosed_md + NZ(dbo_CD_DATUM.datum_elevation, 0),

Random Solutions  
 
programming4us programming4us