Basic VBA outline for a search function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VBnewb13
    New Member
    • Jun 2010
    • 17

    Basic VBA outline for a search function

    Hello everyone,

    I already accomplished what I am trying to do by using Microsoft Access, but I would like to somehow do it in excel as well (or atleast do something similar).

    What I have done is created a search enging (with the advice of a few kind experts from bytes!) that searches through tables and displays the results in a subform based upon user input. The user has the ability to select which columns from the table are displayed in the results as well. Here is the code:

    Code:
    Private Sub cmdProcess_Click() 
        On Error Resume Next    
        Dim ctl As Control 
        Dim intDataX As Integer, intDataY As Integer, iinti As Integer, intj As Integer 
        'position the controls on the form 
        intX = 1000 
        intY = 100 
        If Me!lstEmployees.ItemsSelected.Count = 0 Then 
            MsgBox "Select at least one item from the list first", vbExclamation, "System Message" 
            Me!lstEmployees.SetFocus 
            Exit Sub 
        End If 
        'adjust the subform to hook to the placeholder then open the newly created form in degin 
        ' and set its recordsource accordingly 
        Me!Submain.SourceObject = "fsubPlaceHolder" 
        'delete pre- existing form 
        DoCmd.DeleteObject acForm, "fsubEmployees" 
        'copy the template to a new form 
        DoCmd.CopyObject "", "fsubEmployees", acForm, "fsubEmployeesEmptyTemplate" 
        'and open in design 
        DoCmd.OpenForm "fsubEmployees", acDesign, "", "", , acHidden 
        Forms!fsubEmployees.RecordSource = "SELECT * FROM Employees" 
        'loop through the listbox and create a textbox for each field selected in the list 
        For Each varItem In Me!lstEmployees.ItemsSelected 
            Set ctl = CreateControl("fsubEmployees", acTextBox, , acDetail, lstEmployees.ItemData(varItem), _ 
                                    intX, intY) 
            ctl.Name = lstEmployees.ItemData(varItem) 
        Next varItem 
        'close the form and save 
        DoCmd.Close acForm, "fsubEmployees", acSaveYes 
        'now adjust the subform to pick up on the newly created form having the fields for display 
        Me!Submain.SourceObject = "fsubEmployees" 
    End Sub 
      
    Private Sub Reset(lst As Access.ListBox) 
        On Error Resume Next 
        Dim i As Integer 
        With lst 
            For i = (.ItemsSelected.Count - 1) To 0 Step -1 
                .Selected(.ItemsSelected(i)) = False 
            Next i 
        End With 
    End Sub 
      
    Private Sub cmdReset_Click() 
        On Error Resume Next 
        Call Reset(Me!lstEmployees) 
        Me!Submain.SourceObject = "fsubPlaceholder" 
    End Sub

    I would like to do something similar in excel. Maybe have a form that asks for user input, does the same process outlined above, but displays the results in a new tab rather than a subform (excel does not have subforms I believe)

    I apologize if it is vague but I am definitely open to any suggestions or guidance at all :)

    Thanks
Working...