Results 1 to 9 of 9

Thread: [RESOLVED] recordset SQL (similar field in seperate tables rst!ansID)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Resolved [RESOLVED] recordset SQL (similar field in seperate tables rst!ansID)

    Hello there,

    i'm working on a recordset.Open (sql statement)

    st.Open "select * from qns_table, ans_table, ss_table where (some conditions)", cn, adOpenDynamic, adLockOptimistic

    and i'm adding them into a list box

    Me.List3.AddItem rst!ansID & ";" & etc...etc... (take note of ansID)

    the problem lies at 'ansID' being part of ans_table, as well as ss_table and when i add rst!ansID, the application gets confused because it doesn't know if this ansID belongs to either ans_table or ss_table.

    i have tried alternative methods
    i.e, rst!ans_table.ansID - apparently the syntax is not correct.

    can someone shed light here?

    Thank you very much =)

    Astro

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: recordset SQL (similar field in seperate tables rst!ansID)

    In your SQL specify the actual fields that you want, this way you can assign an alias for the field names.. for example

    "SELECT ans_table.a_ansID as ansID, ss_table.ansID as s_ansID..."

    You can also assign alias against the tables to help reduce the size of the sql string

    e.g.

    "SELECT a.ansID as a_ansID, s.ansID as s_ansID FROM ans_table a, ss_table s..."
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: recordset SQL (similar field in seperate tables rst!ansID)

    Hahaha that's absolutely brilliant!

    Ok erm one more? =)

    "select * from ans_table, ss_table where ss_table.qnsID = " & List1.ItemData(List1.ListIndex)

    Any ideas if this is likely to work? Tried several times, different ways.
    I'm a lil skeptical bout sql working with vba with =" & List.blah blah blah.

    Tough luck so far.

    Thanks

    Astro

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: recordset SQL (similar field in seperate tables rst!ansID)

    It will work if you are building the SQL before opening the recordset.. it won't work directly inside the SQL as you will need to refer to the form like..

    Forms!FormName!List1.ItemData(List1.ListIndex
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: recordset SQL (similar field in seperate tables rst!ansID)

    Bah! I'm quite helpless now Danny.

    rst.Open "select ss_table.ansID as ss_table_ansID, ans_table.answer as ans_table_answer from ans_table, ss_table where ans_table.ansID = ss_table.ansID and ss_table.qnsID =" & "List1.ItemData(List1.ListIndex)", cn, adOpenDynamic, adLockOptimistic
    rst.MoveFirst
    Do While Not rst.EOF
    Me.List3.AddItem rst!ss_table_ansID & ";" & rst!ans_table_answer & ";"

    any easier way to this?

    Thanks

  6. #6
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: recordset SQL (similar field in seperate tables rst!ansID)

    Seeing as you are opening the recordset with the sql then you will need to either code in the reference to the form like so..

    rst.Open "select ss_table.ansID as ss_table_ansID, ans_table.answer as
    ans_table_answer from ans_table, ss_table where ans_table.ansID =
    ss_table.ansID and ss_table.qnsID =" & Forms!FormName!List1.ItemData
    (List1.ListIndex), cn, adOpenDynamic, adLockOptimistic
    The FormName needs to replicate your form's name..

    Another way of doing this without using the form name is to create the sql as a string then open the recordset..

    VB Code:
    1. Dim strSQL as String
    2.   strSQL = "select ss_table.ansID as ss_table_ansID, ans_table.answer as " & _
    3.   "ans_table_answer from ans_table, ss_table where ans_table.ansID = " & _
    4.   "ss_table.ansID and ss_table.qnsID =" & List1.ItemData(List1.ListIndex)
    5.   rst.Open strSQL, cn, adOpenDynamic, adLockOptimistic

    It will also depend on the contents of the listbox and whether or not qnsID is numeric or text..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: recordset SQL (similar field in seperate tables rst!ansID)

    Private Sub List0_Click()

    Rem fetch
    Me.List1.RowSource = ""
    rst.Open "select [qns_table]qnsID from qns_table where [qns_table]qnsID =" & [Forms]![Form3]![List0.ItemData(List0.ListIndex)], cn, adOpenDynamic, adLockOptimistic
    rst.MoveFirst
    'Do While Not rst.EOF

    'Me.List1.AddItem rst!qns_table_qnsID & ";"
    'rst.MoveNext
    'Loop
    rst.Close
    End Sub

    I've tried both methods but still prefer the one which requires specifying the form.
    i'm getting closer with this. now it says that Access cannot read the List0.ItemData(List0.ListIndex).

    Thanks

    Astro

  8. #8
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: recordset SQL (similar field in seperate tables rst!ansID)

    not..

    ]![List0.ItemData(List0.ListIndex)],

    but

    ].List0.ItemData(List0.ListIndex),
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: recordset SQL (similar field in seperate tables rst!ansID)

    Hey Danny,

    I got it!

    Thanks

    Astro

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