|
-
Jul 29th, 1999, 08:33 AM
#1
Thread Starter
Junior Member
Can some one give me a code example of how to do this please? I have been trying to do this for WEEKS! with no luck. Help appreciated!
-
Jul 29th, 1999, 08:53 AM
#2
Thread Starter
Junior Member
This is in Visual Basic 6.0 btw
-
Jul 29th, 1999, 08:55 AM
#3
Thread Starter
Junior Member
Here is the query that I am trying to run:
SELECT Items.Item
FROM (Categories INNER JOIN [Sub Categories] ON Categories.CatID = [Sub Categories].CatID) INNER JOIN Items ON [Sub Categories].SubID = Items.SubID
WHERE ((([Sub Categories].[Sub Category])=[]));
For one: I need to be able to pass it a value?
For two: How can I get the results of that query in a list box?
-
Jul 29th, 1999, 05:41 PM
#4
Lively Member
Hi,
First, where is your query defined? In your database (I am assuming your are using access) or are you building the query on the fly?
Let's say you are building your query on the fly...
Dim db as database
dim rs as recordset
dim sSQL as string
Set db = Workspaces(0).OpenDatabase(app.path & "databasename.mdb")
sSQL = "SELECT Items.Item FROM " _
& "(Categories INNER JOIN " _
& "[Sub Categories] ON Categories.CatID = " _
& "[Sub Categories].CatID) INNER JOIN " _
& "Items ON [Sub Categories].SubID = " _
& "Items.SubID WHERE ((([Sub Categories]. " _
& "[Sub Category])=[" & variable & "]));"
'Note if Sub Category is a string you pass it surrounded by quotes:
& "[Sub Category])=['" & variable & "']));"
set rs=db.openrecordset(sSQL, dbopendynaset)
do until rs.eof
listbox.additem rs!field1 & vbtab & rsfield2 'etc...
'vbtab is for spacing
rs.movenext
loop
rs.close
db.close
That's it!!
Preeti
Set ProjectRs = ProjectDb.OpenRecordset(SQLStmt, dbOpenDynaset)
-
Jul 29th, 1999, 06:06 PM
#5
Thread Starter
Junior Member
-
Jul 29th, 1999, 10:17 PM
#6
Lively Member
Hi,
Sorry, I should have been more specific.
Second Problem:
rs!Field indicates the name of the field of your table. You replace the word "field" with the name of the field that you want to add to the listbox.
ie:
TableName: Temp
FieldName: ID
FieldName: Comment
list2.additem rs!Comment
'etc...
First Problem: You do not need to store the value in a variable, because you already have access to the value. The only reason you would need a variable is because you need this value later on which is probably not your case.Therefore, you can write:
currentindex = list1.text 'and it will work fine.
I hope that I've cleared up the problems.
Preeti
-
Jul 29th, 1999, 10:28 PM
#7
Thread Starter
Junior Member
Ok here is my edited code:
List2.Clear
Dim db As Database
Dim rs As Recordset
Dim sSQL As String
Dim currentindex As String
Set db = Workspaces(0).OpenDatabase("c:\inventory2.mdb")
currentindex = List1.Text
sSQL = "SELECT Items.Item FROM " _
& "(Categories INNER JOIN " _
& "[Sub Categories] ON Categories.CatID = " _
& "[Sub Categories].CatID) INNER JOIN " _
& "Items ON [Sub Categories].SubID = " _
& "Items.SubID WHERE (([Sub Categories]. " _
& "[Sub Category]=['" & currentindex & "']))"
Set rs = db.OpenRecordset(sSQL, dbOpenDynaset)
Do Until rs.EOF
List2.AddItem rs!("Item")
rs.MoveNext
Loop
rs.Close
db.Close
This is the error I get:
Type-decleration character does not match declared data type
what am I doing wrong?
-
Jul 29th, 1999, 10:35 PM
#8
Thread Starter
Junior Member
Preeti, Take a look at the new topic I added Need SERIOUS HELP!!
I stated out the whole project that I am trying to do, starting with the database design. Can you take a stab at it?
If you can't np, but I would really appreciate it if you would
-
Jul 29th, 1999, 10:43 PM
#9
Lively Member
When you are accessing the recordset field, there are two ways...
1: rs!fieldname
- OR -
2: rs("fieldname")
You can use one or the other, but not both!!
change rs!("Item") into the format of either option!! 
And yes I did read your other note, and if this is all you need done, then you can do it yourself - Just stop driving yourself crazy!
Preeti.
PS - Feel Free to email me. I left my email in your other message.
-
Jul 29th, 1999, 10:51 PM
#10
Thread Starter
Junior Member
Ok, did that and now I get the error message
This is a long one 
Invalid use of '.', '!', or '()' in query expression ([Sub Categories]. " _
& "[Sub Category]=['" & currentindex & "']))"
-
Jul 30th, 1999, 03:48 AM
#11
Member
Sorry man it takes too much of writing and explaining.
The Sql statement i wrote does same thing your Sql statement does. Just syntax difference.
If you have aol's Instant messenger my screen name is kishore111. you can send me a message and i can explain it clearly, Which will be more comfortable and cut this off.
I will be on online from 7:30pm to 8:30pm Eastern time.
Looking forward to talk.
-
Jul 30th, 1999, 11:20 AM
#12
Member
You are making the query complex
Change the Sql to the Following :
if currentindex is integer then use
following Sql
sSQL="Select I.Item from Categories C,Sub Categories Sc,Items I where C.Catid=SC.Catid and Sc.Subid=I.SubId and Sc.Sub Category=" & Currentindex
If currentindex is a string then use following SQl
sSQL="Select I.Item from Categories C,Sub Categories Sc,Items I where C.Catid=Sc.Catid and Sc.Subid=I.SubId and Sc.Sub Category='" & Currentindex & "'"
One More Suggestion:
It is good to Avoid spaces within FieldNames.
I hope this Helps
-
Jul 30th, 1999, 11:24 AM
#13
Thread Starter
Junior Member
I know that I am sounding REALLY dumb but, did you abbreviate some of that SQL statement? if so could you post the SQL statement in it's entirity please? Thanks
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
|