SQL statements again!

MarionD

Registered User.
Local time
Today, 20:20
Joined
Oct 10, 2000
Messages
425
Hi All,

So sorry to be back with the same theme, but somehow I just can't get the hang of these SQL Statements! I hope the kind person who helped last time is still around!

Set Gebuehr = db.OpenRecordset("SELECT * FROM tblxx_Gebuehr " _
& "WHERE tblxx_Gebuehr.tblxx_grabart_id= " & Grabart AND " _
& "tblxx_Gebuehr.tblxx_Gebuehrkategorie_ID= " & Kategorie AND " _
& "tblxx_Gebuehr.deleted=0 AND " _
& "(tblxx_Gebuehr.tblxx_friedhof_id Is Null OR tblxx_Gebuehr.tblxx_friedhof_id = " & Friedhof &"))"

Where Grabart, Friedhof and Kategorie are variables. I think it has something to do with the way I tray and break the lines up but have tried a million different ways!

Thanks a lot
Marion
 
Are Grabart, Friedhof and Kategorie numbers or text? If they are numbers that should work, but if they are text i think you need some extra ' s. It took me ages (and a lot of help from these forums) to get the hang of these, so i know how you feel!

If they are text then try this:

Set Gebuehr = db.OpenRecordset("SELECT * FROM tblxx_Gebuehr " _
& "WHERE tblxx_Gebuehr.tblxx_grabart_id= '" & Grabart & _
"'AND tblxx_Gebuehr.tblxx_Gebuehrkategorie_ID= '" & Kategorie & _
"'AND tblxx_Gebuehr.deleted=0 AND " _
& "(tblxx_Gebuehr.tblxx_friedhof_id Is Null OR tblxx_Gebuehr.tblxx_friedhof_id = '" & Friedhof & "'))"

Hope that helps!
 
Oops, 1st line should be:
Set Gebuehr = db.OpenRecordset("SELECT * FROM tblxx_Gebuehr " & _
and then remove the & at the start of the 2nd line.
 
Code:
Set Gebuehr = db.OpenRecordset("SELECT * FROM tblxx_Gebuehr " & _
 "WHERE tblxx_Gebuehr.tblxx_grabart_id= " & Grabart AND " & _
"tblxx_Gebuehr.tblxx_Gebuehrkategorie_ID= " & Kategorie AND " &  _
 "tblxx_Gebuehr.deleted=0 AND " & _
 "(tblxx_Gebuehr.tblxx_friedhof_id Is Null OR tblxx_Gebuehr.tblxx_friedhof_id = " & Friedhof &"))"
 
Hi branston - Thanks a mill but it still doesn't work. It wont compile. Attached the message (sorry about the German!)

The variables are all dimmed as Long and have the values 1 , 1, and 5
 
Umm.. sorry attachment - try again
 

Attachments

  • sql.JPG
    sql.JPG
    51.5 KB · Views: 106
hum, i obviously still ahvent got the hang of it! Does Aman's work? If not ill have another look!
 
Nope - sorry Aman - get the same message - won't compile
Thanks for the trouble
 
Hi

Try the following. As the variables are numbers so add &" marked in green in the code.

Code:
Set Gebuehr = db.OpenRecordset("SELECT * FROM tblxx_Gebuehr " & _
 "WHERE tblxx_Gebuehr.tblxx_grabart_id= " & Grabart [COLOR=green]& "[/COLOR] AND " & _
"tblxx_Gebuehr.tblxx_Gebuehrkategorie_ID= " & Kategorie [COLOR=green]&" [/COLOR]AND " &  _
 "tblxx_Gebuehr.deleted=0 AND " & _
 "(tblxx_Gebuehr.tblxx_friedhof_id Is Null OR tblxx_Gebuehr.tblxx_friedhof_id = " & Friedhof &")")
 
Last edited:
Ok try:

Set Gebuehr = ("SELECT * FROM tblxx_Gebuehr WHERE tblxx_Gebuehr.tblxx_grabart_id= " & Grabart & "AND tblxx_Gebuehr.tblxx_Gebuehrkategorie_ID= " & Kategorie & "AND tblxx_Gebuehr.deleted=0 AND IsNull(tblxx_Gebuehr.tblxx_friedhof_id) OR tblxx_Gebuehr.tblxx_friedhof_id = " & Friedhof & "))")

Im not too sure why you have the 2 )'s at the end. Maybe try removing them as well, so the end looks like & Friedhof)
 
Thanks guys it works like this:-

Set Gebuehr = db.OpenRecordset("SELECT * FROM tblxx_Gebuehr " & _
"WHERE tblxx_Gebuehr.tblxx_grabart_id= " & Grabart & " AND " & _
"tblxx_Gebuehr.tblxx_Gebuehrkategorie_ID= " & Kategorie & " AND " & _
"tblxx_Gebuehr.deleted=0 AND " & _
"(tblxx_Gebuehr.tblxx_friedhof_id Is Null OR tblxx_Gebuehr.tblxx_friedhof_id = " & Friedhof & ")")
 
Just another tip: Make your code readable ...

Something like this:
Code:
dim mySQL as string
mySQL = ""
mySQL = mySQL & " SELECT * "
mySQL = mySQL & " FROM  tblxx_Gebuehr "
mySQL = mySQL & " WHERE tblxx_Gebuehr.tblxx_grabart_id= " & Grabart
mySQL = mySQL & "   AND tblxx_Gebuehr.tblxx_Gebuehrkategorie_ID= " & Kategorie
mySQL = mySQL & "   AND tblxx_Gebuehr.deleted=0 "
mySQL = mySQL & "   AND (      tblxx_Gebuehr.tblxx_friedhof_id Is Null "
mySQL = mySQL & "           OR tblxx_Gebuehr.tblxx_friedhof_id = " & Friedhof & ")" 

Set Gebuehr = db.OpenRecordset(mySQL)

is a lot more readable and maintainable as now everyone can right off see whats goings ons...
 
Thanks for the tip - I appreciate all helpful comments!
 

Users who are viewing this thread

Back
Top Bottom