Results 1 to 6 of 6

Thread: [RESOLVED] Problem with complex SQL string

  1. #1

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Resolved [RESOLVED] Problem with complex SQL string

    I'm using VB6 with an Access DB.
    In one of my subs I have a complex SQL string.
    It is pulling up records but it's not returning one of the fields I need.

    rsUseA.Fields("TextID") is getting an 'item cannot be found' error.
    Which seems strange since the TextID field is being used in the condition statement of the SQL string.

    Any idea what's wrong?
    Here's most of the code:
    vb Code:
    1. ReDim Preserve g_tTexts(0) 'clear the utd
    2.     sSQL = "SELECT * FROM tblSelectedGroups SG, tblSelectedTexts ST, tblTexts T " & _
    3.             "WHERE (ST.UserID = " & CStr(g_lUserID) & _
    4.             ") AND (SG.UserID = " & CStr(g_lUserID) & _
    5.             ") AND (ST.USE = True) " & _
    6.             "AND (ST.TextID = T.TextID) " & _
    7.             "AND (SG.GroupID = T.GroupID)"
    8.     Set rsUseA = New ADODB.Recordset
    9.     rsUseA.Open sSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
    10.     If Not rsUseA.EOF Then
    11.         rsUseA.MoveFirst
    12.         Do Until rsUseA.EOF
    13.             g_tTexts(lCnt).TextID = rsUseA.Fields("TextID")
    14.             g_tTexts(lCnt).Msg = rsUseA.Fields("Text")
    15.             g_tTexts(lCnt).dtEarliest = rsUseA.Fields("EarliestStart")
    16.             g_tTexts(lCnt).dtLatest = rsUseA.Fields("LatestRun")

    I'm getting the error in line 13.
    All of the rest of the rsUseA.Fields are loading without a problem.

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Problem with complex SQL string

    It appears that you have the same a Field called "TextID" in the tblSelectedGroups, tblSelectedTexts and tblTexts Tables. You will need to explicitly specify table and field you want to use in your SQL String.
    Last edited by Mark Gambo; May 10th, 2007 at 01:01 PM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Problem with complex SQL string

    Try something like this:

    SQL Code:
    1. sSQL = "SELECT *, ST.UserID as TargetField FROM tblSelectedGroups SG, tblSelectedTexts ST, tblTexts T " & _
    2.             "WHERE (ST.UserID = " & CStr(g_lUserID) & _
    3.             ") AND (SG.UserID = " & CStr(g_lUserID) & _
    4.             ") AND (ST.USE = True) " & _
    5.             "AND (ST.TextID = T.TextID) " & _
    6.             "AND (SG.GroupID = T.GroupID)"

    and then try this:

    rsUseA.Fields("TargetField")
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Problem with complex SQL string

    Since you are using Access you can access fields with the same name using the TableAlias.FieldAlias syntax, in your case

    rsUseA.Fields("ST.TextID")
    rsUseA.Fields("T.TextID")

    Note this is an Access Jet Provider solution, other database providers do not include the TableAlias as part of the Recordset.Field.Name.

  5. #5
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Problem with complex SQL string

    Quote Originally Posted by brucevde
    Note this is an Access Jet Provider solution, other database providers do not include the TableAlias as part of the Recordset.Field.Name.
    @ Bruce - It would make life alot easier!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  6. #6

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Problem with complex SQL string

    Thx Mark and Bruce!

    Both methods work and I'm glad to learn both of them.

    (I did have to make a sm change to Mark's code, ST.UserID to ST.TextID)

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