Skip to content

Instantly share code, notes, and snippets.

@Maffsie
Created October 31, 2009 00:48
Show Gist options
  • Save Maffsie/222864 to your computer and use it in GitHub Desktop.
Save Maffsie/222864 to your computer and use it in GitHub Desktop.
Option Explicit 'I forget what this does but it's good practice to use.
Private Type MusicDB 'Instantiate private type for flat-file database.
Title As Variant
Artist As Variant
ReleaseYear As Integer
End Type
'Instantiate variables
Dim viewed As Boolean 'Bool variable, determines if file has been viewed or not
Dim searchstring As String 'String variable, defines what the last thing searched for was
Dim path As String 'Path to the file in use
Dim fileopen As Boolean 'Bool variable, determines if file's been opened before
Dim pos As Integer 'Int variable, determines what the position in the file is
Dim greyness As String 'Mostly useless variable
Dim music As MusicDB 'Database variable for storing data from the private type.
Private Sub Form_activate() 'Run this code when the program launches
fileopen = False 'Reset fileopen to false
path = App.path & "\music.dat" 'Define where the file to be used is at
pos = 0 'Reset position.
greyness = txtSearch.ForeColor 'Mostly useless.
End Sub
Private Sub tmrRTSearch_Timer() 'Run this code every 1 millisecond.
If Not txtSearch.Text = searchstring And Not txtSearch.Text = "Search.." And viewed And fileopen Then 'Basically, if the search text isn't the default text, the file's open and in view, and the search text is different, run this code.
'This code runs every time the user enters anything into the searchbox.
lstArtist.Clear
lstTitle.Clear
lstYear.Clear 'Clear listboxes for output
imgCancel.Visible = True 'Show the Clear Search button (stolen from the apple website)
searchstring = txtSearch.Text 'Prevent the code from running every millisecond. Reduces CPU usage.
If fileopen Then Close 'Prevent file access issues
Open path For Random As 1 'Reopen the file
pos = 0 'Reset position
Dim count As Integer
count = 0 'Counter variable for determining how many search results are returned on each run.
Do While Not EOF(1) 'Loop through each item in the file, basically.
Get #1, pos + 1, music 'Read entry from the file
If InStr(music.Title, searchstring) <> 0 Or InStr(music.Artist, searchstring) <> 0 Or InStr(music.ReleaseYear, searchstring) <> 0 Then 'If the search string exists within the artist, title or year fields, run this code
lstArtist.AddItem music.Artist
lstTitle.AddItem music.Title
lstYear.AddItem music.ReleaseYear
count = count + 1 'Display search result
End If
pos = pos + 1 'Increment position to prevent infinite loop
Loop
Dim srchOut As String 'Output text for search querying
If count = 0 Then srchOut = "NO RESULTS"
If count > 0 Then srchOut = count & " RESULTS"
frmMain.Caption = "SUPER AWESOME SEARCH: SEARCHING FOR '" & searchstring & "' - " & srchOut 'Set title to search information
ElseIf Not fileopen And Not txtSearch.Text = "Search.." Then 'Alerts the user to the fact that searching for data without opening the file is a retarded concept.
MsgBox ("You can't search for something without opening the file! :(")
txtSearch.Text = "Search.."
imgCancel.Visible = False
frmMain.Caption = "SUPER AWESOME MUSIC LIBRARY"
End If
If frmMain.Caption <> "SUPER AWESOME MUSIC LIBRARY" And (txtSearch.Text = "Search.." Or txtSearch.Text = "") Then frmMain.Caption = "SUPER AWESOME MUSIC LIBRARY" 'Reset title after search is complete
End Sub
Private Sub txtSearch_GotFocus() 'Simple textbox highlighting and clearing code. Used for niceness.
If txtSearch.Text = "Search.." Then
txtSearch.Text = ""
Else
txtSearch.SelStart = 0
txtSearch.SelLength = Len(txtSearch.Text)
End If
txtSearch.ForeColor = vbBlack
End Sub
Private Sub txtSearch_LostFocus()
If txtSearch.Text = "" Then
txtSearch.Text = "Search.."
imgCancel.Visible = False
frmMain.Caption = "SUPER AWESOME MUSIC LIBRARY"
End If
txtSearch.ForeColor = greyness 'The greyness variable here just stores the colour code for a light-ish shade of grey.
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment