Results 1 to 9 of 9

Thread: [RESOLVED] Access & SQL problems, again.

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Resolved [RESOLVED] Access & SQL problems, again.

    Hello again, i have a table in an Access database holding entries from a diary. Each entry has a Subject, the Entry itself and the Username (The author of the entry).

    I want to do a search of the entire entry table, for one specific user, for example the user would be "Jim", and in VB i would use the .RecordCount to count the number of records in that query, thus showing how many entries the user has made.

    This current code will only show the user "Ben" to have 1 entry, where he really has 3? Any ideas.?

    VB Code:
    1. Sqltxt = "SELECT Entry.Username, Entry.Date, Entry.Subject, Entry.Entry"
    2. Sqltxt = sqltxt = " From Entry WHERE"
    3. Sqltxt = Sqltxt = " Entry.Username='" & UniUsername & "';"
    4. Set MySearch = MyDatabase.OpenRecordset(Sqltxt)
    5.    
    6. lblEntry.Caption = "You have " & MySearch.RecordCount & " entries"

    Thank you
    ILMV

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Access & SQL problems, again.

    Try this...

    VB Code:
    1. Sqltxt = "SELECT * From Entry WHERE Username = '" & UniUsername & "'"
    2. Set MySearch = MyDatabase.OpenRecordset(Sqltxt)
    3.    
    4. lblEntry.Caption = "You have " & MySearch.RecordCount & " entries"

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Access & SQL problems, again.

    The RecordCount might not work in all cases it all depends on which type of cursor you're using. Sometimes you also need to move to the last position in the recordset before this property shows the correct number of records. A better approach might be to use the COUNT SQL statement.
    VB Code:
    1. Sqltxt = "SELECT COUNT(Entry.UserName) AS EntryCount WHERE Entry.UserName = '" & UniUsername & "'"
    2. Set MySearch = MyDatabase.OpenRecordset(Sqltxt)
    3. lblEntry.Caption = "You have " & MySearch("EntryCount") & " entries"
    If you don't want to use that try to MoveLast and then MoveFirst in the recordset before you read the RecordCount.

  4. #4

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Access & SQL problems, again.

    nope im affraid that hasnt worked.

    Anything else :S

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Access & SQL problems, again.

    Have you verfied that Ben, in fact, has three entries?

    I'm not seeing why, using DAO as you appear to be, that your code won't work.
    Last edited by Hack; Jul 26th, 2005 at 06:09 AM.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Access & SQL problems, again.

    Quote Originally Posted by I_Love_My_Vans
    nope im affraid that hasnt worked.

    Anything else :S
    Was this referring to my reply or ....?

  7. #7

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Access & SQL problems, again.

    YEs Ben does have three entries, as you can see form the attached image.

    When i run the query like this...

    VB Code:
    1. sqltxt = "SELECT * From Entry"
    2. Set mysearch = myusers.OpenRecordset(Sqltxt)

    It returns 1 entry, which is incorrect as there are 4

    When i do it like this (No query, straight from the table)

    Set mysearch = myusers.OpenRecordset("Entry")

    It returns 4 entries, which is correct?
    Attached Images Attached Images  

  8. #8

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Access & SQL problems, again.

    sorry Joacim

    didnt see your post :S, will look at that now

  9. #9

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Access & SQL problems, again.

    Thanks Joacim!

    Didnt use you code, but gave me the idea to .MoveLast, and that has now worked.

    Thank you all

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