Page 1 of 2 12 LastLast
Results 1 to 40 of 58

Thread: [RESOLVED] Again Please tell me how to tell vb6 this?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Resolved [RESOLVED] Again Please tell me how to tell vb6 this?

    Hello
    Using Ado, please tell me how to tell vb6 that if all the values in a given column of access table are the same then msgbox "....." but if at least there is one different value in the same column then msgbox "....."

    I spent nearly a whole night trying and trying but without success.
    Example
    PID
    1
    1
    1
    1
    1
    msgbox "the values are the same in "PID"

    PID
    1
    1
    1
    2
    1
    3
    msgbox "the values are not the same in "PID"
    Is this possible?

    Thanks

  2. #2
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,254

    Re: Again Please tell me how to tell vb6 this?

    Well, one easy way is to execute this query

    SELECT PID FROM Table GROUP BY PID
    or
    SELECT DISTINCT PID FROM table

    then count the rows returned. 1 is good. More than 1 not so good.
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  3. #3
    Hyperactive Member
    Join Date
    Jul 2013
    Posts
    400

    Re: Again Please tell me how to tell vb6 this?

    Select distinct PID from ... > 1 ?

  4. #4
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,254

    Re: Again Please tell me how to tell vb6 this?

    Of course, you should prevent 'bad' data from getting into your tables in the first place rather than fire-fighting afterwards but sometimes it's unavoidable..
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: Again Please tell me how to tell vb6 this?

    Thank you all for the help
    But I don't know if what you are suggesting meets my need or not.
    The values in PID column are not necessarely constant.
    I mean they can be:
    PID
    2
    2
    2
    2
    3
    2
    4
    the query I need is to check if the values in PID are similar or not.
    I tested this query but I don't know wether I'm on the right path or not.
    RS.Open "Select distinct PID from T1 where PID > " & 1 , DB, adOpenStatic, adLockOptimistic
    If RS.RecordCount > 1 Then
    MsgBox "not same"
    Else
    MsgBox "same"
    End If
    But it always returns (same)
    thanks

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Again Please tell me how to tell vb6 this?

    just use: RS.Open "Select distinct PID from T1", DB, adOpenStatic, adLockOptimistic

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: Again Please tell me how to tell vb6 this?

    SELECT PID FROM Table GROUP BY PID

    I think it is working with this query
    Thank you a lot

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Again Please tell me how to tell vb6 this?

    Yes, that would work as well.

  9. #9
    Hyperactive Member
    Join Date
    Jul 2013
    Posts
    400

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by samer22 View Post
    SELECT PID FROM Table GROUP BY PID

    I think it is working with this query
    Thank you a lot
    I don't think so. Please check again

    This should work:
    Code:
    RS.Open "Select distinct PID from T1", DB, adOpenStatic, adLockOptimistic 
    If RS.RecordCount > 1 Then
    MsgBox "not same"
    Else
    MsgBox "same"
    End If

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Again Please tell me how to tell vb6 this?

    Carlos....BOTH "SELECT PID FROM T1 GROUP BY PID" and "SELECT DISTINCT PID FROM T1" WILL work. Why do you say the former will not????

  11. #11
    Addicted Member masoudk1990's Avatar
    Join Date
    Nov 2013
    Location
    Persia
    Posts
    172

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by samer22 View Post
    SELECT PID FROM Table GROUP BY PID
    A little performance
    SELECT 1 FROM Table GROUP BY PID
    Computer Enterprise Masoud Keshavarz
    For more information contact masoudk1990@yahoo.com

  12. #12
    gibra
    Guest

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by masoudk1990 View Post
    A little performance
    SELECT 1 FROM Table GROUP BY PID
    What?

  13. #13
    Addicted Member masoudk1990's Avatar
    Join Date
    Nov 2013
    Location
    Persia
    Posts
    172

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by gibra View Post
    What?
    There is a slight overhead when you use field name instead of 1
    Computer Enterprise Masoud Keshavarz
    For more information contact masoudk1990@yahoo.com

  14. #14
    Hyperactive Member
    Join Date
    Jul 2013
    Posts
    400

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by SamOscarBrown View Post
    Carlos....BOTH "SELECT PID FROM T1 GROUP BY PID" and "SELECT DISTINCT PID FROM T1" WILL work. Why do you say the former will not????
    Sam, I don't think I'm mistaken. Using the last sample data wouldn't the "group by" clause force the return 2,3,4?
    Testing with SQLite it works like this

  15. #15
    Hyperactive Member
    Join Date
    Jul 2013
    Posts
    400

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by masoudk1990 View Post
    There is a slight overhead when you use field name instead of 1
    How do you know PID is the first field?

  16. #16
    Addicted Member masoudk1990's Avatar
    Join Date
    Nov 2013
    Location
    Persia
    Posts
    172

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by Carlos Rocha View Post
    Sam, I don't think I'm mistaken. Using the last sample data wouldn't the "group by" clause force the return 2,3,4?
    Testing with SQLite it works like this
    Yes it returns 2,3,4. Which means after executing the query RecordCount will become equal to 3. And it means there is 3 distinct ID and its totally correct.
    Computer Enterprise Masoud Keshavarz
    For more information contact masoudk1990@yahoo.com

  17. #17
    Addicted Member masoudk1990's Avatar
    Join Date
    Nov 2013
    Location
    Persia
    Posts
    172

    Re: Again Please tell me how to tell vb6 this?

    How do you know PID is the first field?
    1 doest mean first column. Try this:

    Code:
    CREATE TABLE [dbo].[table1](
    	[a] [int] NOT NULL,
    	[b] [int] NULL
    ) ON [PRIMARY]
    GO
    INSERT [dbo].[table1] ([a], [b]) VALUES (0, 3)
    INSERT [dbo].[table1] ([a], [b]) VALUES (0, 3)
    INSERT [dbo].[table1] ([a], [b]) VALUES (0, 7)
    
    select 1 from table1 group by b
    Computer Enterprise Masoud Keshavarz
    For more information contact masoudk1990@yahoo.com

  18. #18
    Hyperactive Member
    Join Date
    Jul 2013
    Posts
    400

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by masoudk1990 View Post
    Yes it returns 2,3,4. Which means after executing the query RecordCount will become equal to 3. And it means there is 3 distinct ID and its totally correct.
    Oh, sorry. My bad

  19. #19
    Hyperactive Member
    Join Date
    Jul 2013
    Posts
    400

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by masoudk1990 View Post
    1 doest mean first column. Try this:

    Code:
    CREATE TABLE [dbo].[table1](
        [a] [int] NOT NULL,
        [b] [int] NULL
    ) ON [PRIMARY]
    GO
    INSERT [dbo].[table1] ([a], [b]) VALUES (0, 3)
    INSERT [dbo].[table1] ([a], [b]) VALUES (0, 3)
    INSERT [dbo].[table1] ([a], [b]) VALUES (0, 7)
    
    select 1 from table1 group by b
    masoudk1990, you are right again, sorry

  20. #20
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Again Please tell me how to tell vb6 this?

    Assuming this is SQL Server you can improve it further with a count distinct:-

    Code:
    Select Count(Distinct PID)
    From myTable
    That will return you a single cell table with the count of individual PIDs in it. I'm not sure if that will work with other DBs and I'm not sure whether it's in the Ansii standard or not so if you're using some other DB my best advice is just to try it out and see what happens.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  21. #21
    gibra
    Guest

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by masoudk1990 View Post
    1 doest mean first column. Try this:

    Code:
    CREATE TABLE [dbo].[table1](
        [a] [int] NOT NULL,
        [b] [int] NULL
    ) ON [PRIMARY]
    GO
    INSERT [dbo].[table1] ([a], [b]) VALUES (0, 3)
    INSERT [dbo].[table1] ([a], [b]) VALUES (0, 3)
    INSERT [dbo].[table1] ([a], [b]) VALUES (0, 7)
    
    select 1 from table1 group by b
    This doesn't work!

  22. #22
    Hyperactive Member
    Join Date
    Jul 2013
    Posts
    400

    Re: Again Please tell me how to tell vb6 this?

    Another option to improve performance would be to limit RS to 2, like in
    select 1 from T1 group by PID limit 2

  23. #23
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Again Please tell me how to tell vb6 this?

    Do you honestly think samer22 is concerned with performance? OP is obviously a beginner...I'd think it best just to advise on HOW to return what is needed, rather than getting into the weeds quite so much. If this was a thread on performance, I'd say ya'll are 'right on' with providing these insights, but maybe not for the OP (at least in this juncture of his/hers learning curve.

  24. #24
    gibra
    Guest

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by SamOscarBrown View Post
    Do you honestly think samer22 is concerned with performance? OP is obviously a beginner...I'd think it best just to advise on HOW to return what is needed, rather than getting into the weeds quite so much. If this was a thread on performance, I'd say ya'll are 'right on' with providing these insights, but maybe not for the OP (at least in this juncture of his/hers learning curve.
    I agree 100%.

  25. #25
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Again Please tell me how to tell vb6 this?

    This doesn't work!
    Works fine for me

    Do you honestly think samer22
    I sort of half agree. While I wouldn't want to blind him with science I do think the general principal of doing as much work as possible in the query and transmitting as little data back and forth across the connection is a good one for anyone to take on early and I don't think the examples given here have been difficult to follow.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  26. #26
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,254

    Re: Again Please tell me how to tell vb6 this?

    Quote Originally Posted by FunkyDexter View Post
    Assuming this is SQL Server you can improve it further with a count distinct:-

    Code:
    Select Count(Distinct PID)
    From myTable
    That will return you a single cell table with the count of individual PIDs in it. I'm not sure if that will work with other DBs and I'm not sure whether it's in the Ansii standard or not so if you're using some other DB my best advice is just to try it out and see what happens.
    That was my original thought but I wasn't sure how it translated to the OP's DBMS so I went with something more universal. There are a few ways to skin this particular cat!
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  27. #27
    Addicted Member masoudk1990's Avatar
    Join Date
    Nov 2013
    Location
    Persia
    Posts
    172

    Re: Again Please tell me how to tell vb6 this?

    Select Count(Distinct PID)
    From myTable
    Quote Originally Posted by ColinE66 View Post
    That was my original thought but I wasn't sure how it translated to the OP's DBMS so I went with something more universal. There are a few ways to skin this particular cat!
    Yes, I tested it and it didnt work on access 2010. But all other examples worked as well.
    Computer Enterprise Masoud Keshavarz
    For more information contact masoudk1990@yahoo.com

  28. #28

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: Again Please tell me how to tell vb6 this?

    Two days without internet
    Thank you all very much
    I'm so grateful to you
    Problem solved with both queries: distinct and group by
    Thank you again

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    Execuse me to open again this thread.
    I want to ask if this query is possible.
    Code:
    RS.Open "SELECT TEST FROM GradesTable GROUP BY TEST where PID like '" & Text2.Text & "'", DB, adOpenStatic, adLockOptimistic
    If RS.RecordCount > 1 Then
    I want via this query to check whether a given PID has values which are not all the same.
    example:
    PID TEST
    1 10
    1 10
    1 11
    2 12
    2 12
    2 12
    The example above shows that PID 2 has the same values however PID 2 has a different value.
    I tried with the query above but did succeed.
    I'm sorry again

  30. #30
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    Almost...do your "GROUP BY" after your "WHERE" statement.
    You didn't run into an error on that query of yours?

    you know...you REALLY should ask these DB questions in the Database Development Section.

    I'm going to ask an Admin to move'

  31. #31

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    SamOscarBrown
    Please I want to keep my post here because the members here know quite well that I'm just a beginner.
    For the query, Itested this query but it doesn't return correct results.
    Code:
    RS.Open "SELECT TEST FROM GradesTable where PID like '" & Text2.Text & "', GROUP BY TEST , DB, adOpenStatic, adLockOptimistic
    Thanks
    Last edited by samer22; Oct 27th, 2015 at 04:25 PM.

  32. #32
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,254

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    Lose the comma and put the GROUP BY within the double quotes.

    "SELECT TEST FROM GradesTable where PID = '" & Text2.Text & "' GROUP BY TEST"

    you don't need LIKE either, in this case. = will do.
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  33. #33

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    I think I got it
    This is the right syntax
    Code:
    RS.Open "SELECT TEST FROM GradesTable where PID like '" & Text2.Text & "', GROUP BY TEST , DB, adOpenStatic,
     adLockOptimistic
    Thank you very much all of you

  34. #34

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    ColinE66
    Sorry I have written my reply before I saw yours.
    When I put = instead of like I get an error
    type de données incompatible...
    Thanks

  35. #35

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    What about if we want to select more than a field in a distinct or group by query.

    Code:
    RS.Open "SELECT TEST, exam FROM GradesTable where PID like '" & Text2.Text & "', GROUP BY TEST , DB, adOpenStatic,
     adLockOptimistic
    Is this possible?

  36. #36
    Addicted Member masoudk1990's Avatar
    Join Date
    Nov 2013
    Location
    Persia
    Posts
    172

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    Quote Originally Posted by samer22 View Post
    What about if we want to select more than a field in a distinct or group by query.

    Code:
    RS.Open "SELECT TEST, exam FROM GradesTable where PID like '" & Text2.Text & "', GROUP BY TEST , DB, adOpenStatic,
     adLockOptimistic
    Is this possible?
    Yes, but not like this.
    When you use GROUP BY in your queries all columns in select section must be inside GROUP BY section too.

    Code:
    RS.Open "SELECT TEST, exam FROM GradesTable where PID like '" & Text2.Text & "', GROUP BY TEST,exam" , DB, adOpenStatic,
     adLockOptimistic
    Or if you dont want to include it in group by section you must use one of aggregate functions for it in select section.

    Code:
    RS.Open "SELECT TEST, COUNT(exam) FROM GradesTable where PID like '" & Text2.Text & "', GROUP BY TEST" , DB, adOpenStatic,
     adLockOptimistic
    By the way the LIKE you wrote make no sense and you can replace it with =

    To understand what like use for in queries read this:
    www.w3schools.com/sql/sql_like.asp
    Computer Enterprise Masoud Keshavarz
    For more information contact masoudk1990@yahoo.com

  37. #37
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    Please I want to keep my post here because the members here know quite well that I'm just a beginner.
    I've seen you say this before. You do know that the database section is part of this forum don't you. Anyone from this section who knows about databases will also visit the database section along with a bunch of other database experts who aren't necessarily VB6 experts and they will be every bit as sympathetic to the fact that you're a beginner. The database section is a far more suitable home for your thread and you'd be likely to get better help there.

    If you really want this left in the VB6 section I'm happy to respect that but I do think you're doing yourself a disservice. The answers you're getting in this section are very good but the ones you'd get in the DB section are likely to be even better.

    By the way, you really should specify what database you're using. An answer that is right for Access may be wrong for SQLServer or Oracle and vice versa. Without knowing which database your using we may accidentally give you bad advice.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  38. #38

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    masoudk1990
    Thanks a lot for the help.
    For this query it quite OK
    Code:
    RS.Open "SELECT TEST, exam FROM GradesTable where PID like '" & Text2.Text & "', GROUP BY TEST,exam" , DB, adOpenStatic,
     adLockOptimistic
    But for this one it isn't working.
    Code:
    RS.Open "SELECT TEST, COUNT(exam) FROM GradesTable where PID like '" & Text2.Text & "', GROUP BY TEST" , DB, adOpenStatic,
     adLockOptimistic
    I mean I can't retreive data from exam field on listview.
    Code:
    With .ListItems.Add(, , RS("TEST"))
                    .SubItems(1) = RS("EXAM")
    I can only see data from TEST field.

    For (LIKE) and '(=)
    When I use = I get error: (type de données incompatible)
    thanks a lot

  39. #39

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    Quote Originally Posted by FunkyDexter View Post
    I've seen you say this before. You do know that the database section is part of this forum don't you. Anyone from this section who knows about databases will also visit the database section along with a bunch of other database experts who aren't necessarily VB6 experts and they will be every bit as sympathetic to the fact that you're a beginner. The database section is a far more suitable home for your thread and you'd be likely to get better help there.

    If you really want this left in the VB6 section I'm happy to respect that but I do think you're doing yourself a disservice. The answers you're getting in this section are very good but the ones you'd get in the DB section are likely to be even better.

    By the way, you really should specify what database you're using. An answer that is right for Access may be wrong for SQLServer or Oracle and vice versa. Without knowing which database your using we may accidentally give you bad advice.
    I'm sorry if I have broken the rules of the forum
    I like this corner because the members here know me and understand my problems very well.
    But please do what you think is appropriate.

  40. #40
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: [RESOLVED] Again Please tell me how to tell vb6 this?

    I'm sorry if I have broken the rules of the forum
    You haven't broken the rules and I don't intend to come across as telling you off in any way. I'm just concerned that you're probably not getting the best out of the forum.

    If you post in the wrong section you're likely to get fewer and lower quality answers than if you post in the right one. (No disrespect meant to any of the contributors to this thread - the answers given here have been very good).

    I would hope that users in the database forum respect the fact that you're a beginner just as much as the users in here. If people don't respect that you're a beginner and you feel you're not being given the consideration you deserve then report it to the mods. Our aim is to create a friendly forum where people of all ability levels can be supported. We all start out as beginners so if we cut beginners out we end up with no members at all.

    As long as they are polite to others and follow the forum rules, everyone should be made to feel welcome in any part of the forum. I do not like the idea that anyone might be being made to feel unwelcome in any part of this forum and would want to deal with it appropriately.

    Anyway, on to your questions:-
    For this query it quite OK
    But for this one it isn't working
    I'm surprised that either works. Both have a comma before the group by which would be a syntax error in most forms of sql. If you remove the comma I would expect both to work fine.

    I mean I can't retreive data from exam field on listview.
    I think your field name probably isn't "Exam". In the first query you're selecting Exam so the field gets returned as Exam. In the second you're selecting Count(Exam) so some arbitrary field name is being assigned. Try aliasing the second query like this:-
    Code:
    SELECT TEST, COUNT(exam) as Exam...
    I think that will probably fix it.

    When I use = I get error: (type de données incompatible)
    That's a bit of a mystery. If you don't use a wildcard then a like and a = do exactly the same thing. What is the datatype of the PID field in the database and what is the value contained in Text2.Text? It sounds like they're incompatible but why they would then work with a Like statement is a mystery to me.


    And finally, what database are you using? It's really important that you let us know because the advice I'm giving is based on the assumption that you're using SQLServer while others are assuming you're using access. So at least one person in this thread is probably giving you bad (or at least, not the best) advice.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width