Results 1 to 15 of 15

Thread: Use data from mdb table w/ another to display info

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    42

    Use data from mdb table w/ another to display info

    Ok, i wasn't sure how to sum this up in the title or even search this because i'm not sure what it would be called in VB or Access.

    What I have is a db with transactions done by about 5 or 6 users. Each user has their own user name and instead of displaying the user name i'd like to link it to another table that has their full name attached to their user name. (i.e. jsmith --> John Smith) and i would like to make a drop down box for a search/filter so that in this drop down box it pulls the full name instead of the user name.

    thanks in advance for any help.

  2. #2
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    509

    Re: Use data from mdb table w/ another to display info

    In your sql statement

    Say something like

    Code:
    mysql = "select * from table1, table2 INNER JOIN table1.code ON table2.code "
    Then in your drop down box
    Code:
    while not objrs.eof
    
    combobox1.additem (objrs![Name])
    wend

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    42

    Re: Use data from mdb table w/ another to display info

    when you say table1.code and table2.code what does the code stand for?

    I tried this and it gives me an error of : Run-time error '91' Object variable or With block variable not set

  4. #4
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Use data from mdb table w/ another to display info

    Quote Originally Posted by snipered
    Code:
    mysql = "select * from table1, table2 INNER JOIN table1.code ON table2.code "
    is assuming that table1 is the table with the transactions in and table2 the one with the user information. However the SQL is wrong. It should be
    Code:
    mysql = "select * from table1 INNER JOIN table2 on table1.code = table2.code "
    To make it clearer, if your table with the transactions in is tbl_Transactions and the other is tbl_Users, try this
    Code:
    mysql = "select tbl_Transactions.transaction, tbl_users.FullName from tbl_Transactions INNER JOIN tbl_Users ON tbl_Transactions.UserName = tbl_Users.UserName "

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    42

    Re: Use data from mdb table w/ another to display info

    great thank you, now on the drop down itself snipered said to put:
    VB Code:
    1. while not objrs.eof
    2.  
    3. combobox1.additem (objrs![Name])
    4. wend
    i have no idea how this works on this. i am pretty new to vb and as familiar with as i am with php/mysql so if you need to use that as a reference for explaining feel free.

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

    Re: Use data from mdb table w/ another to display info

    I'm guessing that objrs is a recordset object created somewhere in the code (not shown) like this
    VB Code:
    1. Dim objrs As ADODB.Recordset
    2. Set objrs = New ADODB.Recordset
    The recordset is then use to return the results of the SQL query.

    Name I'm further guessing is used as an example of the Db table field that stores the actual name you are going after.

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    42

    Re: Use data from mdb table w/ another to display info

    i think i'm doing this completly wrong:

    VB Code:
    1. Private Sub Combo1_Change()
    2. Dim objrs As ADODB.Recordset
    3. Set objrs = New ADODB.Recordset
    4.  
    5. objrs.Open "select bcm003.Username, Name.FullName from bcm_mt003 INNER JOIN name ON bcm003.UserName = Name.Username "
    6.  
    7. While Not objrs.EOF
    8.  
    9. combobox1.AddItem (objrs![UserName])
    10. Wend
    11. End Sub

    bcm_mt003 is the db
    bcm003 is the table that has the transaction in it
    Name is the username and full names are

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

    Re: Use data from mdb table w/ another to display info

    I would take it out of the change event. That is only going to fire if someone types in the combo box or pastes something into it.

    Put that code in your Form_Load event and give that a shot.

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    42

    Re: Use data from mdb table w/ another to display info

    ok, now i'm getting an error saying that the context of the .Open is incorrect. am i doing this correctly

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. Dim objrs As ADODB.Recordset
    4. Set objrs = New ADODB.Recordset
    5.  
    6. objrs.Open "select bcm003.Username, Name.FullName from bcm_mt003 INNER JOIN name ON bcm003.UserName = Name.Username "
    7.  
    8.  
    9. Call Connect
    10. RsSearch.Open "select * from bcm003", Cn, 2, 3
    11. RsSearch.MoveFirst
    12. Call DisplayRecord
    13. Grid1.Cols = 0
    14. Grid2.Cols = 0
    15. Grid3.Cols = 0
    16.  
    17.  
    18. 'mysql = "select bcm003.Username, Name.FullName from bcm_mt003 INNER JOIN name ON bcm003.UserName = Name.Username "
    19.  
    20.  
    21. End Sub
    22.  
    23.  
    24.  
    25. Private Sub Combo1_Change()
    26.  
    27. While Not objrs.EOF
    28.  
    29. combobox1.AddItem (objrs![UserName])
    30. Wend
    31. End Sub

  10. #10
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Use data from mdb table w/ another to display info

    The error might be caused by using "name" as a table name, as it is probably a SQL reserved word. Try enclosing it in square brackets: e.g. INNER JOIN [name] on ...

  11. #11
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Use data from mdb table w/ another to display info

    Also you need a second parameter to your objrs.open statement, saying what the connection object is.

  12. #12
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Use data from mdb table w/ another to display info

    Try this:
    VB Code:
    1. Public ADOCn As ADODB.Connection
    2. Private strConnString As String
    3. Private strSQL As String
    4.  
    5. Private Sub Form_Load()
    6. Dim rs As ADODB.Recordset
    7. strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[I]put your databse name and path here[/I];Persist Security Info=False"
    8. Set ADOCn = New ADODB.Connection
    9. ADOCn.ConnectionString = strConnString
    10. ADOCn.Open strConnString
    11.  
    12. Dim objrs As ADODB.Recordset
    13. Set objrs = New ADODB.Recordset
    14.  
    15. objrs.Open "select bcm003.Username, [Name].FullName from bcm_mt003 INNER JOIN [name] ON bcm003.UserName = [Name].Username ", ADOCn
    16. End Sub
    17.  
    18.  
    19.  
    20. Private Sub Combo1_Change()
    21.  
    22. While Not objrs.EOF
    23.  
    24. combobox1.AddItem (objrs![UserName])
    25. Wend
    26. End Sub

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    42

    Re: Use data from mdb table w/ another to display info

    Now it's giving me an error saying that "Syntax error in JOIN operation"

  14. #14
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Use data from mdb table w/ another to display info

    Have you tried your SQL string in the Access query designer?

  15. #15

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    42

    Re: Use data from mdb table w/ another to display info

    Ok, i fixed the SQL string (as dumb as it sounds i didn't realize you could do that, thanks for the tip) I'm new to access.

    However, it's still not populating anything into the drop down box.

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