If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Search & edit function

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-04-11, 10:13
onetwntyone onetwntyone is offline
Registered User
 
Join Date: May 2011
Posts: 1
Question Search & edit function

hello

i built a reasonably simple database in access (with very limited knowledge) and i want to include a button that allows users to search for a record based on a parameter they choose.

In other words i want to have a combo box listing the row headers of a n existing table. Then i want there to be a text box where they can enter the word/portion of the word they want to search for.

Can any one help?
Reply With Quote
  #2 (permalink)  
Old 05-04-11, 13:36
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
1. I suppose that the form is bound to the table (here named 'Shipments') in which the search is to be performed. If it's not the case you'll have to adapt the code.

2. On the form, create a combo box, name it 'cbo_Columns' and assign 'Field List' to its RowSourceType property and the name of the table ('Shipments') to its RowSource property.

3. On the same form, also create a text box (name: 'txt_Search') and a command button (name: 'cmd_Search').

4. You can use this code to perform the search:
Code:
Private Sub cmd_Search_Click()

    Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
    Dim rst As DAO.Recordset
    Dim strCriteria As String
    Dim strOperator As String
    Dim strArgument As String

    If (Len(Nz(Me.cbo_Columns.Value, "")) > 0) And (Len(Nz(Me.txt_Search.Value, "")) > 0) Then
        Set dbs = CurrentDb
        Set tdf = dbs.TableDefs("Shipments")
        Select Case tdf.Fields(Me.cbo_Columns.Value).Type
            Case dbText, dbMemo
                strArgument = "'" & Me.txt_Search.Value & "'"
                strOperator = " Like "
            Case dbDate
                strArgument = "#" & Format(Me.txt_Search.Value, "mm/dd/yyyy") & "#"
                strOperator = " = "
            Case Else
                strArgument = Me.txt_Search.Value
                strOperator = " = "
        End Select
        Set tdf = Nothing
        strCriteria = Me.cbo_Columns.Value & strOperator & strArgument
        Set rst = Me.RecordsetClone
        rst.FindFirst strCriteria
        If rst.NoMatch = False Then
            Me.Bookmark = rst.Bookmark
        Else
            MsgBox "No match: " & strCriteria, vbInformation, "Not Found"
        End If
        rst.Close
        Set rst = Nothing
    End If
    
End Sub
__________________
Have a nice day!
Reply With Quote
Reply

Tags
combo box, edit, parameters, records, search

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On