KD Jayakody

Call Us: 076 72 33 595

Making a Software using MS Access – Production Ready

Oct 15, 2023

The following are some of the Code Snippets we used for the Search Bar. However, here you will see some other snippets also. Before you paste the code, you have to make a query.


Private Sub searchResult_AfterUpdate()
If Me.searchResult.ListIndex <> -1 Then
Me.searchFor.Value = Me.searchResult.Column(1)
Me.salesId.Value = Me.searchResult.Column(0)
‘ Optionally, set focus to the searchFor textbox
End If
End Sub


Private Sub searchFor_Change()
Dim vSearchString As String
vSearchString = searchFor.Text
TXTsEARCH.Value = vSearchString

If Len(Me.TXTsEARCH) <> 0 And InStr(Len(TXTsEARCH), TXTsEARCH, ” “, vbTextCompare) > 0 Then
Exit Sub
End If

Me.searchResult = Me.searchResult.ItemData(1)

If Not IsNull(Len(Me.searchFor)) Then
Me.searchFor.SelStart = Len(Me.searchFor)
End If

End Sub


Private Sub Command65_Click()
Dim db As DAO.Database
Dim strSQL As String
Const dbFailOnError = 128

‘ Check if the salesId textbox is not empty
If Len(Me.salesId) > 0 Then
‘ Open a connection to the database
Set db = CurrentDb

‘ Build the SQL statement to delete records
strSQL = “DELETE FROM Sales ” _
& “WHERE salesId = ” & Me.salesId
‘ Note: No single quotes around Me.salesId for Autonumber

‘ Debug statements
Debug.Print “SQL Statement: ” & strSQL
Debug.Print “salesId Value: ” & Me.salesId

‘ Execute the delete query
db.Execute strSQL, dbFailOnError

‘ Close the database connection

‘ Clear the salesId textbox
Me.salesId = “”
MsgBox “Please enter a value in the salesId textbox before deleting.”, vbExclamation, “No Value Entered”
End If
End Sub


Recent Posts