SQL statement help needed for two conditions.

vishal120

New Member
Joined
Sep 17, 2009
Messages
37
Hi ,

I actually need help on the below sql statement. I am actually downloading data from access database to excel workbook based on two conditions. I am using the select statement where two conditions shall meet.

This is the part of the code where i am having compile error syntax error :

Code:
Set rs = CreateObject("ADODB.Recordset")
        
       rs.Open = "SELECT Sample_Auto_ref , Ref_Client, Ref_Karina, Saison, Marketing_Manager, Merchandiser, Client, Depart, Theme, Desc, Type_Echantillion,Taille,Qty,Keep_KI,Type_Lavage,Colori_Gmt_Dyed,Valeur_Ajouter,Date_request_Merc,Date_Livraison FROM Tbl_Sample_details order by Date_request_Merc desc WHERE Sample_Auto_ref = '" & Range("f1").Value & " & Merchandiser='" & Range("f3").Value ", cn, , , adCmdText

many thanks for the kind help in advance
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
The WHERE clause goes before the ORDER BY clause in a SQL statement.
 
Upvote 0
Try changing your SQL query as:

"SELECT Sample_Auto_ref , Ref_Client, Ref_Karina, Saison, Marketing_Manager, Merchandiser, Client, Depart, Theme, Desc, Type_Echantillion,Taille,Qty,Keep_KI,Type_Lavage,Colori_Gmt_Dyed,Valeur_Ajouter,Date_request_Merc,Date_Livraison FROM Tbl_Sample_details WHERE Sample_Auto_ref = '" & Range("f1").Value & "' and Merchandiser='" & Range("f3").Value "' order by Date_request_Merc desc"

You had ORDER BY before WHERE (as Joe mentioned), & instead of AND, and missing single quotes around string values.
 
Upvote 0
Hi ,

thanks for the quick reply, I have try as advise but is still having the errors as comile errors :

the code is as such:

rs.Open = "SELECT Sample_Auto_ref , Ref_Client, Ref_Karina, Saison, Marketing_Manager, Merchandiser, Client, Depart, Theme, Desc, Type_Echantillion,Taille,Qty,Keep_KI,Type_Lavage,Colori_Gmt_Dyed,Valeur_Ajouter,Date_request_Merc,Date_Livraison FROM Tbl_Sample_details WHERE Sample_Auto_ref = '" & Range("f1").Value & "' and Merchandiser='" & Range("f3").Value & "' order by Date_request_Merc desc", cn, , , adCmdText

thanks for advise
 
Upvote 0
You can also write the SQL code out to a message box to see if the code you are building looks as expected. i.e.
Code:
Dim mySQL as String
[COLOR=#333333]mySQL = "SELECT Sample_Auto_ref , Ref_Client, Ref_Karina, Saison, Marketing_Manager, Merchandiser, Client, Depart, Theme, Desc, Type_Echantillion,Taille,Qty,Keep_KI,Type_Lavage,Colori_Gmt_Dyed,Valeur_Ajouter,Date_request_Merc,Date_Livraison FROM Tbl_Sample_details WHERE Sample_Auto_ref = '" & Range("f1").Value & "' and Merchandiser='" & Range("f3").Value & "' order by Date_request_Merc desc"
MsgBox mySQL[/COLOR]
To verify it is good code, try running exact same code in Access and see if it works.

The way I usually approach this is to go to Access first, and come up with working SQL code. Once you have that, then mirror building that in Excel VBA. You can use MsgBox to see how close your code is to what it needs to look like.
 
Upvote 0
Hi ,

Thanks a lot for the help. I have been able to make it to work with the below code. Sharing it just in case someone else will be needing.

Code:
      ssql1 = "select Sample_Auto_ref,"
     ssql1 = ssql1 & "  Ref_Client,"
     ssql1 = ssql1 & "  Ref_Karina,"
     ssql1 = ssql1 & " saison, "
     ssql1 = ssql1 & " Marketing_Manager, "
     ssql1 = ssql1 & " Merchandiser, "
     ssql1 = ssql1 & " Client, "
     ssql1 = ssql1 & " Depart, "
     ssql1 = ssql1 & " Theme, "
     ssql1 = ssql1 & " Desc, "
     ssql1 = ssql1 & " Type_Echantillion, "
     ssql1 = ssql1 & " Taille, "
     ssql1 = ssql1 & " Qty, "
     ssql1 = ssql1 & " Keep_KI, "
     ssql1 = ssql1 & " Type_Lavage, "
     ssql1 = ssql1 & " Colori_Gmt_Dyed, "
     ssql1 = ssql1 & " Valeur_Ajouter, "
     ssql1 = ssql1 & " Date_request_Merc, "
     ssql1 = ssql1 & " Date_Livraison "
     ssql1 = ssql1 & " FROM tbl_sample_details WHERE"
     ssql1 = ssql1 & " SAISON='" & Range("E1").Value & "'  and Merchandiser='" & Range("G1").Value & "'order by Date_request_Merc desc"

thanks again and good day to all.

By the way how to mark the thread as solved.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,225,408
Messages
6,184,826
Members
453,262
Latest member
tifra

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