Results 1 to 17 of 17

Thread: [RESOLVED] How can I find something in a field

  1. #1

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Resolved [RESOLVED] How can I find something in a field

    I have an access database with many fields (Client, City, House Model and many others...)

    In the "Client" field, there's the Name and the First Name (for example Jack Strap)

    I want to find in my database all records with the name Strap in the "Client" field ?

    Help me ! Thanks !

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: How can I find something in a field

    Use Like operator.
    VB Code:
    1. Select * from YourTable Where Client Like "*Strap*"
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: How can I find something in a field

    I'm not really sure if I understand...I have only to do this:
    VB Code:
    1. Data1.Recordset.MoveFirst
    2.  
    3. Do
    4.  
    5.     If Data1.Recordset.Fields("Client").Value = "*Strap*" Then
    6.       msgbox "Item found"
    7.     End If
    8.  
    9.     Data1.Recordset.MoveNext
    10.    
    11.     If Data1.Recordset.EOF = True Then
    12.       Exit Do
    13.     End If
    14.  
    15.   Loop

  4. #4
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Re: How can I find something in a field

    VB Code:
    1. MyRst.open "select * from myTable where firstname like %'Strep' %'"

    This will return all records from firstname field where strep is exist .

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: How can I find something in a field

    Quote Originally Posted by DubweiserTM
    I'm not really sure if I understand...I have only to do this:
    VB Code:
    1. Data1.Recordset.MoveFirst
    2.  
    3. Do
    4.  
    5.     If Data1.Recordset.Fields("Client").Value = "*Strap*" Then
    6.       msgbox "Item found"
    7.     End If
    8.  
    9.     Data1.Recordset.MoveNext
    10.    
    11.     If Data1.Recordset.EOF = True Then
    12.       Exit Do
    13.     End If
    14.  
    15.   Loop
    Seems like you are using Data Controls.

    You should ne using the ADODB connection and recordsets in VB and not Data Controls. Data Controls are no good. Search this form and you will get lot of samples on how to use ADO.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: How can I find something in a field

    I dont know how to use this, but I found a tutorial on this...

    Is it really better than DAO ?

    Thanks for your suggestion !

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

    Re: How can I find something in a field

    Quote Originally Posted by DubweiserTM
    Is it really better than DAO ?
    Yes.

    In addition, you will find the doing everything through code rather than using bound controls will give you a lot more flexibility and control over what your program does, how it does it and when it does it.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How can I find something in a field

    Here is a good ADO DB Tutorial - http://www.vbforums.com/showthread.php?t=337051
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: [RESOLVED] How can I find something in a field

    Originally Posted by zubairkhan

    MyRst.open "select * from myTable where firstname like %'Strep' %'"
    I tried to understand something in this line ? Who can help me ?

    Sorry but I'm Beginner with ADO...

    Thanks !
    Last edited by DubweiserTM; Feb 28th, 2006 at 02:58 PM.

  10. #10

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: [RESOLVED] How can I find something in a field

    I followed the ADO Tutorial but is there nothing about "Select" ?

    Is it complicated to use ADO or I'm stupid ?

    Help me please ! Thanks !

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] How can I find something in a field

    Here is a short example of connection and retrieving data from a table in an Access db.
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS ActiveX 2.x Data Objects library
    3. Private Sub Command1_Click()
    4.     On Error GoTo MyError
    5.  
    6.     Dim oCnn As ADODB.Connection
    7.     Dim oRs As ADODB.Recordset
    8.     Dim sSQL As String
    9.  
    10.     Set oCnn = New ADODB.Connection
    11.     oCnn.Connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;User Id=admin;Password=;"
    12.     oCnn.Open
    13.  
    14.     Set oRs = New ADODB.Recordset
    15.     sSQL = "SELECT * FROM Table1;"
    16.     oRs.Open sSQL, oCnn, adOpenKeyset, adLockOptimistic, adCmdText
    17.     If oRs.BOF = False And oRs.EOF = False Then
    18.         Do While oRs.EOF = False
    19.             MsgBox oRs.Fields("Field1").Value
    20.             oRs.MoveNext
    21.         Loop
    22.     Else
    23.         MsgBox "No Records Returned"
    24.     End If
    25.     oRs.Close
    26.     Set oRs = Nothing
    27.     oCnn.Close
    28.     Set oCnn = Nothing
    29.     Exit Sub
    30. MyError:
    31.     MsgBox Err.Number & " - " & Err.Description
    32. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: [RESOLVED] How can I find something in a field

    For example, Field1 contains Date (ex: 30-12-2005)

    How to find in Field1, all Date with 2005 ???

    Thanks !

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] How can I find something in a field

    Instead of ...
    sSQL = "SELECT * FROM Table1;"

    You would pass a sql string that will filter the table to the record(s) your looking for.

    sSQL = "SELECT * FROM Table1 WHERE Field1 = #30-12-2005#;"

    Field1 must be defined in the table as a Date Field Data type or you need a different syntax.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: [RESOLVED] How can I find something in a field

    Am I stupid or SQL is very complicated ???


    Thanks for your help RobDog888 but this line returns only this date 30-12-2005
    VB Code:
    1. sSQL = "SELECT * FROM Table1 WHERE Field1 = #30-12-2005#;"
    I use this statement and all records with 2005 are found:
    VB Code:
    1. sSQL = "SELECT * FROM Table1 WHERE Field1 like '%2005%'"

    Another questions regarding these two lines:

    (a) sSQL = "SELECT * FROM Table1 WHERE Dépositaire like '%home%' ;"
    (b) sSQL = "SELECT * FROM Table1 WHERE Dépositaire like '*eric*' ;"

    Why (a) returns many records and (b) returns nothing ?
    Last edited by DubweiserTM; Mar 1st, 2006 at 11:32 AM.

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] How can I find something in a field

    Because when using ADO the wild card cahracter is the "%" and not then "*".

    Sorry, I thought you wanted a particular date, but if you want a date range, like all 2005, then you can modify the stateement like so.

    VB Code:
    1. sSQL = "SELECT * FROM Table1 WHERE YEAR([Field1]) = 2005;"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16
    Junior Member
    Join Date
    Feb 2006
    Posts
    20

    Re: [RESOLVED] How can I find something in a field

    i was trying to get a user input, using a text box, i put the txt from the user input into varbale called nameSearch and added the code below Now i get error "-2147217904- No value given for one or more required prams"

    So i did a step through on the program and checked the var nameSearch, and it did indeed contain the data.
    Im just wondering if its my syntax that is incorrect, or just my code


    sSQL = "SELECT * FROM patient WHERE Name = " & nameSearch & " "
    'sSQL = "SELECT * FROM patient;"
    rs.Open sSQL, cn, adOpenKeyset, adLockOptimistic, adCmdText
    If rs.BOF = False And rs.EOF = False Then
    Do While rs.EOF = False
    List1.AddItem rs.Fields("ID")
    rs.MoveNext
    Loop
    Else
    MsgBox "No Records Returned"
    End If

    when i used the code from the example it outputs all the ID's into the list box

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

    Re: [RESOLVED] How can I find something in a field

    Your syntax isn't quite correct, text data needs to be enclosed in single quotes, and Name is a reserved word (which can cause problems) so needs to be in square brackets - like this:
    VB Code:
    1. sSQL = "SELECT * FROM patient WHERE [Name] = [B]'[/B]" & nameSearch & "[B]'[/B] "

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