Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

Using VBA to filter a field by first letter?

I'm working on a small inventory database that lists all of the software at my company.  I am attempting to make a form that allows a user to search for a software package by first letter of the application name.

I've tried to write some VBA code to do this, but I am new to A2k and VBA so I am sure I am missing something since I cannot get this to work.  I always get an error stating that I cannot assign a value to the Me.Filter action.  The following is the code I was trying to use:

Begin Code
----------------------------------
Private Sub Sort_By_First_Letter_Of_Program_Name_Click()
On Error GoTo Err_Sort_By_First_Letter_Of_Program_Name_Click
    MsgBox "Hello World"
    Dim strLetter As String, strInput As String
    strInput = "Please type in the first letter of the program name: "
    strLetter = InputBox(Prompt:=strInput)
    Me.Filter = "ProgramName = LIKE M*"
    Me.FilterOn = True


   

Exit_Sort_By_First_Letter_Of_Program_Name_Click:
    Exit Sub

Err_Sort_By_First_Letter_Of_Program_Name_Click:
    MsgBox Err.Description
    Resume Exit_Sort_By_First_Letter_Of_Program_Name_Click
   
End Sub
----------------------------------------
End Code

I am beginning to think I may need to use ADO or DAO to do this, but I am unfamiliar with how to use either one of them.  Any pointers or hints would be greatly appreciated.
0
cjweti
Asked:
cjweti
  • 4
1 Solution
 
nexusnationCommented:
try the below.

Begin Code
----------------------------------
Private Sub Sort_By_First_Letter_Of_Program_Name_Click()
On Error GoTo Err_Sort_By_First_Letter_Of_Program_Name_Click
   MsgBox "Hello World"
   Dim strLetter As String, strInput As String
   strInput = "Please type in the first letter of the program name: "
   strLetter = InputBox(Prompt:=strInput)
   Me.Filter = "[ProgramName] = "LIKE" & strinput & "*"
   Me.FilterOn = True

Exit_Sort_By_First_Letter_Of_Program_Name_Click:
   Exit Sub

Err_Sort_By_First_Letter_Of_Program_Name_Click:
   MsgBox Err.Description
   Resume Exit_Sort_By_First_Letter_Of_Program_Name_Click
   
End Sub
----------------------------------------
End Code
0
 
Arthur_WoodCommented:
very very minor item--this line:

Me.Filter = "[ProgramName] = "LIKE" & strinput & "*"

will generate an error.


Change it to:

Me.Filter = "[ProgramName] LIKE '" & strinput & "*'"

1) notice the space after the work LIKE, and

2) the strInput needs to be enclosed in '....' s

AW
0
 
cjwetiAuthor Commented:
I tried the code you posted nexusnation, and now I am getting a Type Mismatch error.  

At first I thought it might just be a missing quotation mark or the use of strInput in the Me.Filter line instead of strLetter, but I get the same error regardless of which variable I use.
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
cjwetiAuthor Commented:
I tried the code you posted nexusnation, and now I am getting a Type Mismatch error.  

At first I thought it might just be a missing quotation mark or the use of strInput in the Me.Filter line instead of strLetter, but I get the same error regardless of which variable I use.
0
 
cjwetiAuthor Commented:
Arthur Wood,

Your line does work, however I had to use strLetter instead of strInput as the variable.

Thanks for the fast response nexusnation and Arthur.  I'll award points here shortly.
0
 
cjwetiAuthor Commented:
Fast answer, minor issues with line format and incorrect variable use but other than that right on.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now