Results 1 to 8 of 8

Thread: [RESOLVED] Searching for compound names in a access db file?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [RESOLVED] Searching for compound names in a access db file?

    Hey guys I ws wondering. I have a database made in access 2000 that is called compound names.

    i use it in a program where the user enters a compound like FeO2 or something in text1 and press the search button. i would like it so it would search the database to look for the compound in text1 and if there is a match then put the compound name which is in the column debside the first colum that has compounds and put it in text2.

    If there is not a match found then have text2 say no matches found.

    I can use this code right?
    VB Code:
    1. Dim con As ADODB.Connection
    2. Dim strSQL As String
    3. Dim strCol As String
    4. Set con = New ADODB.Connection
    5. con.CursorLocation = adUseClient
    6. con.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.path & "\compoundname.mdb;" & "Jet OLEDB:Database Password=****;"
    7. strSQL = "SELECT * FROM table1"
    8. strCol = "compound"
    9. Call FillCombo(List2, con, strSQL, strCol)
    10. con.Close
    11. Set con = Nothing

    but see this code originally was for another prog i had, but i dont want it search using what is in list 2, i would like it to use text1

    Can you hook a brotha up? Thanks alot!!!!!

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

    Re: Searching for compound names in a access db file?

    ?????????????? ???????????? And what is the problem?

  3. #3
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Searching for compound names in a access db file?

    Missing WHERE clause in the SQL query?

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Searching for compound names in a access db file?

    Oh yea, the code I posted would load what is in a column in a database file into list2,

    so my problem is I need to modify the code so it searches for the compound is the first column of the compounds database file and if there is a match found between text1 and the first colum, then text2 would display what is in the other column for that row.

    How would I set up a Where clause in that code?

    thanks!

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Searching for compound names in a access db file?

    Spend some time checking out the tutorials in the "SQL" section of our Database Development FAQs/Tutorials (at the top of the Database Development forum)

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Searching for compound names in a access db file?

    Alrighty, I think I got it, but when I go to try it says the select statement contains reserved word or an argument name that is spelled wrong.

    heres the code I have now.

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim con As ADODB.Connection
    4. Dim rs As ADODB.Recordset
    5. Dim strSQL As String
    6. Dim strCol As String
    7. Set rs = New ADODB.Recordset
    8. 'Set con = ADODB.Connection
    9. Set con = New ADODB.Connection
    10. con.CursorLocation = adUseClient
    11.  
    12. con.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\compoundnamedb.mdb;" & "Jet OLEDB:Database Password=;"
    13. strSQL = "Select Compound, Compoundname, From Table1 " & _
    14. "Where (Compound = '" & Text1.Text & "')"
    15. rs.Open strSQL, con, adOpenStatic, adLockOptimistic
    16. ' then when your rs returns...
    17. If rs.EOF = True Then  ' might also be able to check if rs.RecordCount=0
    18.   Text2.Text = "Unknown"
    19.  
    20.  
    21. Else
    22.    ' note: use of & vbNullString below prevents errors if a field is null
    23.    rs.MoveFirst
    24.    Text2.Text = rs.Fields("Compoundname") & vbNullString
    25.  '  Label26.Caption = rs.Fields("TypeOfFile") & vbNullString
    26.  'Label36.Caption = rs.Fields("Description") & vbNullString
    27. End If
    28.  
    29.  
    30. rs.Close
    31. Set rs = Nothing
    32. End Sub

    When I go to debug it highlights the line

    rs.Open strSQL, con, adOpenStatic, adLockOptimistic

    I hope this code above searches for a match between text1 and the compound colum in the database file and if there is a match put what is in the same row but over in compoundname column in text2

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Searching for compound names in a access db file?

    The problem is a typo which causes a syntax error that confuses the database engine - you have a comma after the last field name in the Select list, so it thinks that "From" is also the name of a field, rather than the keyword as you intended.

    The rest of your code should work, but is not ideal. Here are a few things that it would be best to tidy up:
    • As you have declared and opened the connection in that routine, you should also close it properly (in the same way you have for the recordset).
    • There is no need for the rs.MoveFirst, as you are already there when the recordset is first opened (which was the last action that was done with it).
    • It is not a good idea to set the .CursorLocation to adUseClient unless you really need to, as it can cause problems and slow things down (particularly for multiple user systems). In this case there is no need for it.
    • The parameters you used for rs.Open are not suited to the way you are using the data - it would be better (quicker and fewer problems for multi-user systems) to specify the apt parameters, as explained here.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Searching for compound names in a access db file?

    Excellent, thanks for your help!

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