|
-
Jul 31st, 2002, 06:16 AM
#1
Thread Starter
Fanatic Member
Whats wrong with this SQL query?
This query doesen't work. Please, help me...
VB Code:
Dim db As Database
Dim rs As Recordset
Dim SQL, SQLWhere, Number1, Number2
If Len(Me![Text0]) > 0 Then
Number1 = "=""" & Me![Text0] & """"
Else
Number1 = "Is Null"
End If
If Len(Me![Text1]) > 0 Then
Number2 = "=""" & Me![Text1] & """"
Else
Number2 = "Is Null"
End If
SQL = "SELECT * FROM tbltable1 "
SQLWhere = "WHERE tbltable1.[Field1] " & "" & Number1 & " " & _
"AND tbltable1.[Field2] " & "" & Number2 & ";"
Set db = CurrentDb
Set rs = db.OpenRecordset(SQL & SQLWhere)
Last edited by Pirre001; Jul 31st, 2002 at 06:19 AM.
-
Jul 31st, 2002, 06:20 AM
#2
Retired VBF Adm1nistrator
Try this :
Debug.Print SQL & SQLWhere
And post the result here
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jul 31st, 2002, 06:21 AM
#3
Frenzied Member
To make it easier for us just post the finished query once it has been built up..
Also - you can use ' instead of " inside queries.. eg;
SQL = "SELECT * FROM tblTable1 WHERE Field1=' "+this+" ' "
this will stop you going mad with the multiple " required.
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Jul 31st, 2002, 06:22 AM
#4
-= B u g S l a y e r =-
think you are missing =
SQLWhere = "WHERE tbltable1.[Field1] " & "" & Number1 & " " & _
"AND tbltable1.[Field2] " & "" & Number2 & ";"
should be
SQLWhere = "WHERE tbltable1.[Field1] =" & "" & Number1 & " " & _
"AND tbltable1.[Field2] =" & "" & Number2 & ";"
assuming field1 and 2 are text fields and not numeric...
if numeric you also have to loose the ""
-
Jul 31st, 2002, 06:30 AM
#5
Thread Starter
Fanatic Member
Peet,
I "fix" = in this part of the code...
VB Code:
If Len(Me![Text0]) > 0 Then
Number1 = "=""" & Me![Text0] & """"
Else
Number1 = "Is Null"
End If
Or...
-
Jul 31st, 2002, 06:38 AM
#6
Addicted Member
if your field is text field try this:
VB Code:
If Len(Me![Text0]) > 0 Then
Number1 = "='" & Me![Text0] & "'"
Else
Number1 = "Is Null"
End If
If Len(Me![Text1]) > 0 Then
Number2 = "='" & Me![Text1] & "'"
Else
Number2 = "Is Null"
End If
SQL = "SELECT * FROM tbltable1 "
SQLWhere = "WHERE tbltable1.[Field1] " & "" & Number1 & " " & _
"AND tbltable1.[Field2] " & "" & Number2 & ";"
Set db = CurrentDb
Set rs = db.OpenRecordset(SQL & SQLWhere)
but if your fileds are numeric then u must loose the "'"
-
Jul 31st, 2002, 06:38 AM
#7
-= B u g S l a y e r =-
-
Jul 31st, 2002, 06:40 AM
#8
Thread Starter
Fanatic Member
Ok, peet. I do that..
-
Jul 31st, 2002, 06:56 AM
#9
Thread Starter
Fanatic Member
E-Link,
I shuld try your example also...
-
Aug 1st, 2002, 01:48 AM
#10
Thread Starter
Fanatic Member
This is exactly what I get with Debug.print.
VB Code:
SELECT * FROM tbltable1 WHERE tbltable1.[Field1] Is Null AND tbltable1.[Field2] ="D=8
L=30
U=37VAC
R=160W";
Why can´t I use BOF and EOF whith the code? Now I always get True on both EOF and BOF.
VB Code:
If Len(Me![Text0]) > 0 Then
Number1 = "='" & Me![Text0] & "'"
Else
Number1 = "Is Null"
End If
If Len(Me![Text1]) > 0 Then
Number2 = "='" & Me![Text1] & "'"
Else
Number2 = "Is Null"
End If
SQL = "SELECT * FROM tbltable1 "
SQLWhere = "WHERE tbltable1.[Field1] " & "" & Number1 & " " & _
"AND tbltable1.[Field2] " & "" & Number2 & ";"
Set db = CurrentDb
Set rs = db.OpenRecordset(SQL & SQLWhere)
If rs.BOF = False And rs.EOF = False Then
Msgbox "Test"
End if
Last edited by Pirre001; Aug 1st, 2002 at 02:57 AM.
-
Aug 1st, 2002, 02:28 AM
#11
Fanatic Member
If If rs.BOF = False And rs.EOF = False Then
Msgbox "Test"
End if
Did u use 2 If sentences ???
try to omit one.
Btw, what kind of cursor type do u use to open ur recordset ? if u're using adOpenKeyset, u can use
Code:
If rs.RecordCount > 0 Then
MsgBox "Test"
End If
-
Aug 1st, 2002, 03:23 AM
#12
Thread Starter
Fanatic Member
The only I wanna know...if I get any result of my query or not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|